2014-02-25 35 views
0

我想写代码,需要一个代词,主体(名词),和动词的三个措词句妥协。例如:我讨厌你。然后它会识别每个单词(它是一个名词,动词还是代词)并记住名词和动词。然后它会以非常明显的方式回复这个句子,例如: 输入:我讨厌苹果。 回复:你为什么讨厌苹果? 输入:我玩运动。 回复:你为什么要玩体育?如何解构和回复3条款的句子

很简单的东西,但我有麻烦拆分句子中的每个单词,然后保存每个单词,他们是哪种类型。以后我会处理专有名词,现在我正在处理一个约2000个名词的简单列表。

from nouns import nouns_list 
from adjectives import adjectives_list 
from verbs import verbs_list 
from adverbs import adverbs_list 
from pronouns import pronouns_list 



test = raw_input("Please input a fruit: ") 

words = test.split() 
    #numbers = map(int, test.split()) 
    #use later! 


def word_identify(words): 
    for word in words: 
     if word in nouns_list: 
      print words + " is a noun!" 
      word == noun 
     elif words in verbs_list: 
      print words + " is a verb!" 
      word == verb 
     elif words in pronouns_list: 
      print words + " is a pronoun!" 
      word == pronoun 
     elif words in adjectives_list: 
      print words + " is an adjective!" 
      word == adjective 
     elif words in adverbs_list: 
      print word + " is a adverb!" 
      word == adverb 
     elif words == 'i' or words == "I": 
      print "This is the I pronoun!" 
      word == 'you' 
     else: 
      print "Word " + words + " not identified!" 
      return -1 
     return 1 

while word_identify(test) > 0: 
    test = raw_input("Please input a fruit: ") 

最后,我只是打印一个简单的打印“为什么”+动词+名词+'?' 基本上我问我如何正确使用.split()函数,我如何记住哪个特定的单词被触发?它会以不恰当的时态回复一个动词的事实对我来说很好,我会在稍后处理! 非常感谢 Braam在

编辑:忽略numbers = map(int, test.split())

回答

0

当您使用==运营商,你是比较两个对象。因此,根据对象是否相等,word == adverb返回TrueFalse。相反,试试这个:

def word_identify(words): 
    parts_of_speech = [] 
    for word in words: 
     if word in nouns_list: 
      print word + " is a noun!" 
      part_of_speech = 'noun' 
     elif words in verbs_list: 
      print word + " is a verb!" 
      part_of_speech = 'verb' 
     ... 
     else: 
      print "Word " + word + " not identified!" 
      part_of_speech = None 
     parts_of_speech.append(part_of_speech) 
    return parts_of_speech 

然后,word_identify()将返回类似下面的列表:

['pronoun', 'verb', 'noun'] 

如果它不能识别一个单词:

['pronoun', None, 'noun'] 
+0

这非常有帮助!唯一的问题是,现在它不输出任何东西!我查看了代码,我不知道为什么,因为打印功能确实存在。 – user146929

+0

您必须发布一些代码,否则我们看不到问题。 – tsroten

+0

如果您的问题已解决,请选择一个标记为正确的答案。 – tsroten

0

检查:

d = {'noun':nouns_list, 
    'pronoun':pronouns_list, 
    'adjective':adjectives_list, 
    'verb':verbs_list, 
    'adverb':adverbs_list 
} 

def part_of_speech(word): 
    for pos,l in d.items(): 
     if word in l: 
      return pos 
    return None 

def get_parts_of_speech(sentence): 
    return [part_of_speech(word.lower()) for word in sentence.split()] 

sentence = raw_input("Enter a three word sentence [<noun> <verb> <pronoun>]:"); 

print "The parts are:", get_parts_of_speech(sentence)