pygeonlp.api.parser module
- class pygeonlp.api.parser.Parser(db_dir=None, jageocoder=None, address_regex=None, **options)
ベースクラス:
object
形態素解析と地名語抽出を行なうパーザ。
- service
利用する Service インスタンス。
- Type:
pygeonlp.service.Service
- jageocoder_tree
住所ジオコーダー jageocoder の AddressTree インスタンス。 利用しない場合は False を指定してください。
- Type:
jageocoder.address.AddressTree
- address_regex
住所要素の先頭になりえる固有名クラスの正規表現(コンパイル済み)。
- Type:
regex
- scoring_class
パスのスコアリングに利用するクラス。
- Type:
class
- __init__(db_dir=None, jageocoder=None, address_regex=None, **options)
パーザを初期化します。
- パラメータ:
db_dir (PathLike, optional) -- データベースディレクトリ。 省略した場合は
api.init.get_db_dir()
が返す値を利用します。jageocoder (jageocoder.tree.AddressTree, optional) -- 利用する住所ジオコーダーを指定します。省略した場合、 jageocoder モジュールのデフォルトオブジェクトを利用します。 False を指定した場合、ジオコーディング機能を利用しません。
address_regex (str, optional) -- 住所表記の開始とみなす地名語の固有名クラスを表す正規表現。 省略した場合、
r'^(都道府県|市区町村|行政地域|居住地名)(/.+|)'
を利用します。
- add_address_candidates(lattice, keep_nodes=False, **kwargs)
ラティス表現に住所候補を追加します。
- パラメータ:
lattice (list) -- analyze_sentence の結果のラティス表現(住所を含まない)。
keep_nodes (bool, optional) -- 住所以外のノードを維持するかどうかを指示するフラグ。 デフォルトは False (維持しない)。
- 戻り値:
住所候補を追加したラティス表現。
- 戻り値の型:
list
サンプル
>>> import pygeonlp.api as api >>> from pygeonlp.api.devtool import pp_lattice >>> from pygeonlp.api.node import Node >>> api.init() >>> parser = api.parser.Parser(jageocoder=True) >>> lattice = parser.analyze_sentence('アメリカ大使館:港区赤坂1-10-5') >>> lattice_address = parser.add_address_candidates(lattice, True) >>> pp_lattice(lattice_address) #0:'アメリカ大使館' アメリカ大使館(NORMAL) #1:':' :(NORMAL) #2:'港区' 港区(GEOWORD:['東京都']) 港区(GEOWORD:['愛知県', '名古屋市']) 港区(GEOWORD:['大阪府', '大阪市']) 港区赤坂1-10-(ADDRESS:東京都/港区/赤坂/一丁目/10番)[6] #3:'赤坂' 赤坂(GEOWORD:['上毛電気鉄道', '上毛線']) 赤坂(GEOWORD:['東京地下鉄', '9号線千代田線']) 赤坂(GEOWORD:['富士急行', '大月線']) 赤坂(GEOWORD:['福岡市', '1号線(空港線)']) #4:'1' 1(NORMAL) #5:'-' -(NORMAL) #6:'10' 10(NORMAL) #7:'-' -(NORMAL) #8:'5' 5(NORMAL) >>> lattice_address = parser.add_address_candidates(lattice) >>> pp_lattice(lattice_address) #0:'アメリカ大使館' アメリカ大使館(NORMAL) #1:':' :(NORMAL) #2:'港区赤坂1-10-' 港区赤坂1-10-(ADDRESS:東京都/港区/赤坂/一丁目/10番)[6] #3:'5' 5(NORMAL) >>> node = lattice_address[2][0] >>> len(node.morphemes) 6 >>> '東京都' in node.morphemes[0].prop['hypernym'] True >>> node.morphemes[1].node_type == Node.NORMAL True
- analyze(sentence, **kwargs)
文を解析した結果をラティス表現で返します。
- パラメータ:
sentence (str) -- 解析するテキスト。
- 戻り値:
解析結果のラティス表現。
- 戻り値の型:
list
サンプル
>>> from pygeonlp.api.parser import Parser >>> parser = Parser() >>> parser.analyze('今日は国会議事堂前まで歩きました。') [[{"surface": "今日", "node_type": "NORMAL", "morphemes": {"conjugated_form": "*", "conjugation_type": "*", "original_form": "今日", "pos": "名詞", "prononciation": "キョー", "subclass1": "副詞可能", "subclass2": "*", "subclass3": "*", "surface": "今日", "yomi": "キョウ"}, "geometry": null, "prop": null}], [{"surface": "は", "node_type": "NORMAL", "morphemes": {"conjugated_form": "*", "conjugation_type": "*", "original_form": "は", "pos": "助詞", "prononciation": "ワ", "subclass1": "係助詞", "subclass2": "*", "subclass3": "*", "surface": "は", "yomi": "ハ"}, "geometry": null, "prop": null}], [{"surface": "国会議事堂前", "node_type": "GEOWORD", "morphemes": {"conjugated_form": "*", "conjugation_type": "*", "original_form": "国会議事堂前", "pos": "名詞", "prononciation": "", "subclass1": "固有名詞", "subclass2": "地名語", "subclass3": "Bn4q6d:国会議事堂前駅", "surface": "国会議事堂前", "yomi": ""}, "geometry": {"type": "Point", "coordinates": [139.74534166666666, 35.674845]}, "prop": {"body": "国会議事堂前", "dictionary_id": 3, "entry_id": "LrGGxY", "geolod_id": "Bn4q6d", "hypernym": ["東京地下鉄", "4号線丸ノ内線"], "institution_type": "民営鉄道", "latitude": "35.674845", "longitude": "139.74534166666666", "ne_class": "鉄道施設/鉄道駅", "railway_class": "普通鉄道", "suffix": ["駅", ""], "dictionary_identifier": "geonlp:ksj-station-N02"}}, {"surface": "国会議事堂前", "node_type": "GEOWORD", "morphemes": {"conjugated_form": "*", "conjugation_type": "*", "original_form": "国会議事堂前", "pos": "名詞", "prononciation": "", "subclass1": "固有名詞", "subclass2": "地名語", "subclass3": "cE8W4w:国会議事堂前駅", "surface": "国会議事堂前", "yomi": ""}, "geometry": {"type": "Point", "coordinates": [139.74305333333334, 35.673543333333335]}, "prop": {"body": "国会議事堂前", "dictionary_id": 3, "entry_id": "4NFELa", "geolod_id": "cE8W4w", "hypernym": ["東京地下鉄", "9号線千代田線"], "institution_type": "民営鉄道", "latitude": "35.673543333333335", "longitude": "139.74305333333334", "ne_class": "鉄道施設/鉄道駅", "railway_class": "普通鉄道", "suffix": ["駅", ""], "dictionary_identifier": "geonlp:ksj-station-N02"}}], [{"surface": "まで", "node_type": "NORMAL", "morphemes": {"conjugated_form": "*", "conjugation_type": "*", "original_form": "まで", "pos": "助詞", "prononciation": "マデ", "subclass1": "副助詞", "subclass2": "*", "subclass3": "*", "surface": "まで", "yomi": "マデ"}, "geometry": null, "prop": null}], [{"surface": "歩き", "node_type": "NORMAL", "morphemes": {"conjugated_form": "五段・カ行イ音便", "conjugation_type": "連用形", "original_form": "歩く", "pos": "動詞", "prononciation": "アルキ", "subclass1": "自立", "subclass2": "*", "subclass3": "*", "surface": "歩き", "yomi": "アルキ"}, "geometry": null, "prop": null}], [{"surface": "まし", "node_type": "NORMAL", "morphemes": {"conjugated_form": "特殊・マス", "conjugation_type": "連用形", "original_form": "ます", "pos": "助動詞", "prononciation": "マシ", "subclass1": "*", "subclass2": "*", "subclass3": "*", "surface": "まし", "yomi": "マシ"}, "geometry": null, "prop": null}], [{"surface": "た", "node_type": "NORMAL", "morphemes": {"conjugated_form": "特殊・タ", "conjugation_type": "基本形", "original_form": "た", "pos": "助動詞", "prononciation": "タ", "subclass1": "*", "subclass2": "*", "subclass3": "*", "surface": "た", "yomi": "タ"}, "geometry": null, "prop": null}], [{"surface": "。", "node_type": "NORMAL", "morphemes": {"conjugated_form": "*", "conjugation_type": "*", "original_form": "。", "pos": "記号", "prononciation": "。", "subclass1": "句点", "subclass2": "*", "subclass3": "*", "surface": "。", "yomi": "。"}, "geometry": null, "prop": null}]]
注釈
ラティス表現では全ての地名語の候補を列挙して返します。
analyze_sentence()
に住所ジオコーディングの結果も追加します。
- analyze_sentence(sentence, **kwargs)
sentence を解析し、全ての地名語候補を含む Node リストを ラティス表現で返します。
- パラメータ:
sentence (str) -- 解析対象の文字列。
- 戻り値:
ラティス表現。形態素ごとに、対応する地名語候補のリストを含むリストです。
- 戻り値の型:
list
サンプル
>>> import pygeonlp.api as api >>> api.init() >>> parser = api.parser.Parser() >>> for nodes in parser.analyze_sentence('NIIは千代田区一ツ橋2-1-2にあります。'): ... [x.simple() for x in nodes] ... ['NII(NORMAL)'] ['は(NORMAL)'] ["千代田区(GEOWORD:['東京都'])"] ['一ツ橋(NORMAL)'] ['2(NORMAL)'] ['-(NORMAL)'] ['1(NORMAL)'] ['-(NORMAL)'] ['2(NORMAL)'] ['に(NORMAL)'] ['あり(NORMAL)'] ['ます(NORMAL)'] ['。(NORMAL)']
- check_word(word, filter)
Word の形態素情報が filter に含まれる全ての key, value と 一致しているかどうかを調べます。
- パラメータ:
word (dict) -- チェック対象となる dict オブジェクト。
filter (dict) -- チェックする項目と値を持つ dict オブジェクト。
- 戻り値:
全ての項目が一致する場合 True, 一つでも一致しない場合 False。
- 戻り値の型:
bool
- get_addresses(lattice, pos)
ラティス表現の単語列から住所部分を抽出します。
- パラメータ:
lattice (list) -- analyze_sentence() が返すノードのリスト(ラティス表現)。
pos (int) -- 住所抽出を開始するリストのインデックス。
- 戻り値:
以下の要素を持つ dict オブジェクトを返します。
- address: jageocoder.address.AddressNode
ジオコーディングの結果, 住所ではなかった場合 None。
- pos: int
住所とみなされた形態素ノードの次のインデックス。
- 戻り値の型:
dict
- get_surfaces(lattice, pos_from, limit)
ラティス表現の単語列の pos 番目から、 limit で指定した 文字数を超える位置までの表記のリストを取得します。
単語の surface と original_form の組み合わせを列挙します。
- パラメータ:
lattice (list) -- analyze_sentence() が返すノードのリスト(ラティス表現)。
pos_from (int) -- 表記リストの先頭となるノードのインデックス。
limit (int) -- 文字列の長さ(limit を超えたノードを含む)
- 戻り値:
単語列(単語表記のリスト)
- 戻り値の型:
list
- set_jageocoder(jageocoder)
この Parser が利用する jageocoder を変更します。
- パラメータ:
jageocoder (jageocoder.tree.AddressTree, optional) -- 利用する住所ジオコーダーを指定します。省略した場合、 jageocoder モジュールのデフォルトオブジェクトを利用します。 False を指定した場合、ジオコーディング機能を利用しません。
- class pygeonlp.api.parser.ParseError
ベースクラス:
RuntimeError
パージング処理の際に例外が起こると、このクラスが発生します。