2016-08-17 4 views
3

Natural Language Toolkit(3.2.1)およびWordNetと共にPython(2.7)を使用しています。私は非常にプログラミングです。ユーザー入力文字列を正しいオブジェクト型に変換する方法

ユーザーに単語を尋ね、その単語の同義語セットを出力し、次にどの補語セットを表示するかを同義語セットに照会するプログラムを作成しようとしています。

raw_inputは文字列しか受け付けないので、ユーザー入力にメソッド.lemma_names()を使用しようとすると、エラーAttributeError: 'str' object has no attribute 'lemma_names'が返されます。ここで

はコードです:

from nltk.corpus import wordnet as wn 

w1 = raw_input ("What is the word? ") 

#This prints the synsets for w1, thus showing them what format to use in the next question. 

for synset in wn.synsets(w1): 
    print synset 

#This asks the user to choose the synset of w1 that interests them. 

synset1 = raw_input ("Which sense are you looking for? [Use same format as above]") 

#This prints the lemmas from the synset of interest. 

for x in synset1.lemma_names(): 
    print x 

私の質問はどのように私は私が上.lemma_names()メソッドを使用することができsynset型に文字列からユーザーの入力を変換します、ありますか?

この質問が話題にならないほど基本的であれば、私はお詫び申し上げます。もしそうなら、私に教えてください。

+0

あなたが任意のより多くのコードを記述する前に、[PyCharm](https://www.jetbrains.com/pycharm/download/)をダウンロード。そのデバッガを使用します。 –

答えて

1

はこれを試してみてください:

from nltk.corpus import wordnet as wn 

w1 = raw_input ("What is the word? ") 

synset_dict = dict() 
for synset in wn.synsets(w1): 
    name = synset.name() 
    synset_dict[name] = synset 
    print name 

synset1 = raw_input ("Which sense are you looking for? [Use same format as above] ") 

if synset1 in synset_dict: 
    synset = synset_dict[synset1] 
    for lemma in synset.lemma_names(): 
     print lemma 
関連する問題