Python difflib 模块,get_close_matches() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用difflib.get_close_matches()。
def load_version(self, version: str) -> None:
"""
Loads the regex json files for a version
:param version: The version to load the files for
"""
self._logger.debug(f"Loading regexes for version {version}...")
directory = os.path.join(self._regex_path, version)
if os.path.isdir(directory):
for file in self.get_json_files(directory):
self.process_file(file)
else:
self._logger.warning(f"Version {version} not found.")
close = difflib.get_close_matches(version, os.listdir(self._regex_path))
if len(close) != 0:
self._logger.warning(f"Using closest variation: {close[0]}. This may cause compatibility issues.")
self.load_version(close[0])
else:
self._logger.error("No close variation found. Not attempting to load.")
return
self._logger.debug("Regexes loaded.")
def tag(self, ctx, tag : str = ""):
'''Tags/notes that you can trigger later'''
if not tag:
await self.bot.embed_reply("Add a tag with `{0}tag add [tag] [content]`\nUse `{0}tag [tag]` to trigger the tag you added\n`{0}tag edit [tag] [content]` to edit it and `{0}tag delete [tag]` to delete it".format(ctx.prefix))
return
if tag in self.tags_data.get(ctx.message.author.id, {}).get("tags", []):
await self.bot.reply(self.tags_data[ctx.message.author.id]["tags"][tag])
elif tag in self.tags_data["global"]:
await self.bot.reply(self.tags_data["global"][tag]["response"])
self.tags_data["global"][tag]["usage_counter"] += 1
with open("data/tags.json", 'w') as tags_file:
json.dump(self.tags_data, tags_file, indent = 4)
else:
close_matches = difflib.get_close_matches(tag, list(self.tags_data.get(ctx.message.author.id, {}).get("tags", {}).keys()) + list(self.tags_data["global"].keys()))
close_matches = "\nDid you mean:\n{}".format('\n'.join(close_matches)) if close_matches else ""
await self.bot.embed_reply("Tag not found{}".format(close_matches))
def _check_value(self, action, value):
"""
It's probably not a great idea to override a "hidden" method
but the default behavior is pretty ugly and there doesn't
seem to be any other way to change it.
"""
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
msg = ['Invalid choice, valid choices are:\n']
for i in range(len(action.choices))[::self.ChoicesPerLine]:
current = []
for choice in action.choices[i:i+self.ChoicesPerLine]:
current.append('%-40s' % choice)
msg.append(' | '.join(current))
possible = get_close_matches(value, action.choices, cutoff=0.8)
if possible:
extra = ['\n\nInvalid choice: %r, maybe you meant:\n' % value]
for word in possible:
extra.append(' * %s' % word)
msg.extend(extra)
raise argparse.ArgumentError(action, '\n'.join(msg))
def get_closer_gallery_title_from_list(original: str, titles: List[Tuple[str, str]], cutoff: float) -> Optional[Tuple[str, str]]:
compare_titles = []
compare_ids = []
for title in titles:
compare_titles.append(title[0])
compare_ids.append((title[0], title[1]))
matches = difflib.get_close_matches(original, list(compare_titles), 1, cutoff)
if len(matches) == 0:
return None
match_title = matches[0]
for i, compare_title in enumerate(compare_titles):
if compare_title == match_title:
return compare_ids[i][0], compare_ids[i][1]
return None
def get_gallery_closer_title_from_gallery_values(original: str, gallery_datas: List[GalleryData], cutoff: float) -> ResultContainer:
result = ResultContainer()
compare_titles = []
for gallery_dict in gallery_datas:
compare_titles.append(gallery_dict.title)
matches = difflib.get_close_matches(original, compare_titles, 1, cutoff) # type: ignore
if len(matches) == 0:
return result
result.match_title = str(matches[0])
for i, compare_title in enumerate(compare_titles):
if compare_title == result.match_title:
result.match_values = gallery_datas[i]
result.match_link = gallery_datas[i].link
gallery_datas[i].link = None
return result
def setProvider(self, provider):
"""Set the provider."""
if(provider):
matches = difflib.get_close_matches(
provider, self.__VALID_PROVIDERS, 1)
if(len(matches) == 0):
new = self.__VALID_PROVIDERS[0]
else:
new = matches[0]
if(self.__data['provider'] != new):
self.__data['provider'] = new
self.__metaChanged = True
else:
self.__data['provider'] = self.__VALID_PROVIDERS[0]
self.__initMatchGrabber()
return True
def autoSetMyTeam(self):
"""Try to set team via fav teams."""
try:
for team_idx in range(2):
team = self.__data['teams'][team_idx]['name']
matches = difflib.get_close_matches(
team.lower(), scctool.settings.config.getMyTeams(), 1)
if(len(matches) > 0):
self.setMyTeam(team_idx * 2 - 1)
return True
self.setMyTeam(0)
return False
except Exception as e:
module_logger.exception("message")
return False
def parse(word):
"""Interpret word as a fTerm word."""
if word in synonyms.values():
return word
elif word in synonyms:
return synonyms[word]
else:
lookup = get_close_matches(word, verbs.keys() + synonyms.keys())
if len(lookup) == 0:
# there aren't any reasonable matches
raise KeyError
else:
if lookup[0] in synonyms:
return synonyms[lookup[0]]
else:
return lookup[0]
def map_strings(set_keys,
set_values,
cutoff=0.8,
ignore_no_matches=True):
"""Map a set of secondary strings to a set of primary strings."""
N = 1
CUTOFF = cutoff
def get_matches(x):
"""Help to get matches."""
result_list = difflib.get_close_matches(
x, set_values, n=N, cutoff=CUTOFF)
if ignore_no_matches:
if result_list:
return result_list[0]
else:
return ''
else:
return result_list[0]
mapper = map(lambda x: (x, get_matches(x)),
set_keys)
return dict(mapper)
def resolve_command(self, ctx, args):
"""
Override clicks ``resolve_command`` method and appends *Did you mean ...* suggestions to the
raised exception message.
"""
try:
return super(AliasedGroup, self).resolve_command(ctx, args)
except click.exceptions.UsageError as error:
error_msg = str(error)
original_cmd_name = click.utils.make_str(args[0])
matches = difflib.get_close_matches(
original_cmd_name,
self.list_commands(ctx),
self.max_suggestions,
self.cutoff)
if matches:
error_msg += '{0}{0}Did you mean one of these?{0} {1}'.format(
os.linesep,
'{0} '.format(os.linesep).join(matches, ))
raise click.exceptions.UsageError(error_msg, error.ctx)
def get_user_matches(name):
"""get_user_matches
Gets the closest name based on the users you have added.
params:
name: str: The name to get the match from.
"""
name = ' '.join(name)
vprint('Attempting to match: "{}"'.format(name), 2)
with open('{}/data.json'.format(FILE_DIR)) as f:
data = f.read()
data = json.loads(data)
if name == '':
return data
names = list(data.keys())
close = difflib.get_close_matches(name, names, 1, 0)
if close == []:
vprint("Uh oh, couldn't find a match. Using '{}'".format(names[0]), 2)
close = names
vprint("I suppose '{}' is close enough.".format(close[0]), 2)
return {close[0]: data[close[0]]}
def get_trace_name(cls, instance_id, trace_id, create=False, hook=False):
if create:
trace_id = cls.create_new_id(instance_id)
elif trace_id is None: # get last id
trace_id = sorted(cls._existing_trace_ids(instance_id))[-1]
else:
if not hook:
existing = sorted(cls._existing_trace_ids(instance_id))
if trace_id not in existing:
try:
i = int(trace_id)
except:
i = None
if i is not None:
i = cls._format_id(i)
if i in existing:
trace_id = i
res = difflib.get_close_matches(trace_id, existing, 1, 0)
if not res:
if len(existing) == 0:
raise Exception("No exising trace result dirs")
trace_id = existing[-1]
else:
trace_id = res[0]
return cls._format_id(trace_id)
def search(self, query: str) -> typing.List[Station]:
"""
Searches for unique stations that match the query.
"""
logging.debug("Query: %s", query)
return [
self.code_station[code]
for code in collections.OrderedDict.fromkeys(
self.name_station[name].code
for name in difflib.get_close_matches(query.lower(), self.names)
)
]
# Bot implementation.
# ----------------------------------------------------------------------------------------------------------------------
def resolve_command(self, ctx, args):
"""
Overrides clicks ``resolve_command`` method
and appends *Did you mean ...* suggestions
to the raised exception message.
"""
try:
return super(DYMMixin, self).resolve_command(ctx, args)
except click.exceptions.UsageError as error:
error_msg = str(error)
original_cmd_name = click.utils.make_str(args[0])
matches = difflib.get_close_matches(original_cmd_name,
self.list_commands(ctx), self.max_suggestions, self.cutoff)
if matches:
error_msg += '\n\nDid you mean one of these?\n %s' % '\n '.join(matches) # pylint: disable=line-too-long
raise click.exceptions.UsageError(error_msg, error.ctx)
def check_argument(args):
if is_python2:
for i in range(len(args)):
args[i] = args[i].decode(stdin_encoding)
if len(args) < 2:
exit_due_to_invalid_command()
if args[1].lower() in ('-v', '-version', 'v', 'version'):
print('qsctl %s' % __version__)
sys.exit(0)
if args[-1].lower() == 'help' and len(args) <= 3:
args[0] = "qsctl"
command = "-".join(args[:-1])
renderer = get_renderer(command)
renderer.render()
sys.exit(0)
command = args[1]
if command not in COMMANDS:
suggest_commands = get_close_matches(command, COMMANDS)
exit_due_to_invalid_command(suggest_commands)
def dispatch(self, globals):
from difflib import get_close_matches
meta = "ACTION" # function is only invoked for listing ACTIONs
if len(sys.argv) == 1:
self.print_help()
action = sys.argv[1]
if not action in self.valid_actions:
print >> sys.stderr, "[error] {0} not a valid {1}\n".format(action, meta)
alt = get_close_matches(action, self.valid_actions)
print >> sys.stderr, "Did you mean one of these?\n\t{0}\n".\
format(", ".join(alt))
self.print_help()
globals[action](sys.argv[2:])
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_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 tag_search(self, ctx, *, search : str):
'''Search your tags'''
if (await self.check_no_tags(ctx)): return
tags = self.tags_data[ctx.message.author.id]["tags"]
results = [t for t in tags.keys() if search in t]
if results:
await self.bot.embed_reply("{} tags found: {}".format(len(results), ", ".join(results)))
return
close_matches = difflib.get_close_matches(search, tags.keys())
close_matches = "\nDid you mean:\n{}".format('\n'.join(close_matches)) if close_matches else ""
await self.bot.embed_reply("No tags found{}".format(close_matches))
def check_no_tag(self, ctx, tag):
tags = self.tags_data[ctx.message.author.id]["tags"]
if not tag in tags:
close_matches = difflib.get_close_matches(tag, tags.keys())
close_matches = "\nDid you mean:\n{}".format('\n'.join(close_matches)) if close_matches else ""
await self.bot.embed_reply("You don't have that tag{}".format(close_matches))
return not tag in tags
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
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_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)