
OpenHownet APIは、Hownetで情報を検索したり、Sememeの木を表示したり、セメームを介して単語の類似性を計算したりする便利な方法を提供するThunlpによって開発されています。
OpenHownetが研究で提供しているデータまたはAPIを使用する場合は、次の論文を引用してください。
@article{qi2019openhownet,
title={OpenHowNet: An Open Sememe-based Lexical Knowledge Base},
author={Qi, Fanchao and Yang, Chenghao and Liu, Zhiyuan and Dong, Qiang and Sun, Maosong and Dong, Zhendong},
journal={arXiv preprint arXiv:1901.09957},
year={2019},
}
Hownetは、最も典型的なSememeの知識ベースです。 Sememeは言語学の最小セマンティックユニットとして定義されており、一部の言語学者は、あらゆる言語のすべての単語の意味は限られたセットのセットで表現できると考えています。 Zhendong Dong氏と息子のQiang Dongはこのアイデアを実践し、30年近くに費やしてHownetを構築しました。これは、約2,000のセメームを事前に定義し、200,000以上の英語と中国語の言葉に注釈を付けます。
Hownetが構築されて以来、さまざまなNLPタスクで広く利用されています。このペーパーリストを参照して、ハウネット関連のすべての研究を見てみましょう。
hownetコアデータファイル(すなわち、ここからダウンロードできるhownet辞書)は、中国語と英語の単語とフレーズで表される237,973の概念(または感覚)で構成されています。 Hownetの各概念には、Sememeベースの定義、POSタグ、センチメントの向き、文章の例などが注釈が付けられています。ここに、Hownetで概念が注釈される方法の例があります。
NO.=000000026417 # Concept ID
W_C=不惜 # Chinese word
G_C=verb # POS tag of the Chinese word
S_C=PlusFeeling|正面情感 # Sentiment orientation
E_C=~牺牲业余时间,~付出全部精力,~出卖自己的灵魂 # Example sentences of the Chinese word
W_E=do not hesitate to # English word
G_E=verb # POS tag of the English word
S_E=PlusFeeling|正面情感 # Sentiment orientation
E_E= # Example sentences of the English word
DEF={willing|愿意} # Sememe-based definition
RMK=
OpenHownet APIをインストールするには、次の2つの方法のいずれかを選択できます。
pip install OpenHowNetgit clone https://github.com/thunlp/OpenHowNet/
cd OpenHowNet
python setup.py install次のコードスニペットは、OpenHownet APIのいくつかの基本的な機能を示しています。このJupyterノートブックをダウンロードして、コードを実行することもできます。その他の機能と詳細情報については、ドキュメントをご覧ください。
import OpenHowNet
hownet_dict = OpenHowNet . HowNetDict () hownetデータをダウンロードしていない場合、エラーが発生します。この場合、最初にOpenHowNet.download()を実行する必要があります。
デフォルトでは、APIは、特定の単語(英語または中国語)で表されるすべての概念(感覚)をHownetを検索し、Senseクラスのインスタンスのリストを返します。検索時間を短縮するために言語を設定することもできます。 Hownetに与えられた単語が存在しない場合、このAPIは空のリストを返します。
> >> # Get all the senses represented by the word "苹果".
>> > result_list = hownet_dict . get_sense ( "苹果" )
> >> print ( "The number of retrievals: " , len ( result_list ))
The number of retrievals : 8
> >> print ( "An example of retrievals: " , result_list )
An example of retrievals : [ No . 244401 | apple |苹果, No . 244402 | malus pumila |苹果, No . 244403 | orchard apple tree |苹果, No . 244396 | apple |苹果, No . 244397 | apple |苹果, No . 244398 | IPHONE |苹果, No . 244399 | apple |苹果, No . 244400 | iphone |苹果]Sense InstanceでSenseの詳細情報を取得できます。
> >> sense_example = result_list [ 0 ]
> >> print ( "Sense example:" , sense_example )
Sense example : No . 244401 | apple |苹果
> >> print ( "Sense id: " , sense_example . No )
Sense id : 000000244401
> >> print ( "English word in the sense: " , sense_example . en_word )
English word in the sense : apple
> >> print ( "Chinese word in the sense: " , sense_example . zh_word )
Chinese word in the sense : 苹果
> >> print ( "HowNet Def of the sense: " , sense_example . Def )
HowNet Def of the sense : { tree |树:{ reproduce |生殖: PatientProduct = { fruit |水果}, agent = { ~ }}}
>> > print ( "Sememe list of the sense: " , sense_example . get_sememe_list ())
Sememe list of the sense : { fruit |水果, tree |树, reproduce |生殖}構造化された記号ベースの感覚の定義を視覚化できます(すなわち、「Sememe Tree」)
> >> sense_example . visualize_sememe_tree ()
[ sense ] No .244401 | apple |苹果
└── [ None ] tree |树
└── [ agent ] reproduce |生殖
└── [ PatientProduct ] fruit |水果パッケージは、Hownetですべての感覚、単語、およびセメームを取得するためのAPIを提供します。
> >> all_senses = hownet_dict . get_all_senses ()
> >> print ( "The number of all senses: {}" . format ( len ( all_senses )))
The number of all senses : 237974
> >> zh_word_list = hownet_dict . get_zh_words ()
> >> print ( "Chinese words in HowNet: " , zh_word_list [: 30 ])
Chinese words in HowNet : [ '' , '"' , '#' , '#号标签' , '$' , '$.J.' , '$A.' , '$NZ.' , '%' , "'" , '(' , ')' , '*' , '+' , ',' , '-' , '--' , '.' , '...' , '...为止' , '...也同样使然' , '...以上' , '...以内' , '...以来' , '...何如' , '...内' , '...出什么问题' , '...发生了什么' , '...发生故障' , '...家里有几口人' ]
> >> en_word_list = hownet_dict . get_en_words ()
> >> print ( "English words in HowNet: " , en_word_list [: 30 ])
English words in HowNet : [ 'A' , 'An' , 'Frenchmen' , 'Frenchwomen' , 'Ottomans' , 'a' , 'aardwolves' , 'abaci' , 'abandoned' , 'abbreviated' , 'abode' , 'aboideaux' , 'aboiteaux' , 'abscissae' , 'absorbed' , 'acanthi' , 'acari' , 'accepted' , 'acciaccature' , 'acclaimed' , 'accommodating' , 'accompanied' , 'accounting' , 'accused' , 'acetabula' , 'acetified' , 'aching' , 'acicula' , 'acini' , 'acquired' ]
> >> all_sememes = hownet_dict . get_all_sememes ()
> >> print ( 'There are {} sememes in HowNet' . format ( len ( all_sememes )))
There are 2540 sememes in HowNet 与えられた単語で表される感覚のSememeベースの定義を取得できます。デフォルトでは、パッケージは単語で表されるすべての感覚を取得し、Sememeリストを個別に返します。
> >> hownet_dict . get_sememes_by_word ( word = '苹果' , display = 'list' , merge = False , expanded_layer = - 1 , K = None )
[{ 'sense' : No . 244396 | apple |苹果,
'sememes' : { PatternValue |样式值, SpeBrand |特定牌子, able |能, bring |携带, computer |电脑}},
{ 'sense' : No . 244397 | apple |苹果,
'sememes' : { fruit |水果}},
{ 'sense' : No . 244398 | IPHONE |苹果,
'sememes' : { PatternValue |样式值, SpeBrand |特定牌子, able |能, bring |携带, communicate |交流, tool |用具}},
{ 'sense' : No . 244399 | apple |苹果,
'sememes' : { PatternValue |样式值, SpeBrand |特定牌子, able |能, bring |携带, communicate |交流, tool |用具}},
{ 'sense' : No . 244400 | iphone |苹果,
'sememes' : { PatternValue |样式值, SpeBrand |特定牌子, able |能, bring |携带, communicate |交流, tool |用具}},
{ 'sense' : No . 244401 | apple |苹果,
'sememes' : { fruit |水果, reproduce |生殖, tree |树}},
{ 'sense' : No . 244402 | malus pumila |苹果,
'sememes' : { fruit |水果, reproduce |生殖, tree |树}},
{ 'sense' : No . 244403 | orchard apple tree |苹果,
'sememes' : { fruit |水果, reproduce |生殖, tree |树}}] displayを変更することにより、センスのセメームをリスト形式( list )、辞書フォーム( dict )、ツリーノードフォーム( tree )、視覚化フォーム( visual )で表示できます。
# Get the sememes in the form of dictionary
> >> hownet_dict . get_sememes_by_word ( word = '苹果' , display = 'dict' )[ 0 ]
{ 'sense' : No . 244396 | apple |苹果, 'sememes' : { 'role' : 'sense' , 'name' : No . 244396 | apple |苹果, 'children' : [{ 'role' : 'None' , 'name' : computer |电脑, 'children' : [{ 'role' : 'modifier' , 'name' : PatternValue |样式值, 'children' : [{ 'role' : 'CoEvent' , 'name' : able |能, 'children' : [{ 'role' : 'scope' , 'name' : bring |携带, 'children' : [{ 'role' : 'patient' , 'name' : '$' }]}]}]}, { 'role' : 'patient' , 'name' : SpeBrand |特定牌子}]}]}}
# Get the sememes in the form of tree node (get the root node of the sememe tree)
> >> d . get_sememes_by_word ( word = '苹果' , display = 'tree' )[ 0 ]
{ 'sense' : No . 244396 | apple |苹果, 'sememes' : Node ( '/No.244396|apple|苹果' , role = 'sense' )}
# Visualize the sememes (Set K to control the num of visualized tree to print)
> >> d . get_sememes_by_word ( word = '苹果' , display = 'visual' , K = 2 )
Find 8 result ( s )
Display #0 sememe tree
[ sense ] No .244396 | apple |苹果
└── [ None ] computer |电脑
├── [ modifier ] PatternValue |样式值
│ └── [ CoEvent ] able |能
│ └── [ scope ] bring |携带
│ └── [ patient ]$
└── [ patient ] SpeBrand |特定牌子
Display #1 sememe tree
[ sense ] No .244397 | apple |苹果
└── [ None ] fruit |水果さらに、 display=='list'の場合、すべてのSememeリストを1つにマージし、パラメーターexpanded_layerを変更してSememe Treesの展開層を制限することを選択できます(-1はすべてのレイヤーを拡張することを意味します)。
> >> hownet_dict . get_sememes_by_word ( word = '苹果' , display = 'list' , merge = True , expanded_layer = - 1 , K = None )
{ PatternValue |样式值, SpeBrand |特定牌子, able |能, bring |携带, communicate |交流, computer |电脑, fruit |水果,
reproduce |生殖, tool |用具, tree |树}セメームを表す単語(英語または中国語)を入力することにより、2つのセメーム間の関係を取得できます。 (sememe1、relation、sememe2)のトリプレットを表示することを選択できます。
> >> relations = hownet_dict . get_sememe_relation ( 'FormValue' , '圆' , return_triples = False )
> >> print ( relations )
'hyponym'
> >> triples = hownet_dict . get_sememe_relation ( 'FormValue' , '圆' , return_triples = True )
> >> print ( triples )
[( FormValue |形状值, 'hyponym' , round |圆)]Sememeと特定の関係を持つすべてのセメームを検索できます。同様に、Sememeは単語(英語または中国語)で表されるべきですが、関係は小文字英語でなければなりません。
> >> triples = hownet_dict . get_related_sememes ( 'FormValue' , relation = 'hyponym' , return_triples = True )
> >> print ( triples )
[( FormValue |形状值, 'hyponym' , round |圆), ( FormValue |形状值, 'hyponym' , unformed |不成形), ( AppearanceValue |外观值, 'hyponym' , FormValue |形状值), ( FormValue |形状值, 'hyponym' , angular |角), ( FormValue |形状值, 'hyponym' , square |方), ( FormValue |形状值, 'hyponym' , netlike |网), ( FormValue |形状值, 'hyponym' , formed |成形)]実装は論文に基づいています。
Jiangming Liu、Jinan Xu、Yujie Zhang。 Hownetによる単語類似性コンピューティングのためのハイブリッド階層構造のアプローチ。 IJCNLP 2013の議事録。[PDF]
類似の計算にロードする必要があるファイルがいくつかあるため、初期化オーバーヘッドは以前よりも大きくなります。
そもそも、次のようにhownet_dictオブジェクトを初期化できます。
> >> hownet_dict_advanced = OpenHowNet . HowNetDict ( init_sim = True )
Initializing OpenHowNet succeeded !
Initializing similarity calculation succeeded !また、使用するまで類似性計算の初期化を延期することもできます。
> >> hownet_dict . initialize_similarity_calculation ()
Initializing similarity calculation succeeded !感覚を持つ同じSememeベースの定義を持つ感覚を得ることができます。
> >> s = hownet_dict_advanced . get_sense ( '苹果' )[ 0 ]
> >> hownet_dict_advanced . get_sense_synonyns ( s )[: 10 ]
[ No . 110999 | pear |山梨, No . 111007 | hawthorn |山楂, No . 111009 | haw |山楂树, No . 111010 | hawthorn |山楂树, No . 111268 | Chinese hawthorn |山里红, No . 122955 | Pistacia vera |开心果树, No . 122956 | pistachio |开心果树, No . 122957 | pistachio tree |开心果树, No . 135467 | almond tree |扁桃, No . 154699 | fig |无花果]特定の単語で表され、最も近いトップKの感覚を取得し、対応する単語を返す感覚のパッケージ検索。指定された単語の言語を設定する必要があることに注意してください。
また、単語のPOSを設定し、類似性を出力することを選択し、Difference Sensesに属するすべての単語を単一のリストなどにマージすることもできます。詳細については、ドキュメントを参照してください。
入力単語がhownetにない場合、APIは空のリストを返します。
> >> hownet_dict_advanced . get_nearest_words ( '苹果' , language = 'zh' , K = 5 )
{ No . 244396 | apple |苹果: [ 'IBM' , '东芝' , '华为' , '戴尔' , '索尼' ],
No . 244397 | apple |苹果: [ '丑橘' , '乌梅' , '五敛子' , '凤梨' , '刺梨' ],
No . 244398 | IPHONE |苹果: [ 'OPPO' , '华为' , '苹果' , '智能手机' , '彩笔' ],
No . 244399 | apple |苹果: [ 'OPPO' , '华为' , '苹果' , '智能手机' , '彩笔' ],
No . 244400 | iphone |苹果: [ 'OPPO' , '华为' , '苹果' , '智能手机' , '彩笔' ],
No . 244401 | apple |苹果: [ '山梨' , '山楂' , '山楂树' , '山里红' , '开心果树' ],
No . 244402 | malus pumila |苹果: [ '山梨' , '山楂' , '山楂树' , '山里红' , '开心果树' ],
No . 244403 | orchard apple tree |苹果: [ '山梨' , '山楂' , '山楂树' , '山里红' , '开心果树' ]}
> >> hownet_dict_advanced . get_nearest_words ( '苹果' , language = 'zh' , K = 5 , merge = True )
[ 'IBM' , '东芝' , '华为' , '戴尔' , '索尼' ]hownetに与えられた2つの単語のいずれかが存在しない場合、それは-1を返します。
> >> print ( 'The similarity of 苹果 and 梨 is {}.' . format ( hownet_dict_advanced . calculate_word_similarity ( '苹果' , '梨' )))
The similarity of 苹果 and 梨 is 1.0 .このパッケージは、babelnet(babelnet synset)のシンセットの情報のクエリ関数を統合します。 Babelnetは、Babelnet Synsetsで構成される多言語百科事典辞書で、それぞれに同じ意味を持つ多言語の同義語が含まれています。以下の作業は、いくつかのバベルネットシンセットのセメームに注釈を付け、この部分の関数はその注釈結果に基づいています。
多言語Sememeナレッジベースの構築に向けて:Babelnetシンセットのセメームの予測。 Fanchao Qi、Liang Chang、Maosong Sun、Sicong Ouyang、Zhiyuan Liu 。 aaai-20。 [PDF] [コード]
そもそも、BabelNet Synset Dictionaryを初期化する必要があります。
> >> hownet_dict . initialize_babelnet_dict ()
Initializing BabelNet synset Dict succeeded !
# Or you can initialize when create the HowNetDict instance
>> > hownet_dict_advanced = HowNetDict ( init_babel = True )
Initializing OpenHowNet succeeded !
Initializing BabelNet synset Dict succeeded !次のAPIを使用すると、BabelNet Synset(中国語と英語の同義語、定義、画像URLなど)の豊富な情報を照会できます。
> >> syn_list = hownet_dict_advanced . get_synset ( '黄色' )
> >> print ( "{} results are retrieved and take the first one as an example" . format ( len ( syn_list )))
3 results are retrieved and take the first one as an example
>> > syn_example = syn_list [ 0 ]
> >> print ( "Synset: {}" . format ( syn_example ))
Synset : bn : 00113968 a | yellow |黄
> >> print ( "English synonyms: {}" . format ( syn_example . en_synonyms ))
English synonyms : [ 'yellow' , 'yellowish' , 'xanthous' ]
> >> print ( "Chinese synonyms: {}" . format ( syn_example . zh_synonyms ))
Chinese synonyms : [ '黄' , '黄色' , '淡黄色+的' , '黄色+的' , '微黄色' , '微黄色+的' , '黄+的' , '淡黄色' ]
> >> print ( "English glosses: {}" . format ( syn_example . en_glosses ))
English glosses : [ 'Of the color intermediate between green and orange in the color spectrum; of something resembling the color of an egg yolk' , 'Having the colour of a yolk, a lemon or gold.' ]
> >> print ( "Chinese glosses: {}" . format ( syn_example . zh_glosses ))
Chinese glosses : [ '像丝瓜花或向日葵花的颜色。' ]関連するSynsetを使用して、関連するBabelnetシンセットを取得できます。
> >> related_synsets = syn_example . get_related_synsets ()
> >> print ( "There are {} synsets that have relation with the {}, they are: " . format ( len ( related_synsets ), syn_example ))
There are 6 synsets that have relation with the bn : 00113968 a | yellow |黄, they are :
>> > print ( related_synsets )
[ bn : 00099663 a | chromatic |彩色, bn : 00029925 n | egg_yolk |蛋黄, bn : 00092876 v | resemble |相似, bn : 00020726 n | color |颜色, bn : 00020748 n | visible_spectrum |可见光, bn : 00081866 n | yellow |黄色]BabelNetシンセットに単語を入力することにより、BabelNetシンセットのセメームを取得できます。
> >> print ( hownet_dict_advanced . get_sememes_by_word_in_BabelNet ( '黄色' ))
[{ 'synset' : bn : 00113968 a | yellow |黄, 'sememes' : [ yellow |黄]}, { 'synset' : bn : 00101430 a | dirty |淫秽的, 'sememes' : [ lascivious |淫, dirty |龊, despicable |卑劣, BadSocial |坏风气]}, { 'synset' : bn : 00081866 n | yellow |黄色, 'sememes' : [ yellow |黄]}]
> >> print ( hownet_dict_advanced . get_sememes_by_word_in_BabelNet ( '黄色' , merge = True ))
[ lascivious |淫, despicable |卑劣, BadSocial |坏风气, dirty |龊, yellow |黄]詳細については、ドキュメントを参照してください。
コードまたはデータが役立つ場合は、次の論文を引用してください。
@article{qi2019openhownet,
title={Openhownet: An open sememe-based lexical knowledge base},
author={Qi, Fanchao and Yang, Chenghao and Liu, Zhiyuan and Dong, Qiang and Sun, Maosong and Dong, Zhendong},
journal={arXiv preprint arXiv:1901.09957},
year={2019}
}