pygeonlp.api.dict_manager module

class pygeonlp.api.dict_manager.DictManager(db_dir: str | bytes | PathLike | None = None)

ベースクラス: object

地名語辞書の管理を行なうクラスです。

_dict_cache

辞書ID(整数)をキー、辞書identifier(文字列)を値にもつ マッピングテーブル(キャッシュ用)。

Type:

dict

capi_ma

C 実装の拡張形態素解析機能を利用するためのオブジェクト。

Type:

pygeonlp.capi オブジェクト

db_dir

管理対象データベースディレクトリのパス。

Type:

str

addDictionaryFromCsv(csvfile, name=None, identifier=None)

指定したパスにある地名解析辞書(CSVファイル)をデータベースに登録します。

辞書の name と identifier を省略した場合、 CSV ファイル名から自動的に設定されます。

既に同じ identifier を持つ辞書データがデータベースに登録されている場合、 削除してから新しい辞書データを登録します。

パラメータ:
  • csvfile (str) -- 地名解析辞書ファイルのパス。

  • name (str, optional) -- 辞書名。省略した場合、 CSV ファイルの basename を利用します。

  • identifier (str, optional) -- 辞書 identifier。省略した場合、 CSV ファイルの basename を取り、 geonlp:<basename> を利用します。

戻り値:

常に True。登録に失敗した場合は例外が発生します。

戻り値の型:

bool

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.addDictionaryFromCsv(
...   'base_data/ksj-station-N02-2020.csv',
...   name='日本の鉄道駅(2020年)')
True
>>> manager.updateIndex()
True
addDictionaryFromFile(jsonfile, csvfile)

指定したパスにある辞書メタデータ(JSONファイル)と 地名解析辞書(CSVファイル)をデータベースに登録します。

既に同じ identifier を持つ辞書データがデータベースに登録されている場合、 削除してから新しい辞書データを登録します。

パラメータ:
  • jsonfile (str) -- 辞書メタデータファイルのパス。

  • csvfile (str) -- 地名解析辞書ファイルのパス。

戻り値:

常に True。登録に失敗した場合は例外が発生します。

戻り値の型:

bool

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.addDictionaryFromFile(
...   'base_data/geoshape-city.json', 'base_data/geoshape-city.csv')
True
>>> manager.updateIndex()
True
addDictionaryFromWeb(url, params=None, **kwargs)

指定した URL にあるページに含まれる辞書メタデータ(JSON-LD)を取得し、 メタデータに記載されている URL から地名解析辞書(CSVファイル)を取得し、 データベースに登録します。

既に同じ identifier を持つ辞書データがデータベースに登録されている場合、 削除してから新しい辞書データを登録します。

パラメータ:
  • url (str) -- 辞書メタデータを含むウェブページの URL。

  • params (dict, optional) -- requests.get に渡す params パラメータ。

  • **kwargs (dict, optional) -- requests.get に渡す kwargs パラメータ。

戻り値:

常に True。登録に失敗した場合は例外が発生します。

戻り値の型:

bool

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.clearDatabase()
True
>>> manager.addDictionaryFromWeb('https://geonlp.ex.nii.ac.jp/dictionary/geoshape-city/')
True
>>> manager.updateIndex()
True
clearDatabase()

データベースに登録されている辞書をクリアします。 データベース内の地名語も全て削除されます。

この関数は、データベースを作り直す際に利用します。

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.clearDatabase()
True
>>> manager.addDictionaryFromWeb(
...   'https://geonlp.ex.nii.ac.jp/dictionary/geoshape-city/')
True
>>> manager.updateIndex()
True
getDictionaries()

インストール済み辞書のメタデータ一覧を返します。

戻り値:

Metadata インスタンスのリスト。

戻り値の型:

list

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = Manager()
>>> sorted([x.get_identifier() for x in manager.getDictionaries()])
['geonlp:geoshape-city', 'geonlp:geoshape-pref', 'geonlp:ksj-station-N02']
getDictionary(id_or_identifier)

指定した id または identifier を持つ辞書のメタデータを返します。 id, identifier に一致する辞書が存在しない場合は None を返します。

パラメータ:

