我创建了一个简单的py测验,现在想开发我的程序。当我已经非常接近匹配一个单词时,我会感到非常沮丧,但它并没有完全拼写正确。
因此我编写了以下函数,根据以下规则检查单词的大致拼写:前两个字母必须匹配,最后一个字母必须匹配,用户答案是1个字符,加或减,正确的答案。
代码有效但我相信它可以使用常规expr进行简化。我看了几个教程,但现在卡住了。
字=巴黎
我试图匹配[p,a] {3:5} [s]
def closeMatch():
word=input('input your word here')
wordLen=len(word)
lowWord=wordLen-1
highWord=wordLen+1
frontSplit=(word[0:2])
backSplit=(word[-1])
myWord=input('input the word you want to test')
print('this is the word you entered ' , myWord)
myWordLen=len(myWord)
myWordFSplit=(myWord[0:2])
myWordBSplit=(myWord[-1])
if myWord==word:
print('Correct')
elif myWordFSplit==frontSplit and myWordBSplit==backSplit and myWordLen>=lowWord and myWordLen<=highWord:
print('Nearly correct ' , myWord , ' has at least the first 2 and last letters the same')
print('Also the length of ' , myWord , ' is within one character, plus or minus, of the the word ' , word )
else:
print('Incorrect')
closeMatch()
答案 0 :(得分:1)
因此我编写了以下函数,根据以下规则检查单词的大致拼写:前两个字母必须匹配,最后一个字母必须匹配,用户答案是1个字符,加或减,正确答案。 代码有效但我相信它可以使用常规expr进行简化。我看了几个教程,但现在卡住了。
我认为你的方法不是好方法。根据你所说的,你需要从你的字典中找到closest string,所以我认为你应该计算你输入的单词和字典中单词之间的hamming distance。我认为我读过阅读障碍的人通常会在单词的第一个和最后一个单词中找到正确的单词,所以我会寻找与汉明距离最接近的第一个和最后一个字母相同的单词。
因此,我对您的算法所做的改进将是:
HTH
答案 1 :(得分:0)
我重写了你的代码。它需要一个raw_input字符串,word,并将其与另一个匹配 string,match_word,应该正确拼写。函数closeMatch()检查 三个条件。首先,它尝试匹配整个单词,word ==匹配单词,然后是first_two letters,first = match_word [0:2],最后它尝试匹配结尾[-1]。我把“汽车”写成了 对match_word的测试。我使用了两个函数,startwith和endswith,它们与开头相匹配 和字符串的结尾字母分别为。
import re
# takes raw_input word, word
word = raw_input('Word:')
# takes correctly spelled word
match_word = "car"
def closeMatch(word, match_word):
first = match_word[0:2] # first two letters
back = match_word[-1] # end letter
for i in range(len(word)):
# checks if the word starts with (startswith function) the front letter
if word == match_word:
return "Correct, you spelled", word, "correctly!"
elif word.startswith(first):
return "Nearly correct, you've spelled", word, "begining with",first
elif word[-1].endswith(back):
return "Almost correct, you've spelled", word, "ending with",back
else:
return "Incorrect"
print closeMatch(word,match_word)
输出: 如果正确,('正确,你拼写','汽车','正确!') 如果差不多,(“几乎正确,你拼写”,“出租车”,“开始时”,“ca”) 如果结束信,(“几乎正确,你拼写”,'vcr','以'结尾','r') 否则,错误
答案 2 :(得分:0)
这是我如何根据评论修改代码 - 感谢您的帮助
import difflib
word = 'maneuver' #nasty word to spell (for us dyslexics anyway)
match_word =input('The word is maneuver >> test it here')
def closeMatch(word, match_word):
first = match_word[0:2] # first two letters
back = match_word[-1] # end letter
#keeping with the rule that first two and last letters must be the same as the word
for i in range(len(word)):
# checks if the word starts with (startswith function) the front letter
if word == match_word:
return 'Correct, you spelled', word, 'correctly!'
elif word.startswith(first) and word[-1].endswith(back):
ratioScore=difflib.SequenceMatcher(None, word, match_word).ratio()
#with the use of the difflib library I now have value to logic test also
return 'Nearly correct - the ratio score is ' , ratioScore , ' Now logic test the ratio score'
else:
return 'Incorrect'
print (closeMatch(word, match_word))