パスワードを忘れた? アカウント作成
15418219 journal
人工知能

yasuokaの日記: bert-base-japanese-uposとTokenClassificationPipelineでおこなう日本語形態素解析

日記 by yasuoka

昨日の日記の手法を使って、bert-base-japanese-uposのトークナイザもBertTokenizerFastに入れ換えてみたところ、TokenClassificationPipelineの結果が改善された。Transformersを最新版に更新しつつ、ちょっとやってみよう。

$ pip3 install -U transformers
$ python3
>>> from transformers import AutoModelForTokenClassification,AutoTokenizer,TokenClassificationPipeline
>>> brt="KoichiYasuoka/bert-base-japanese-upos"
>>> mdl=AutoModelForTokenClassification.from_pretrained(brt)
>>> tkz=AutoTokenizer.from_pretrained(brt)
>>> nlp=TokenClassificationPipeline(model=mdl,tokenizer=tkz,aggregation_strategy="simple")
>>> d=nlp(inputs="國境の長いトンネルを拔けると雪國であつた。難儀な難儀は難儀する。")
>>> print(d)
[{'entity_group': 'NOUN', 'score': 0.99924845, 'word': '國境', 'start': 0, 'end': 2}, {'entity_group': 'ADP', 'score': 0.9997904, 'word': 'の', 'start': 2, 'end': 3}, {'entity_group': 'ADJ', 'score': 0.995852, 'word': '長い', 'start': 3, 'end': 5}, {'entity_group': 'NOUN', 'score': 0.9995555, 'word': 'トンネル', 'start': 5, 'end': 9}, {'entity_group': 'ADP', 'score': 0.9998084, 'word': 'を', 'start': 9, 'end': 10}, {'entity_group': 'VERB', 'score': 0.99934673, 'word': '拔ける', 'start': 10, 'end': 13}, {'entity_group': 'CCONJ', 'score': 0.9946812, 'word': 'と', 'start': 13, 'end': 14}, {'entity_group': 'NOUN', 'score': 0.9560932, 'word': '雪國', 'start': 14, 'end': 16}, {'entity_group': 'AUX', 'score': 0.9979013, 'word': 'で', 'start': 16, 'end': 17}, {'entity_group': 'AUX', 'score': 0.9907406, 'word': 'あつた', 'start': 17, 'end': 20}, {'entity_group': 'PUNCT', 'score': 0.9998325, 'word': '。', 'start': 20, 'end': 21}, {'entity_group': 'ADJ', 'score': 0.9873648, 'word': '難儀', 'start': 21, 'end': 23}, {'entity_group': 'AUX', 'score': 0.99952304, 'word': 'な', 'start': 23, 'end': 24}, {'entity_group': 'NOUN', 'score': 0.99909914, 'word': '難儀', 'start': 24, 'end': 26}, {'entity_group': 'ADP', 'score': 0.99976534, 'word': 'は', 'start': 26, 'end': 27}, {'entity_group': 'VERB', 'score': 0.99604756, 'word': '難儀', 'start': 27, 'end': 29}, {'entity_group': 'AUX', 'score': 0.99927384, 'word': 'する', 'start': 29, 'end': 31}, {'entity_group': 'PUNCT', 'score': 0.99983144, 'word': '。', 'start': 31, 'end': 32}]

さすがに読みにくいので、単語(word)と品詞(entity_group)を抜き出してみよう。

>>> print([(t["word"],t["entity_group"]) for t in d])
[('國境', 'NOUN'), ('の', 'ADP'), ('長い', 'ADJ'), ('トンネル', 'NOUN'), ('を', 'ADP'), ('拔ける', 'VERB'), ('と', 'CCONJ'), ('雪國', 'NOUN'), ('で', 'AUX'), ('あつた', 'AUX'), ('。', 'PUNCT'), ('難儀', 'ADJ'), ('な', 'AUX'), ('難儀', 'NOUN'), ('は', 'ADP'), ('難儀', 'VERB'), ('する', 'AUX'), ('。', 'PUNCT')]

なかなかいい感じだ。ただ、私(安岡孝一)個人としては、ちゃんとstartとendが出るようになったのが、かなりうれしかったりする。入力文字列との対応関係を取れると、便利なことが多いのだ。まあ、このあたりは目的にもよるのだが、ぜひ使ってみてほしい。

この議論は、yasuoka (21275)によって ログインユーザだけとして作成されたが、今となっては 新たにコメントを付けることはできません。
typodupeerror

アレゲはアレゲ以上のなにものでもなさげ -- アレゲ研究家

読み込み中...