direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back']
class Lexicon(object):
def scan(self, sentence):
self.sentence = sentence
self.words = self.sentence.split()
self.term = []
for word in self.words:
if word in direction:
part = ('direction','%s' % word)
self.term.append(word)
return self.term
lexicon = Lexicon()
當我通過lexicon.scan('north south east')
我期待回報給我[('direction','north'),('direction','south'),('direction','east')]
。相反,我得到['north']
。這是我希望程序在整體上做的事情。爲什麼我的代碼不能正確創建元組列表?
- 拿一句話。
- 使用對該句子的掃描並將句子分成不同的單詞。
- 掃描檢查了幾個列表中的所有單詞(這只是單個列表中的第一個測試)。
- 如果在一個列表中找到一個單詞,那麼我想創建一個元組,其中第一個單詞是列表的名稱,第二個單詞是單詞。
- 我想爲不在列表中的單詞創建一個元組,就像之前一樣,但使用「錯誤」而不是列表名稱。
- 我想在元組
FWIW,我會在那裏放置一個'方向=設置(方向)'的效率。 – mgilson
你確定句子,單詞和術語需要是實例變量嗎?如果不需要存儲,最好使用局部變量。 –