id_or_identifier (str or int) -- str の場合は辞書 identifier で指定。 int の場合は内部辞書 id で指定。

戻り値:

Metadata インスタンス。

戻り値の型:

Metadata

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.getDictionary('geonlp:ksj-station-N02')
{"@context": "https://schema.org/", "@type": "Dataset", "alternateName": "", "creator": [{"@type": "Organization", "name": "株式会社情報試作室", "sameAs": "https://www.info-proto.com/"}], "dateModified": "2021-08-27T17:18:18+09:00", "description": "国土数値情報「鉄道データ(N02)」から作成した、日本の鉄道駅(地下鉄を含む)の辞書です。hypernym には運営者名と路線名を記載しています。「都営」ではなく「東京都」のようになっていますので注意してください。自由フィールドとして、railway_classに「鉄道区分」、institution_typeに「事業者種別」を含みます。", "distribution": [{"@type": "DataDownload", "contentUrl": "https://www.info-proto.com/static/ksj-station-N02.csv", "encodingFormat": "text/csv"}], "identifier": ["geonlp:ksj-station-N02"], "isBasedOn": {"@type": "CreativeWork", "name": "国土数値情報 鉄道データ", "url": "https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N02-v2_2.html"}, "keywords": ["GeoNLP", "地名辞書"], "license": "https://creativecommons.org/licenses/by/4.0/", "name": "国土数値情報 鉄道データ(駅)", "size": "10191", "spatialCoverage": {"@type": "Place", "geo": {"@type": "GeoShape", "box": "26.193265 127.652285 45.41616333333333 145.59723"}}, "temporalCoverage": "../..", "url": "https://www.info-proto.com/static/ksj-station-N02.html"}
static get_package_files()

pip list コマンドを実行し、pygeonlp パッケージとしてインストールされた ファイルのフルパス一覧を取得します。

参考: https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program

init_capi_ma()
removeDictionary(identifier)

identifier で指定した辞書をデータベースから削除します。

パラメータ:

identifier (str) -- 辞書の identifier ("geonlp:xxxxx")。

戻り値:

常に True。

戻り値の型:

bool

例外:

RuntimeError -- 指定した辞書が登録されていない場合は RUntimeError が発生します。

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.removeDictionary('geonlp:geoshape-city')
True
>>> manager.updateIndex()
True
saveDictionaryFromWeb(jsonfile, csvfile, url, params=None, **kwargs)

指定した URL にあるページに含まれる辞書メタデータ(JSON-LD)を取得し、 メタデータに記載されている URL から地名解析辞書(CSVファイル)を取得し、 指定されたファイルに保存します。

パラメータ:
  • jsonfile (str) -- json-ld を保存するファイル名。

  • csvfile (str) -- CSV データを保存するファイル名。

  • url (str) -- 辞書メタデータを含むウェブページの URL。

  • params (dict, optional) -- requests.get に渡す params パラメータ。

  • **kwargs (dict, optional) -- requests.get に渡す kwargs パラメータ。

戻り値:

常に True。ダウンロードに失敗した場合は例外が発生します。

戻り値の型:

bool

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.saveDictionaryFromWeb('geoshape.json', 'geoshape.csv', 'https://geonlp.ex.nii.ac.jp/dictionary/geoshape-city/')
True
>>> import os
>>> os.remove('geoshape.json')
>>> os.remove('geoshape.csv')
setupBasicDatabase(src_dir=None)

基本的な地名解析辞書を登録したデータベースを作成します。

パラメータ:

src_dir (str, optional) -- 地名解析辞書ファイルが配置されているディレクトリ。 省略された場合には、 sys.prefix または site.USER_BASE の 下に pygeonlp_basedata がないか探します。 見つからない場合は RuntimeError を送出しますので、 ディレクトリを指定してください。

updateIndex()

辞書のインデックスを更新して検索可能にします。

サンプル

>>> from pygeonlp.api.dict_manager import DictManager
>>> manager = DictManager()
>>> manager.clearDatabase()
True
>>> manager.addDictionaryFromWeb('https://geonlp.ex.nii.ac.jp/dictionary/geoshape-city/')
True
>>> manager.updateIndex()
True