Python 3-使用正则表达式简化我的近似拼写函数
我已经创建了一个简单的py测试,现在想开发我的程序。作为诵读困难症患者,当我接近匹配一个单词时,我感到相当沮丧,但它只是拼错了 因此,我编写了下面的函数,根据以下规则检查单词的大致拼写:前两个字母必须匹配,最后一个字母必须匹配,并且用户的答案在正确答案的1个字符以内(加号或减号) 代码可以工作,但我确信它可以使用常规表达式简化。我看了一些教程,但现在卡住了 word=巴黎Python 3-使用正则表达式简化我的近似拼写函数,python,regex,Python,Regex,我已经创建了一个简单的py测试,现在想开发我的程序。作为诵读困难症患者,当我接近匹配一个单词时,我感到相当沮丧,但它只是拼错了 因此,我编写了下面的函数,根据以下规则检查单词的大致拼写:前两个字母必须匹配,最后一个字母必须匹配,并且用户的答案在正确答案的1个字符以内(加号或减号) 代码可以工作,但我确信它可以使用常规表达式简化。我看了一些教程,但现在卡住了 word=巴黎 我试图匹配[p,a]{3:5}[s] def closeMatch(): word=input('input you
我试图匹配[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()
def closeMatch():
word=input('在此处输入您的单词')
单词len=len(单词)
lowWord=wordLen-1
highWord=wordLen+1
frontSplit=(字[0:2])
反分裂=(字[-1])
myWord=input('输入要测试的单词')
打印('这是您输入的单词',myWord)
myWordLen=len(myWord)
myWordFSplit=(myWord[0:2])
myWordBSplit=(myWord[-1])
如果myWord==word:
打印('正确')
elif myWordFSplit==前拆分和myWordBSplit==后拆分和myWordLen>=低位字和myWordLen
因此,我编写了下面的函数,根据以下规则检查单词的大致拼写:前两个字母必须匹配,最后一个字母必须匹配,并且用户的答案在正确答案的1个字符以内(加号或减号)。
代码可以工作,但我确信它可以使用常规表达式简化。我看了一些教程,但现在卡住了
我认为你的方法不好。根据你所说的,你需要从你的字典中找到答案,所以我认为你应该计算你输入的单词和字典中的单词之间的距离。我想我读过阅读困难的人通常都能正确理解单词的首字母和末字母,所以我会寻找那些首字母和末字母相同、汉明距离最接近的单词
所以我对你的算法的改进是:
- 将dict中第一个和最后一个字母相同(两个?)的每个单词和输入匹配李>
- 对于该集合的每个单词,计算单词和输入的汉明距离李>
- 按最近距离对其排序,并显示匹配项
我已经重写了你的代码。它接受一个原始的输入字符串、单词,并将其与另一个匹配
字符串,匹配单词,应该拼写正确。函数closeMatch()检查
三个条件。首先它尝试匹配整个单词,word==匹配单词,然后匹配前两个单词
字母,第一个=匹配单词[0:2],最后尝试匹配结尾[-1]。我把“车”写成
匹配词的测试。我使用了两个函数startswith和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)
输出:
如果正确,('correct,您拼写的是'car','correct!')
如果接近,(“几乎正确,您拼写为“,”cab',以“,”ca”开头)
如果是结尾字母,(“几乎正确,您拼写了“,”vcr“,”结尾为“,”r“)
否则,不正确这是我根据注释修改代码的方式-感谢您的帮助
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))
谢谢你的建议-汉明距离提供了一个更好的方法。我在这个网站“发现两个字符串有多相似”上找到了一些代码片段,我很快就找到了一些代码片段,现在我相信我有了一条前进的道路——干杯谢谢帮助——这就是我要做的:环顾四周,玩游戏之后,我将继续导入,并规定前两个和最后一个字母。我希望这很有用。除了start-and-endswith之外,可能还有其他函数,但是条件语句将完成大部分工作。我注意到您的代码中使用了input(),注意input()表示数字,而raw_input()表示字符串。