本文整理匯總了Python中Core.Util.UtilBot.levenshtein_distance方法的典型用法代碼示例。如果您正苦於以下問題:Python UtilBot.levenshtein_distance方法的具體用法?Python UtilBot.levenshtein_distance怎麽用?Python UtilBot.levenshtein_distance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Core.Util.UtilBot
的用法示例。
在下文中一共展示了UtilBot.levenshtein_distance方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: finish
# 需要導入模塊: from Core.Util import UtilBot [as 別名]
# 或者: from Core.Util.UtilBot import levenshtein_distance [as 別名]
def finish(bot, event, *args):
if ''.join(args) == '?':
segments = [hangups.ChatMessageSegment('Finish', is_bold=True),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment(
'Usage: /finish <lyrics to finish> <optional: * symbol to show guessed song>'),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment('Purpose: Finish a lyric!')]
bot.send_message_segments(event.conv, segments)
else:
showguess = False
if args[-1] == '*':
showguess = True
args = args[0:-1]
lyric = ' '.join(args)
songs = Genius.search_songs(lyric)
if len(songs) < 1:
bot.send_message(event.conv, "I couldn't find your lyrics.")
lyrics = songs[0].raw_lyrics
anchors = {}
lyrics = lyrics.split('\n')
currmin = (0, UtilBot.levenshtein_distance(lyrics[0], lyric)[0])
for x in range(1, len(lyrics) - 1):
try:
currlyric = lyrics[x]
if not currlyric.isspace():
# Returns the distance and whether or not the lyric had to be chopped to compare
result = UtilBot.levenshtein_distance(currlyric, lyric)
else:
continue
distance = abs(result[0])
lyrics[x] = lyrics[x], result[1]
if currmin[1] > distance:
currmin = (x, distance)
if currlyric.startswith('[') and currlyric not in anchors:
next = UtilBot.find_next_non_blank(lyrics, x)
anchors[currlyric] = lyrics[next]
except Exception:
pass
next = UtilBot.find_next_non_blank(lyrics, currmin[0])
chopped = lyrics[currmin[0]][1]
found_lyric = lyrics[currmin[0]][0] + " " + lyrics[next][0] if chopped else lyrics[next][0]
if found_lyric.startswith('['):
found_lyric = anchors[found_lyric]
if showguess:
segments = [hangups.ChatMessageSegment(found_lyric),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment(songs[0].name)]
bot.send_message_segments(event.conv, segments)
else:
bot.send_message(event.conv, found_lyric)
return