COTOHA API で構文解析 (Node.js)


COTOHA API Portal の使用例です。

次と同じことを Node.js で行いました。

COTOHA API で構文解析

フォルダー構造

$ tree -a
.
├── .env
├── get_config.js
├── get_token.js
└── parsing.js
parsing.js
#! /usr/bin/node
// ---------------------------------------------------------------
//
//  parsing.js
//
//                  Feb/23/2020
//
// ---------------------------------------------------------------
var get_config = require('./get_config.js')
var get_token = require('./get_token.js')
// ---------------------------------------------------------------
function parse_proc(config,sentence)
{
    var Client = require('node-rest-client').Client

    const headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer " + config.access_token
        }

    const data = {
        "sentence": sentence,
        "type": "default"
        }

    const url = config.developer_api_base + "v1/parse"

    const args = {
        data: data,
        headers: headers
        }

    var client = new Client()

    client.post(url, args, function (data, response) {
        for (it in data["result"])
            {
            for (jt in data["result"][it].tokens)
                {
                const token = data["result"][it].tokens[jt]
                console.log (token.form + "\t" + token.pos)
                }
            }
        console.error ("*** 終了 ***")
    })
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const sentence = "特急はくたか"

const config = get_config.get_config_proc()

get_token.get_token_proc(config,sentence,parse_proc)

// ---------------------------------------------------------------
get_config.js
// ---------------------------------------------------------------
//
//  get_config.js
//
//                  Feb/23/2020
//
// ---------------------------------------------------------------
exports.get_config_proc = function ()
{
    const dotenv = require('dotenv')

    dotenv.config()

    const config = {
        client_id: `${process.env.CLIENT_ID}`,
        client_secret: `${process.env.CLIENT_SECRET}`,
        developer_api_base: `${process.env.DEVELOPER_API_BASE_URL}`,
        access_token_publish_url: `${process.env.ACCESS_TOKEN_PUBLISH_URL}`,
}

    return config
}

// ---------------------------------------------------------------
get_token.js
// ---------------------------------------------------------------
//
//  get_token.js
//
//                  Feb/23/2020
//
// ---------------------------------------------------------------
exports.get_token_proc = function(config,sentence,callback)
{
    var Client = require('node-rest-client').Client

    const data = {
        "grantType": "client_credentials",
        "clientId": config.client_id,
        "clientSecret": config.client_secret
        }

    const url = config.access_token_publish_url

    const headers={
        "Content-Type": "application/json"
        }

    const args = {
        data: data,
        headers: headers
    }

    var client = new Client()

    client.post(url, args, function (data, response) {
        config['access_token'] = data.access_token
        callback(config,sentence)
    })
}

// ---------------------------------------------------------------

実行コマンド

export NODE_PATH=/usr/lib/node_modules
./parsing.js