Baidu NLP - Lexer segmentations

#!/usr/bin/node

function lexer(phrases) {
    let AipNlpClient = require('baidu-aip-sdk').nlp
    let sprintf = require('sprintf-js').sprintf
    let fs = require('fs')

    let client = new AipNlpClient(
        /* AppID     */ '11655915',
        /* ApiKey    */ '9X9WmwsdM3kCr462UmTc23D3',
        /* SecretKey */ '2szY9OwrlGA8NSHR0IUARBP5MEDuzSfj'
    )

    async function segment_text(text) {
        try {
            result = await client.lexer(text)
            return result.items.map(x => x.item).join(' ')
        }
        catch(e) {
            console.log(e)
            return null
        }
    }

    function segment_array(arr, index) {
        if (arr.length > 0 && index < arr.length) {
            segment_text(arr[index]).then((result) => {
                console.log(result)
                // 根据 QPS 设置调用频率

                setTimeout(segment_array, 250, arr, index + 1)
            })
        }
    }
    
    if (phrases.length > 0) {
        segment_array(phrases, 0)
    }
}

function main() {
    lexer(process.argv.slice(2, process.argv.length))
}

main()

测试

$ ./baidu-lexer.js 我们从武汉来到了北京参观故宫博物馆
我们 从 武汉 来到 了 北京 参观 故宫博物馆