Last active
September 3, 2019 18:05
-
-
Save takemikami/dd672659399e67eb6902e74a3aae38f3 to your computer and use it in GitHub Desktop.
im@sparqlからアイドルの姓名を取り出し、ATOKに単語登録するためのスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # im@sparql to atok dic | |
| # im@sparqlからアイドルの姓名を取り出し、ATOKに単語登録するためのスクリプト | |
| # | |
| # 登録手順: | |
| # 1. pip install SPARQLWrapper | |
| # 2. python imasparql2atok.py | sort | uniq | nkf -w16 -Lw > imasparql-dic.txt | |
| # 3. atok -> 辞書ユーティリティー | |
| # 4. ツール -> ファイルから登録削除 | |
| # 5. 単語ファイルに、imasparql-dic.txtを選択して、登録ボタンを押す | |
| import json, re | |
| import pandas as pd | |
| from SPARQLWrapper import SPARQLWrapper, JSON | |
| def imasparql(query, endpoint='https://sparql.crssnky.xyz/spql/imas/query'): | |
| sparql = SPARQLWrapper(endpoint) | |
| sparql.setReturnFormat(JSON) | |
| sparql.setQuery(query) | |
| results = sparql.query().convert() | |
| lst = [] | |
| keys = results['head']['vars'] | |
| for x in results['results']['bindings']: | |
| lst.append(dict([(k, x[k]['value']) if k in x else (k, "") for k in keys])) | |
| df = pd.read_json(json.dumps(lst), orient='records') | |
| return df | |
| query = """ | |
| PREFIX schema: <http://schema.org/> | |
| PREFIX imas: <https://sparql.crssnky.xyz/imasrdf/URIs/imas-schema.ttl#> | |
| SELECT ?s ?fn ?gn ?nm ?fnk ?gnk ?nmk | |
| WHERE { | |
| ?s schema:familyName ?fn; schema:givenName ?gn; schema:name ?nm; | |
| imas:familyNameKana ?fnk; imas:givenNameKana ?gnk; imas:nameKana ?nmk. | |
| FILTER(LANG(?fn) = 'ja' && LANG(?gn) = 'ja' && LANG(?nm) = 'ja' | |
| && LANG(?fnk) = 'ja' && LANG(?gnk) = 'ja' && LANG(?nmk) = 'ja') | |
| } | |
| """ | |
| df = imasparql(query) | |
| for r, k in zip(df["fnk"].values, df["fn"].values): | |
| print("{}\t{}\t固有人姓".format(r, k)) | |
| for r, k in zip(df["gnk"].values, df["gn"].values): | |
| r = r.replace("ゔ", "ぶ") | |
| print("{}\t{}\t固有人名".format(r, k)) | |
| for r, k in zip(df["nmk"].values, df["nm"].values): | |
| r = r.replace("ゔ", "ぶ") | |
| print("{}\t{}\t固有人名".format(r, k)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment