Cotoha API, created by NTT Communications Corporation, for Python
Project description
CotohapPy: Cotoha for Python
CotohapPy (Japanese: コトハッピー) is for connecting to Cotoha API, one of the Japanese morphological analysis engines, and is for reshaping the response more readably.
Installation
The easiest way to install the latest version is by using pip/easy_install to pull it from PyPI:
pip install cotohappy
You may also use Git to clone the repository from GitHub and install it manually:
git clone https://github.com/278Mt/cotohappy.git
cd cotohappy
python setup.py install
Python 3.7 and 3.8 are supported (frequently updated).
Requirements
- json
- requests
Usage
This is one of the examples.
├── payload.json
└── sjgyoen.py
JSON preperation (payload.json
):
{
"AccessTokenPublishURL": "https://api.ce-cotoha.com/v1/oauth/accesstokens",
"APIBaseURL" : "https://api.ce-cotoha.com/api/dev/",
"ClientId" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456",
"ClientSecret" : "7890abcdefghijkl"
}
Programme (sjgyoen.py
)
import cotohappy
import requests
from bs4 import BeautifulSoup
def get_kotonoha_story():
url = 'https://www.kotonohanoniwa.jp/page/product.html'
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
p = soup.find_all('p', class_='mb24')[-1]
return p.text
if __name__ == '__main__':
coy = cotohappy.API()
""" getting parse """
print('\n#### parse origin ####')
sentence = get_kotonoha_story()
type_ = 'default'
parse_li = coy.parse(sentence, type_)
for parse in parse_li:
print(parse)
print(parse.key_name)
""" getting tokens; it is a little more difficult than MeCab Janome """
print('\n#### parse tokens ####')
for parse in parse_li:
for token in parse.tokens:
print(token)
print(token.key_name)
""" if you extract just nouns, you write: """
print('\n#### extract nouns ####')
nouns: [str] = []
for parse in parse_li:
for token in parse.tokens:
if token.pos == '名詞':
nouns.append(token.form)
print(nouns)
Output:
#### parse origin ####
靴職人を 0,1,D,1,2
目指す 1,2,D,0,1
高校生・タカオは、 2,51,D,2,3
雨の 3,4,D,0,1
朝は 4,7,D,0,1
...
互いの 47,48,D,0,1
思いを 48,51,D,0,1
よそに 49,51,D,0,1
梅雨は 50,51,D,0,1
明けようとしていた。 51,-1,O,0,6
form id,head,dep,chunk_head,chunk_func
#### parse tokens ####
靴 0,クツ,靴,名詞,*,*,*,*,*
職人 1,ショクニン,職人,名詞,*,*,*,*,*
を 2,ヲ,を,格助詞,連用,*,*,*,*
目指 3,メザ,目指す,動詞語幹,S,*,*,*,*
す 4,ス,す,動詞接尾辞,連体,*,*,*,*
...
し 148,シ,し,動詞活用語尾,*,*,*,*,*
て 149,テ,て,動詞接尾辞,接続,連用,*,*,*
い 150,イ,いる,動詞語幹,A,Lて連用,*,*,*
た 151,タ,た,動詞接尾辞,終止,*,*,*,*
。 152,,。,句点,*,*,*,*,*
form id,kana,lemma,pos,features[:5]
#### extract nouns ####
['靴', '職人', '高校生', 'タカオ', '雨', '朝', '学校', '公園', '日本', '庭園', '靴', 'スケッチ', 'ある日', 'タカオ', 'ひとり', '缶', 'ビール', '年上', '女性', 'ユキノ', 'ふたり', '約束', '雨', '日', '逢瀬', '心', '居場所', 'ユキノ', '彼女', '靴', 'タカオ', '六月', '空', '揺れ', '互い', '思い', 'よそ', '梅雨']
Please check details on examples.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for cotohappy-0.0.2.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc5a673a590e87535c9740bf0d16f728a2ee7779a6a11bde2c7743a6b4b59923 |
|
MD5 | e936223fc33e844db9c43d1ed53a98e4 |
|
BLAKE2b-256 | 6cadaedcc759d24f5ccc3ef2c950f55be1d84c2dde774e6ee1c8b09d9f27616a |