Python
 Computer >> コンピューター >  >> プログラミング >> Python

PythonでNLTKWordNetから同義語/反意語を取得する方法


WordNetは、PythonのNaturalLanguageToolkitの一部です。これは、英語の名詞、形容詞、副詞、動詞の大規模な単語データベースです。これらは、シンセットと呼ばれる認知同義語のセットにグループ化されます。 。

Wordnetを使用するには、最初にNLTKモジュールをインストールしてから、WordNetパッケージをダウンロードする必要があります。

$ sudo pip3 install nltk
$ python3
>>> import nltk
>>>nltk.download('wordnet')

ワードネットには、意味が同じ単語のグループがいくつかあります。

最初の例では、wordnetが単語の意味やその他の詳細をどのように返すかを確認します。場合によっては、いくつかの例が利用できる場合は、それも提供されることがあります。

サンプルコード

from nltk.corpus import wordnet   #Import wordnet from the NLTK
synset = wordnet.synsets("Travel")
print('Word and Type : ' + synset[0].name())
print('Synonym of Travel is: ' + synset[0].lemmas()[0].name())
print('The meaning of the word : ' + synset[0].definition())
print('Example of Travel : ' + str(synset[0].examples()))

出力

$ python3 322a.word_info.py
Word and Type : travel.n.01
Synonym of Travel is: travel
The meaning of the word : the act of going from one place to another
Example of Travel : ['he enjoyed selling but he hated the travel']
$

前の例では、いくつかの単語に関する詳細情報を取得しています。ここでは、wordnetが特定の単語の同義語と反意語を送信する方法を説明します。

サンプルコード

import nltk
from nltk.corpus import wordnet   #Import wordnet from the NLTK
syn = list()
ant = list()
for synset in wordnet.synsets("Worse"):
   for lemma in synset.lemmas():
      syn.append(lemma.name())    #add the synonyms
      if lemma.antonyms():    #When antonyms are available, add them into the list
      ant.append(lemma.antonyms()[0].name())
print('Synonyms: ' + str(syn))
print('Antonyms: ' + str(ant))

出力

$ python3 322b.syn_ant.py
Synonyms: ['worse', 'worse', 'worse', 'worsened', 'bad', 'bad', 'big', 'bad', 'tough', 'bad', 'spoiled', 'spoilt', 'regretful', 'sorry', 'bad', 'bad', 'uncollectible', 'bad', 'bad', 'bad', 'risky', 'high-risk', 'speculative', 'bad', 'unfit', 'unsound', 'bad', 'bad', 'bad', 'forged', 'bad', 'defective', 'worse']
Antonyms: ['better', 'better', 'good', 'unregretful']
$

NLTKワードネットにはもう1つの優れた機能があり、それを使用することで、2つの単語がほぼ等しいかどうかを確認できます。単語のペアから類似度を返します。

サンプルコード

import nltk
from nltk.corpus import wordnet     #Import wordnet from the NLTK
first_word = wordnet.synset("Travel.v.01")
second_word = wordnet.synset("Walk.v.01")
print('Similarity: ' + str(first_word.wup_similarity(second_word)))
first_word = wordnet.synset("Good.n.01")
second_word = wordnet.synset("zebra.n.01")
print('Similarity: ' + str(first_word.wup_similarity(second_word)))

出力

$ python3 322c.compare.py
Similarity: 0.6666666666666666
Similarity: 0.09090909090909091
$

  1. Python Tkinterのチェックボックスから入力を取得するにはどうすればよいですか?

    チェックボックスウィジェットは、TrueまたはFalseの2つの値を持つ入力ウィジェットです。チェックボックスは、特定の値を検証する必要がある多くのアプリケーションで役立ちます。 チェックボックスから入力値を取得して、選択されている場合は選択された値を出力するとします。選択したチェックボックスの値を出力するには、 get()を使用できます。 方法。特定のウィジェットの入力値を返します。 例 # Import Tkinter library from tkinter import * # Create an instance of tkinter frame win = Tk() # Se

  2. Pythonでラベルからテキストを削除するにはどうすればよいですか?

    Tkinterは、GUIベースのアプリケーションの作成と開発に使用されるPythonライブラリです。この記事では、テキストが含まれるラベルからテキストを削除する方法を説明します。 ラベルからテキストを削除するために、ラベルのトリガーとして機能する関連ボタンを作成します。 例 #import Tkinter Library from tkinter import * #Create an instance of tkinter frame win= Tk() #Define the size and geometry of the frame win.geometry("700x