查找长度相同的单词 [python 3 -enchant lib]
Find words with the same length [python 3 -enchant lib]
强调文本我正在创建一个自动更正程序,它可以在一段文本中找到拼写错误的单词,然后提供建议。到目前为止,该代码可以找到拼写错误的单词,但它可以提供建议。我正在考虑使用的策略之一是找到具有相似长度的单词(具有相同数量的字母但 +1 或 -1 个字母)然后它可以建议它。例如,如果用户输入 "teh lion is sleeping",意思是 "the lion is sleeping",程序将搜索具有相同长度的词,找到 "the",然后给出建议。如果我使用单词列表,这将非常容易,但我使用的是 enchant 库,因为它包含所有英文单词,但我找不到使用该库查找具有相同长度的单词的方法。此外,我知道它有一个内置的自动更正功能,但我不允许使用它。提前谢谢你们。我也尝试尽可能地格式化我的问题,如果代码没有被记录下来,我很抱歉,因为我正在处理它。
wrong_words = []
while True:
import enchant
there_is_a_mistake = False
punctuation = set("!#$%&()*+,./:;<=>?@[\]^_`{|}~"+'”' + '"' + '“')
dictionary = enchant.Dict("en_US")
keyboard_input = input()
words_without_special_characters = "".join(x for x in keyboard_input if x not in punctuation) # https://www.youtube.com/watch?v=llf_pTNhYa4
words_without_special_characters = words_without_special_characters.replace("-" and "—" and "-" and "–" and "-"," ")
words_without_spaces_and_special_characters = words_without_special_characters.split()
number_of_words = len(words_without_spaces_and_special_characters)
for x in range(0,number_of_words):
checked_words = dictionary.check(words_without_spaces_and_special_characters[x])
while checked_words == False:
read = open("saved_words.txt","r")#opens the file in reading mode.
read_file = read.readlines()#reads from the file.
read_file = [item.replace("\n","") for item in read_file]
if words_without_spaces_and_special_characters[x] in read_file: # checks if the word is savedd in the clint word list
break
read.close()#closes the file reading for another operation.
print(words_without_spaces_and_special_characters[x])
words_without_spaces_and_special_characters[x].append(wrong_words)
print("false")
there_is_a_mistake = True
break
if there_is_a_mistake == True:
keyboard_input_yes_or_no_to_save_words = input("if you want to save a word to your dictionary type :yes. (press enter if you dont want to)")
if keyboard_input_yes_or_no_to_save_words == "yes":
while True:
keyboard_input_to_save_words = input("which words do you want to save? (type ignore if you dont want to)")
keyboard_input_to_save_words = keyboard_input_to_save_words.split()
check_if_word_was_orginally_written = set(keyboard_input_to_save_words) <= set(words_without_spaces_and_special_characters)
if check_if_word_was_orginally_written == True:
file = open("saved_words.txt","a+") #opens the file in the writing mode.
for i in range(0,len(keyboard_input_to_save_words.split())):
file.write(keyboard_input_to_save_words[i]) # writes information in the other file.
file.write("\n") # creates new line.
file.close()
break
if keyboard_input_to_save_words == "ignore":
print("nope")
break
else:
print("no words found. Try again. (type ignpre if you dont want to save any word)")
for i in range (len(wrong_words)):
length_of_wrong_word = len(wrong_words)
我不太了解 enchant lib 的作用,但我会在 python3+
中这样做
假设您在名为 word
的变量中有一个字符串
word = "hello"
您可以使用 len(str) 检查该单词的长度:len(word)
print(len(word))
>>>5
然后,您可以使用 for 循环来检查列表中长度与您的单词匹配的每个单词。假设您在名为 listofwords
的列表中有一堆单词
for item in listofwords:
if len(item) == len(word):
print(item)
在这个 for 循环中,它检查列表中的任何项目 'listofwords' 是否与字符串单词 (5) 具有相同的长度,如果是,它会打印回所述项目。
强调文本我正在创建一个自动更正程序,它可以在一段文本中找到拼写错误的单词,然后提供建议。到目前为止,该代码可以找到拼写错误的单词,但它可以提供建议。我正在考虑使用的策略之一是找到具有相似长度的单词(具有相同数量的字母但 +1 或 -1 个字母)然后它可以建议它。例如,如果用户输入 "teh lion is sleeping",意思是 "the lion is sleeping",程序将搜索具有相同长度的词,找到 "the",然后给出建议。如果我使用单词列表,这将非常容易,但我使用的是 enchant 库,因为它包含所有英文单词,但我找不到使用该库查找具有相同长度的单词的方法。此外,我知道它有一个内置的自动更正功能,但我不允许使用它。提前谢谢你们。我也尝试尽可能地格式化我的问题,如果代码没有被记录下来,我很抱歉,因为我正在处理它。
wrong_words = []
while True:
import enchant
there_is_a_mistake = False
punctuation = set("!#$%&()*+,./:;<=>?@[\]^_`{|}~"+'”' + '"' + '“')
dictionary = enchant.Dict("en_US")
keyboard_input = input()
words_without_special_characters = "".join(x for x in keyboard_input if x not in punctuation) # https://www.youtube.com/watch?v=llf_pTNhYa4
words_without_special_characters = words_without_special_characters.replace("-" and "—" and "-" and "–" and "-"," ")
words_without_spaces_and_special_characters = words_without_special_characters.split()
number_of_words = len(words_without_spaces_and_special_characters)
for x in range(0,number_of_words):
checked_words = dictionary.check(words_without_spaces_and_special_characters[x])
while checked_words == False:
read = open("saved_words.txt","r")#opens the file in reading mode.
read_file = read.readlines()#reads from the file.
read_file = [item.replace("\n","") for item in read_file]
if words_without_spaces_and_special_characters[x] in read_file: # checks if the word is savedd in the clint word list
break
read.close()#closes the file reading for another operation.
print(words_without_spaces_and_special_characters[x])
words_without_spaces_and_special_characters[x].append(wrong_words)
print("false")
there_is_a_mistake = True
break
if there_is_a_mistake == True:
keyboard_input_yes_or_no_to_save_words = input("if you want to save a word to your dictionary type :yes. (press enter if you dont want to)")
if keyboard_input_yes_or_no_to_save_words == "yes":
while True:
keyboard_input_to_save_words = input("which words do you want to save? (type ignore if you dont want to)")
keyboard_input_to_save_words = keyboard_input_to_save_words.split()
check_if_word_was_orginally_written = set(keyboard_input_to_save_words) <= set(words_without_spaces_and_special_characters)
if check_if_word_was_orginally_written == True:
file = open("saved_words.txt","a+") #opens the file in the writing mode.
for i in range(0,len(keyboard_input_to_save_words.split())):
file.write(keyboard_input_to_save_words[i]) # writes information in the other file.
file.write("\n") # creates new line.
file.close()
break
if keyboard_input_to_save_words == "ignore":
print("nope")
break
else:
print("no words found. Try again. (type ignpre if you dont want to save any word)")
for i in range (len(wrong_words)):
length_of_wrong_word = len(wrong_words)
我不太了解 enchant lib 的作用,但我会在 python3+
中这样做假设您在名为 word
的变量中有一个字符串word = "hello"
您可以使用 len(str) 检查该单词的长度:len(word)
print(len(word))
>>>5
然后,您可以使用 for 循环来检查列表中长度与您的单词匹配的每个单词。假设您在名为 listofwords
的列表中有一堆单词for item in listofwords:
if len(item) == len(word):
print(item)
在这个 for 循环中,它检查列表中的任何项目 'listofwords' 是否与字符串单词 (5) 具有相同的长度,如果是,它会打印回所述项目。