我需要搜索确切的单词,并且如果有意义的话,请不要在单词中包含单词,我很困惑
更新:使用regex尝试仍然产生相似的结果,使用了re match但产生了所有单词都不正确
文件不正确的单词:dis是我的“ spel cheker”程序
正确词的文件:这是我的拼写检查程序
word = ""
with open(sys.argv[1],"r") as fh:
while True:
ch=fh.read(1)
if ch == " " or ch == "\n" or ch == ":" or ch == ".":
with open(sys.argv[2],"r") as fh2:
def check_word(word,fh2):
lines = fh2.readlines()
for line in lines:
x= re.match(word,line)
if x:
print(word + ": " + "0")
#count += 1
else:
print(word, ": " , "1")
#count2 += 1
check_word(word,fh2)
word = ''
else:
word += ch
if not ch:
print(word)
print("End of file")
print(count)
print(count2)
break
“因此,它基本上只是在文件中寻找与确切字母匹配的任何内容,因此,如果我在第一个文件中有pumpki这个词,而在第二个文件中有pumpkin,它将产生说出正确或已找到的结果,即使这是不正确的。我需要搜索确切的单词,并且如果在合理的范围内,单词中不包含单词,那我很困惑。”
#counting words
from collections import Counter
f = open("single.txt", "r")
word_list = [] #initiate the list
for words in f:
counting = Counter(words.split(' '))
my_word = 'pumpkin'
for k,v in counting.items():
if my_word == k:
#v is the counter
pair = (k,v)
word_list.append(pair)
print(word_list)
#[('pumpkin', 1)]