def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
close_commands = get_close_matches(name, commands.keys())
if close_commands:
guess = close_commands[0]
else:
guess = False
return guess
python类get_close_matches()的实例源码
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def find_thx(s, text):
"""Slack?????????????thx???????
:param s: sqlalchemy.orm.session.Session
:param str text: ???????????
:return dict word_map_names_dict:
???thx??????????Slack????????
:return list hint_names: Slack??????????????????
:return list not_matched: Slack???????????????????
"""
word_map_names_dict = {}
hint_names = []
not_matched = []
thx_matcher = re.compile('(?P<user_names>.+)[ \t\f\v]*\+\+[ \t\f\v]+(?P<word>.+)',
re.MULTILINE)
for thx in thx_matcher.finditer(text):
user_names = [x for x in thx.group('user_names').split(' ') if x]
for name in user_names:
if get_user_name(name.lstrip('<@').rstrip('>')):
slack_id = name.lstrip('<@').rstrip('>')
else:
slack_id = get_slack_id(s, name)
if slack_id:
word_map_names_dict.setdefault(
thx.group('word'), []
).append((slack_id, name))
else:
# ????????????
hint = get_close_matches(name, get_users_info().values())
if hint:
hint_names.append(hint[0])
# ?????????????????
else:
not_matched.append(name)
return word_map_names_dict, hint_names, not_matched
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def suggest(word, cutoff=0.77):
"""
Given a domain and a cutoff heuristic, suggest an alternative or return the
original domain if no suggestion exists.
"""
if word in LOOKUP_TABLE:
return LOOKUP_TABLE[word]
guess = difflib.get_close_matches(word, MOST_COMMON_DOMAINS, n=1, cutoff=cutoff)
if guess and len(guess) > 0:
return guess[0]
return word
def spell_correct(input, choices, threshold=0.6):
"""
Find a possible spelling correction for a given input.
"""
guesses = difflib.get_close_matches(input, choices, 1, cutoff=threshold)
return next(iter(guesses), None)
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def get_best_similar(data):
import difflib
key, use_similar, similar_pool = data
# try to find some close key in existing messages...
# Optimized code inspired by difflib.get_close_matches (as we only need the best match).
# We also consider to never make a match when len differs more than -len_key / 2, +len_key * 2 (which is valid
# as long as use_similar is not below ~0.7).
# Gives an overall ~20% of improvement!
#tmp = difflib.get_close_matches(key[1], similar_pool, n=1, cutoff=use_similar)
#if tmp:
#tmp = tmp[0]
tmp = None
s = difflib.SequenceMatcher()
s.set_seq2(key[1])
len_key = len(key[1])
min_len = len_key // 2
max_len = len_key * 2
for x in similar_pool:
if min_len < len(x) < max_len:
s.set_seq1(x)
if s.real_quick_ratio() >= use_similar and s.quick_ratio() >= use_similar:
sratio = s.ratio()
if sratio >= use_similar:
tmp = x
use_similar = sratio
return key, tmp
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def lc_get_close_matches(lbl, possibilities, num_matches=3, cutoff=0.6):
'''Return list of closest matches to lbl from possibilities (case-insensitive).'''
if USING_PYTHON2:
lc_lbl = str.lower(unicode(lbl))
lc_possibilities = [str.lower(unicode(p)) for p in possibilities]
else:
lc_lbl = str.lower(lbl)
lc_possibilities = [str.lower(p) for p in possibilities]
lc_matches = get_close_matches(lc_lbl, lc_possibilities, num_matches, cutoff)
return [possibilities[lc_possibilities.index(m)] for m in lc_matches]
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return False
def get_similar_commands(name):
"""Command name auto-correct."""
from difflib import get_close_matches
close_commands = get_close_matches(name, commands.keys())
if close_commands:
guess = close_commands[0]
else:
guess = False
return guess
def _get_closest_name(self, name):
if not name:
return
pokemon_names = [p.name for p in inventory.pokemons().STATIC_DATA]
closest_names = difflib.get_close_matches(name, pokemon_names, 1)
if closest_names:
closest_name = closest_names[0]
return closest_name
return name
# Represents the JSON params mappings
def get_closest_name(self, name):
mapping = {ord(x): ord(y) for x, y in zip("\u2641\u2642.-", "fm ")}
clean_names = {n.lower().translate(mapping): n for n in self.pokemon_names}
closest_names = difflib.get_close_matches(name.lower().translate(mapping), clean_names.keys(), 1)
if closest_names:
closest_name = clean_names[closest_names[0]]
if name != closest_name:
self.logger.warning("Unknown Pokemon name [%s]. Assuming it is [%s]", name, closest_name)
return closest_name
else:
raise ConfigException("Unknown Pokemon name [%s]" % name)
def __selectMyTeam(self, string):
teams = [self.getTeam(0).lower(), self.getTeam(1).lower()]
matches = difflib.get_close_matches(string.lower(), teams, 1)
if(len(matches) == 0):
return 0
elif(matches[0] == teams[0]):
return -1
else:
return 1