x = '“ There is considerable conjecture and debate as to how...
newx = ''.join(e for e in x.lower() if e.isalnum())
print newx
>>> 'thereisconsiderableconjectureanddebateastohow'
from nltk.stem.snowball import EnglishStemmer
from nltk import word_tokenize
stemmer = EnglishStemmer()
stem_word = stemmer.stem
stem = stem_word(u"conjecture")
sentence = u'He conjectured that the interface was...'
words = word_tokenize(sentence)
found_words = [(i, w) for i, w in enumerate(words) if stem_word(w) == stem]
# -> [(1, u'conjectured')]
您可以先尝试删除特殊字符:
然后使用
find
来定位你的单词。在祝你好运!在
编辑:
如果你想找到你指定单词前面的单词,你可以把句子分开。下面是一段可能有帮助的代码:
^{pr2}$这样可以得到:
忘记了在后台实际完成的隐性工作,这至少能完成你要求的任务(希望如此)。在
老实说,我希望用正则表达式来设置线性搜索,但是unicode值通常被分成两个“字符”。在
相反,这里有一个至少能完成任务的黑客:
^{pr2}$如果你想忘掉那些讨厌的编码字符:
其他答案中显示的
word in string
方法通常会失败,例如,在一个含有communities
的句子中,他们找不到community
这个词。在在这种情况下,您可能需要一个词干分析算法,如^{} package :
还有其他的stem和tokenize methods in nltk,您可以根据具体需要使用。在
“讨厌的字符”是错误地将
^{pr2}$utf-8
字节序列视为cp1252
的结果:你不应该盲目地删除乱码文本,而是修改字符编码。在
Why the #AskObama Tweet was Garbled on Screen: Know your UTF-8, Unicode, ASCII and ANSI Decoding Mr. President显示了这个问题在电视上公开的例子。在
为了理解阅读The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)。在
相关问题 更多 >
编程相关推荐