本文整理汇总了Python中Stemmer.Stemmer.DICT方法的典型用法代码示例。如果您正苦于以下问题:Python Stemmer.DICT方法的具体用法?Python Stemmer.DICT怎么用?Python Stemmer.DICT使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stemmer.Stemmer
的用法示例。
在下文中一共展示了Stemmer.DICT方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lmScoring
# 需要导入模块: from Stemmer import Stemmer [as 别名]
# 或者: from Stemmer.Stemmer import DICT [as 别名]
def lmScoring( sentence ):
# candidates is the list of candiate sentences formed by trying
# all possible definitions of all words with >1 translation
stemmer = Stemmer()
stemmer.DICT = dict
candidates = []
tokens = asTokens( sentence )
for i in range( len(tokens) ):
word = tokens[i]
if word.lower() in dict:
translations = dict[word.lower()]
pos = POSTAG[word.lower()]
# print 'word:',word,', pos:',pos,', dictionary:',translations
if pos == 'V':
try:
stemmer_translations = stemmer.input([word.lower()])
# print 'stemmer returned: ',stemmer_translations
if stemmer_translations:
translations = [stemmer_translations]
except:
pass
# print 'stemmer threw exception on: ', word.lower()
old_candidates = candidates[:]
candidates = []
# print 'old_candidates:', old_candidates
k = len(translations)
if k > 1:
# for idx in range(len(candidates)):
# for t in range(len(translations)):
if len(old_candidates) == 0:
for k in range(len(translations)):
candidates.append( [translations[k]] )
else:
for k in range(len(translations)):
for c in old_candidates:
# print 'c in old_candidates:',c
cnew = c + [translations[k]]
# print cnew
candidates.append( cnew )
else:
# append the current word to all candidate
# sentences
if len(old_candidates) == 0:
candidates.append( [translations[0]] )
else:
for c in old_candidates:
# print 'c in old_candidates:',c
cnew = c + [translations[0]]
# print cnew
candidates.append( cnew )
# print [c.extend(translations[0]) for c in old_candidates]
# candidates.extend( [c.extend(translations[0]) for c in old_candidates] )
else:
# print 'CANDIDATES (',len(candidates),')'
# print candidates
# print word, "NOT IN DICTIONARY"
# words not in dictionary pass through untranslated
translations = [word]
old_candidates = candidates[:]
candidates = []
if len(old_candidates) == 0:
candidates.append( [translations[0]] )
else:
for c in old_candidates:
cnew = c + [translations[0]]
candidates.append( cnew )
# print 'CANDIDATES (',len(candidates),')'
# print candidates
neglobprob = [lm.sentenceProbability( ' '.join(cs) ) for cs in candidates ]
# print neglobprob
bestSentence = candidates[ neglobprob.index( min(neglobprob) ) ]
# print 'CANDIDATES (',len(candidates),')'
# for c in candidates:
# print ' '.join(c)
# print 'bestSentence='
# print ' '.join(bestSentence)
return ' '.join(bestSentence)