LoginSignup
1
1

More than 3 years have passed since last update.

COTOHA でキーワードの抽出 (Node.js)

Posted at

COTOHA API Portal の使用例です。

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

COTOHA でキーワードの抽出

フォルダー構造

$ tree -a
.
├── akai_rousoku.txt
├── .env
├── get_config.js
├── get_token.js
└── key_word.js
key_word.js
#! /usr/bin/node
// ---------------------------------------------------------------
//
//  key_word.js
//
//                  Feb/23/2020
//
// ---------------------------------------------------------------
var fs = require("fs")
var get_config = require('./get_config.js')
var get_token = require('./get_token.js')
// ---------------------------------------------------------------
function key_word_proc(config,doc)
{
    var Client = require('node-rest-client').Client

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

    const data = {
        "document": doc,
        "type": "default"
        }

    const str_json = JSON.stringify (data)

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

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

    var client = new Client()

    client.post(url, args, function (data, response) {
    llx = data["result"].length
    console.log("llx = " + llx)
    for (it in data["result"])
        {
        const unit = data["result"][it]
        console.log(unit['form'] + "\t" + unit['score'])
        }
        console.error ("*** 終了 ***")
    })
}

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

const filename=process.argv[2]
console.error (filename)

if (fs.existsSync(filename))
    {
    const doc = fs.readFileSync (filename,'utf8')

    const config = get_config.get_config_proc()

    try
        {
        get_token.get_token_proc(config,doc,key_word_proc)
        }
    catch (error)
        {
        console.error ("*** error *** from get_token_proc ***")
        console.error (error)
        }
    }
else
    {
    console.error ("*** error *** " + filename + " doesn't exist. ***")
    }
// ---------------------------------------------------------------

get_config.js get_token.js はこちら
COTOHA API で構文解析 (Node.js)

実行コマンド

export NODE_PATH=/usr/lib/node_modules
./key_word.js akai_rousoku.txt
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1