0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

COTOHA で固有名詞の抽出

Last updated at Posted at 2020-02-22

COTOHA API Portal の使用例です。

参考ページ

APIリファレンス
固有表現抽出

次の文から固有名詞を抽出してみます。

特急はくたかで富山に向かいます。それから、金沢に行って、兼六園に行きます。

フォルダー構造

$ tree -a
.
├── .env
├── get_config.py
├── get_token.py
└── proper_noun.py
proper_noun.py
#! /usr/bin/python
# -*- coding:utf-8 -*-
#
#	proper_noun.py
#
#						Feb/22/2020
# ----------------------------------------------------------------------
import sys
import json
import requests
# ----------------------------------------------------------------------
from get_config import get_config_proc
from get_token import get_token_proc
# ----------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
config = get_config_proc()
access_token = get_token_proc(config)
#
sentence = "特急はくたかで富山に向かいます。それから、金沢に行って、兼六園に行きます。"
#
headers={
	"Content-Type": "application/json",
	"Authorization": "Bearer " + access_token
	}
#
data = {
	"sentence": sentence,
	"type": "default"
	}

str_json = json.dumps(data)
url = config['DEVELOPER_API_BASE_URL'] + "v1/ne"
try:
	rr=requests.post(url,headers=headers,data=str_json)
	dict_aa = json.loads(rr.text)
	llx = len(dict_aa['result'])
	sys.stderr.write("llx(result) = %d\n" % llx)
#
	for unit in dict_aa['result']:
		print(unit['form'])
except Exception as ee:
	sys.stderr.write("*** error *** in requests.post ***\n")
	sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("*** 終了 ***\n")
# ----------------------------------------------------------------------

get_config.py get_token.py はこちら
COTOHA API で構文解析

実行結果

$ ./proper_noun.py
*** 開始 ***
llx(result) = 3
富山
金沢
兼六園
*** 終了 ***
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?