Python-text-processing-python-synonyms-and-antonyms

提供:Dev Guides
移動先:案内検索

Python-同義語と反意語

同義語と反意語は、英語の語彙データベースであるワードネットの一部として利用できます。 nltkコーパスアクセスの一部として利用できます。 ワードネットでは、シノニムは同じ概念を示す単語であり、多くのコンテキストで交換可能であるため、順序付けられていないセット(シンセット)にグループ化されます。 以下のプログラムに示すように、これらのシンセットを使用して同義語と反意語を導き出します。

from nltk.corpus import wordnet

synonyms = []

for syn in wordnet.synsets("Soil"):
    for lm in syn.lemmas():
             synonyms.append(lm.name())
print (set(synonyms))

上記のプログラムを実行すると、次の出力が得られます-

set([grease', filth', dirt', begrime', soil',
grime', land', bemire', dirty', grunge',
stain', territory', colly', ground'])

反意語を取得するには、反意語関数を使用します。

from nltk.corpus import wordnet
antonyms = []

for syn in wordnet.synsets("ahead"):
    for lm in syn.lemmas():
        if lm.antonyms():
            antonyms.append(lm.antonyms()[0].name())

print(set(antonyms))

上記のプログラムを実行すると、次の出力が得られます-

set([backward', back'])