很明显,我遗漏了一些简单的东西,我的猜测是,text1中还存在其他一些“whale”的外壳。比答案更重要的是,除了在text1和text1L中不区分大小写地搜索“whale”之外,如何有效地调试它?你知道吗
谢谢,我在NLTK的日子还早。你知道吗
import nltk
from nltk.book import *
text1L=[w.lower() for w in text1]
print(text1L.count('whale'))
>>>1226
print(text1.count('Whale')+text1.count('whale'))
>>>1188
您可以检查下面的代码,看看在
text1
中是否也有“WHALE”所以,现在你有:
有一些鲸鱼。你知道吗
所以:
为了弄清楚这一点,生成了单词“whale”的所有变体,并打印出非零计数的变体。你知道吗
产生变化:
然后:
作为旁注,所有的变化看起来都很整齐:
遍历
nltk.Text
对象返回一个字符串列表,每个字符串都是一个单词,如果对列表中的所有字符串应用相同的操作,那么使用map()
可能是一个好主意。你知道吗为了解答其他“鲸鱼”从何而来的奥秘,我们得到1226条:
关于@axiom生成所有可能的“whale”大小写组合的想法,请参见String manipulation in Python (All upper and lower case derivatives of a word)
相关问题 更多 >
编程相关推荐