本文整理匯總了Python中nltk.stem.snowball.EnglishStemmer.encode方法的典型用法代碼示例。如果您正苦於以下問題:Python EnglishStemmer.encode方法的具體用法?Python EnglishStemmer.encode怎麽用?Python EnglishStemmer.encode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.stem.snowball.EnglishStemmer
的用法示例。
在下文中一共展示了EnglishStemmer.encode方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: str_to_dict
# 需要導入模塊: from nltk.stem.snowball import EnglishStemmer [as 別名]
# 或者: from nltk.stem.snowball.EnglishStemmer import encode [as 別名]
def str_to_dict(s):
'''
creates dictionary of words and counts
input: s string
output: dictionary {word: count}
'''
s = s.encode('ascii','ignore')
s = str(s)
word_dict = {}
l = re.findall(WORDRE, s)
for w in l:
w = w.lower() # make all letters lowercase
if w[0] == "'": # remove single quotes from beginning/
w = w[1:] # end of words in l
elif w[-1] == "'":
w = w[:-1]
w = EnglishStemmer().stem(w) # stems non-noun/verbs
w = w.encode('ascii','ignore')
if w != '':
if w not in word_dict: # build dictionary
word_dict[w] = 1
else:
word_dict[w] += 1
return word_dict