Debugging 在python中调试maxmatch算法时遇到问题
与nltk中的单词列表相比,我一直在研究一种maxmatch算法来标记hashtags,但我在调试方面遇到了麻烦 算法要点如下:Debugging 在python中调试maxmatch算法时遇到问题,debugging,Debugging,与nltk中的单词列表相比,我一直在研究一种maxmatch算法来标记hashtags,但我在调试方面遇到了麻烦 算法要点如下: function MAXMATCH (sentence, dictionary D) returns word sequence W if sentence is empty return empty list for i ← length(sentence) downto 1 firstword = first i chars of sent
function MAXMATCH (sentence, dictionary D) returns word sequence W
if sentence is empty
return empty list
for i ← length(sentence) downto 1
firstword = first i chars of sentence
remainder = rest of sentence
if InDictionary(firstword, D)
return list(firstword, MaxMatch(remainder,dictionary) )
# no word was found, so make a one-character word
firstword = first char of sentence
remainder = rest of sentence
return list(firstword, MaxMatch(remainder,dictionary) )
下面是我的python实现。
我在这里和那里插入了一些试图调试的print
from nltk.corpus import words # words is a Python list
wordlist = set(words.words())
lst = []
def max_match(hashtag, wordlist):
if not hashtag:
return None
for i in range(len(hashtag)-1, -1, -1):
first_word = (hashtag[0:i+1])
print "Firstword: " + first_word
remainder = hashtag[i+1:len(hashtag)]
print "Remainder: " + remainder
if first_word in wordlist:
print "Found: " + first_word
lst.append(first_word)
print lst
max_match(remainder, wordlist)
# if no word is found, make one-character word
first_word = hashtag[0]
remainder = hashtag[1:len(hashtag)]
lst.append(first_word)
max_match(remainder, wordlist)
return lst
print max_match('labourvictory', wordlist)
最后一行,print max_match('labourvictory',wordlist)
应该返回list['labour',victory',我希望它退出,因为如果不是hashtag,则返回None
部分,然而,由于我不明白它为什么会继续运行,一切都会失控
在递归函数中,最常见的错误是没有在正确的点返回值。我根据给定的伪代码要点修改了您的代码。代码中的问题是,当您在字典中找到一个单词时,您没有返回任何值
def max_match(hashtag, wordlist):
if not hashtag:
return []
for i in range(len(hashtag)-1, -1, -1):
first_word = (hashtag[0:i+1])
remainder = hashtag[i+1:len(hashtag)]
if first_word in wordlist:
return [first_word] + max_match(remainder, wordlist)
# if no word is found, make one-character word
first_word = hashtag[0]
remainder = hashtag[1:len(hashtag)]
return [first_word] + max_match(remainder, wordlist)