1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Swallow-MS-7b-v0.1でのFew-Shot Promptingによる日本語品詞付与

Last updated at Posted at 2024-03-31

Terra Blevins, Hila Gonen, Luke Zettlemoyer『Prompting Language Models for Linguistic Structure』の手法をSwallow-MS-7b-v0.1に適用して、国語研短単位でのUPOS (Universal Part-Of-Speech)品詞付与に挑戦してみた。Few-Shot Promptingの例文は、UD_Japanese-GSDの訓練データのうち、固有名詞を含んでいない短めの文で、Swallow-MS-7b-v0.1によるトークナイズが国語研短単位と矛盾しないものを5つ選んだ。

>>> class TextUPOSList(list):
...   __str__=lambda self:"\n".join("###text:"+"".join(t for t,u in s)+"\n###UPOS:"+"|".join(t+"_"+u for t,u in s) for s in self)+"\n"
...
>>> ex=TextUPOSList()
>>> ex.append([("一","NUM"),("直線","NOUN"),("に","ADP"),("伸びる","VERB"),("電撃","NOUN"),("を","ADP"),("放ち","VERB"),("、","PUNCT"),("電撃","NOUN"),("ダメージ","NOUN"),("を","ADP"),("与える","VERB"),("。","PUNCT")])
>>> ex.append([("色々","ADV"),("と","ADP"),("面白い","ADJ"),("メニュー","NOUN"),("の","ADP"),("ある","VERB"),("店","NOUN"),("。","PUNCT")])
>>> ex.append([("しかも","CCONJ"),("、","PUNCT"),("ここ","PRON"),("は","ADP"),("コース","NOUN"),("が","ADP"),("リーズナブル","ADJ"),("な","AUX"),("の","SCONJ"),("です","AUX"),("。","PUNCT")])
>>> ex.append([("彼","PRON"),("は","ADP"),("コンピュータ","NOUN"),("を","ADP"),("個人","NOUN"),("の","ADP"),("持ち物","NOUN"),("に","ADP"),("し","VERB"),("まし","AUX"),("た","AUX"),("。","PUNCT")])
>>> ex.append([("2007","NUM"),("年","NOUN"),("9","NUM"),("月","NOUN"),("現在","ADV"),("、","PUNCT"),("以下","NOUN"),("の","ADP"),("メーカー","NOUN"),("から","ADP"),("対応","NOUN"),("製品","NOUN"),("が","ADP"),("発売","VERB"),("さ","AUX"),("れ","AUX"),("て","SCONJ"),("いる","VERB"),("。","PUNCT")])
>>> from transformers import pipeline
>>> tgn=pipeline("text-generation","tokyotech-llm/Swallow-MS-7b-v0.1",max_new_tokens=128)
>>> print("\n".join(tgn(str(ex)+"###text:国境の長いトンネルを抜けると雪国であった。\n###UPOS:")[0]["generated_text"].split("\n")[len(ex)*2:len(ex)*2+2]))
###text:国境の長いトンネルを抜けると雪国であった。
###UPOS:国境_NOUN|の_ADP|長い_ADJ|トンネル_NOUN|を_ADP|抜ける_VERB|と_ADP|雪国_NOUN|で_ADP|あっ_VERB|た_AUX|。_PUNCT

『雪国』の冒頭文に品詞付与させてみたところ、私(安岡孝一)の手元では「で」「あっ」の品詞が間違っている(2つともAUXが正解)ものの、他はちゃんと品詞付与できた。もう一度やってみよう。

>>> print("\n".join(tgn(str(ex)+"###text:国境の長いトンネルを抜けると雪国であった。\n###UPOS:")[0]["generated_text"].split("\n")[len(ex)*2:len(ex)*2+2]))
###text:国境の長いトンネルを抜けると雪国であった。
###UPOS:国境_NOUN|の_ADP|長い_ADJ|トンネル_NOUN|を_ADP|抜ける_VERB|と_ADP|雪国_NOUN|で_ADP|あっ_VERB|た_AUX|。_PUNCT

どうやら同じ結果だ。ならば「予想に反して品詞付与の精度がイマイチ」という文はどうだろう。

>>> print("\n".join(tgn(str(ex)+"###text:予想に反して品詞付与の精度がイマイチ\n###UPOS:")[0]["generated_text"].split("\n")[len(ex)*2:len(ex)*2+2]))
###text:予想に反して品詞付与の精度がイマイチ
###UPOS:予想_NOUN|に_ADP|反_ADV|して_ADP|品詞_NOUN|付与_NOUN|の_ADP|精度_NOUN|が_ADP|イマイチ_ADJ|。_PUNCT

やはり「反し_VERB|て_SCONJ」が正しくトークナイズできない上に、文末に勝手に「。_PUNCT」が追加されている。一昨々日の記事の手法よりは精度が高い気もするが、それでもトークナイザの問題は残っているし、単語を勝手に追加する可能性もあるということだ。さて、どうしたらいいかな。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?