本文整理匯總了Python中fuzzywuzzy.fuzz.ratio方法的典型用法代碼示例。如果您正苦於以下問題:Python fuzz.ratio方法的具體用法?Python fuzz.ratio怎麽用?Python fuzz.ratio使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fuzzywuzzy.fuzz
的用法示例。
在下文中一共展示了fuzz.ratio方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: tieBreak
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def tieBreak(self, query, i, j):
"""
當去除停用詞後導致兩個字串的匹配度一樣時,從原文裡挑選出更適合的
Args:
- query: 使用者的輸入
- i: index 為 i 的 title
- j: index 為 j 的 title
Return: (target, index)
- target: 較適合的標題
- index : 該標題的 id
"""
raw1 = self.titles[i]
raw2 = self.titles[j]
r1 = fuzz.ratio(query, raw1)
r2 = fuzz.ratio(query, raw2)
if r1 > r2:
return (raw1,i)
else:
return (raw2,j)
示例2: test_ratio
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def test_ratio():
"""Test Korean-specific fuzzy search."""
assert fuzz.ratio('강', '공') == 0
assert ratio('강', '공') == 67
assert fuzz.ratio('안녕', '인형') == 0
assert ratio('안녕', '인형') == 67
assert fuzz.ratio('사당', 'ㅅㄷ') == 0
assert ratio('사당', 'ㅅㄷ') == 57
assert fuzz.ratio('사당', 'ㅏㅏ') == 0
assert ratio('사당', 'ㅏㅏ') == 57
assert fuzz.ratio('사당', 'ㅅㅏㄷㅏㅇ') == 0
assert ratio('사당', 'ㅅㅏㄷㅏㅇ') == 80
示例3: is_mispelling
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def is_mispelling(self, token1, token2):
mis_t1 = set(self.get_misspellings(token1))
mis_t2 = set(self.get_misspellings(token2))
common = mis_t1.intersection(mis_t2).difference({''}) # Difference in case '' included in tokens
if len(common) > 0:
return True
# Misspellings only really make sense if the tokens are words not numbers
if token1.isalpha() and token2.isalpha():
if fuzz.ratio(token1, token2) > self.fuzz_ratio_threshold:
return True
try:
t1f = float(token1)
t2f = float(token2)
if max(t1f, t2f)/min(t1f, t2f) < self.number_fuzz_threshold:
return True
except (ValueError, ZeroDivisionError):
pass
return False
示例4: get_fixture_channels
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def get_fixture_channels(self, events, fixture):
chann = []
items = []
for item in events:
evnt = item['event']
comp = fuzz.ratio(fixture.competition.name, evnt['competition'])
home = fuzz.ratio(fixture.home_team.name, evnt['home'])
away = fuzz.ratio(fixture.away_team.name, evnt['away'])
comb = (comp + home + away) / 3
items.append({ 'ratio': comb, 'channels': item['channels'] })
if items:
sort = sorted(items, key=itemgetter('ratio'), reverse=True)[0]
if sort['ratio'] > 70:
chann = self.data.get_multiple('channel', 'name', sort['channels'])
chann = [c.id for c in chann]
return chann
示例5: ratio
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def ratio(string_a, string_b):
"""At the most basic level, return a Levenshtein distance ratio via
fuzzywuzzy.
:param string_a: str
:param string_b: str
:return: float
"""
from cltk.utils.cltk_logger import logger
try:
from fuzzywuzzy import fuzz
except ImportError as imp_err: # pragma: no cover
message = "'fuzzywuzzy' library required for this module: %s. Install with " \
"`pip install fuzzywuzzy python-Levenshtein`" % imp_err
logger.error(message)
print(message)
raise ImportError
return fuzz.ratio(string_a, string_b) / 100
示例6: similar_string_fast
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def similar_string_fast(first_string, second_string):
"""Determine if two strings are similar (using two most effective methods).
Params:
- first_string: (type: string) first string.
- second_string: (type: string) second string.
Returns:
- result: (type: bool) match result.
"""
partial_score = fuzz.ratio(first_string, second_string)
token_score = fuzz.token_set_ratio(first_string, second_string)
if max(partial_score, token_score) >= SCORE_THRESHOLD_FAST:
return True
return False
示例7: _get_page
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def _get_page(self, topic, request_options=None):
topics_list = self.get_topics_list()
if topic.startswith(':'):
topics_list = [x for x in topics_list if x.startswith(':')]
else:
topics_list = [x for x in topics_list if not x.startswith(':')]
if _USING_FUZZYWUZZY:
possible_topics = process.extract(topic, topics_list, scorer=fuzz.ratio)[:3]
else:
possible_topics = process.extract(topic, topics_list, limit=3, scorer=fuzz.ratio)
possible_topics_text = "\n".join([(" * %s %s" % x) for x in possible_topics])
return """
Unknown topic.
Do you mean one of these topics maybe?
%s
""" % possible_topics_text
示例8: findItemName
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def findItemName(self, itemDictionary, messageItem):
bestScore = 0
score = 0
bestItem = None
try:
for itemName, itemLabel in list(itemDictionary.items()):
score = fuzz.ratio(messageItem, itemLabel)
if score > bestScore:
bestScore = score
bestItem = itemName
except KeyError:
pass
return bestItem
示例9: normalize
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def normalize(self,string):
original_string = string
string = string.lower()
if string[0] == 'x':
if len(string) == 1:
return 'tak'
result_string = 'tak '
string = string[1:]
else:
result_string = ''
results = []
for i in range(len(self.user)):
total = 0
for k in self.user[i]: total += fuzz.ratio(string, k)
results.append(total)
if len(np.where(np.array(results) > 60)[0]) < 1:
return original_string
ids = np.argmax(results)
return result_string + self.corpus[ids]
示例10: normalize
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def normalize(self, string):
original_string = string
string = string.lower()
if string[0] == 'x':
if len(string) == 1:
return 'tak'
result_string = 'tak '
string = string[1:]
else:
result_string = ''
results = []
for i in range(len(self.user)):
total = 0
for k in self.user[i]:
total += fuzz.ratio(string, k)
results.append(total)
if len(np.where(np.array(results) > 60)[0]) < 1:
return original_string
ids = np.argmax(results)
return result_string + self.corpus[ids]
示例11: _expand_df
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def _expand_df(df):
"""
Add alternate forms to DataFrame, then sort DataFrame by alias length
(for order of extraction from text) and similarity to original name (in
order to select most appropriate term to associate with alias).
"""
df = df.copy()
df['alias'] = df['alias'].apply(uk_to_us)
new_rows = []
for index, row in df.iterrows():
alias = row['alias']
alt_forms = _gen_alt_forms(alias)
for alt_form in alt_forms:
temp_row = row.copy()
temp_row['alias'] = alt_form
new_rows.append(temp_row.tolist())
alt_df = pd.DataFrame(columns=df.columns, data=new_rows)
df = pd.concat((df, alt_df), axis=0)
# Sort by name length and similarity of alternate form to preferred term
# For example, "task switching" the concept should take priority over the
# "task switching" version of the "task-switching" task.
df['length'] = df['alias'].str.len()
df['ratio'] = df[['alias', 'name']].apply(_get_ratio, axis=1)
df = df.sort_values(by=['length', 'ratio'], ascending=[False, False])
return df
示例12: _create_fuzzy_wuzzy_features
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def _create_fuzzy_wuzzy_features(self, df):
df['fuzzy_ratio'] = df[['spn_1', 'spn_2']].apply(lambda row: fuzz.ratio(row['spn_1'], row['spn_2']), axis=1)
df['fuzzy_set_ratio'] = df[['spn_1', 'spn_2']].apply(lambda row: fuzz.token_set_ratio(row['spn_1'], row['spn_2']), axis=1)
df['fuzzy_partial_ratio'] = df[['spn_1', 'spn_2']].apply(lambda row: fuzz.partial_ratio(row['spn_1'], row['spn_2']), axis=1)
df['fuzzy_token_sort_ratio'] = df[['spn_1', 'spn_2']].apply(lambda row: fuzz.token_sort_ratio(row['spn_1'], row['spn_2']), axis=1)
df['fuzzy_qratio'] = df[['spn_1', 'spn_2']].apply(lambda row: fuzz.QRatio(row['spn_1'], row['spn_2']), axis=1)
df['fuzzy_WRatio'] = df[['spn_1', 'spn_2']].apply(lambda row: fuzz.WRatio(row['spn_1'], row['spn_2']), axis=1)
def _get_longest_substr_ratio(a, b):
strs = list(distance.lcsubstrings(a, b))
if len(strs) == 0:
return 0
else:
return len(strs[0]) / (min(len(a), len(b)) + 1)
df['longest_substr_ratio'] = df[['spn_1', 'spn_2']].apply(lambda row: _get_longest_substr_ratio(row['spn_1'], row['spn_2']), axis=1)
示例13: find_near_matches
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def find_near_matches(w, sentence):
ret = []
max_ratio = 0
t = 0
for word in sentence.split():
while sentence[t] != word[0]:
t += 1
score = (fuzz.ratio(w, word) + fuzz.partial_ratio(w, word)) / 2
if score > max_ratio:
max_ratio = score
ret = [(t, t + len(word))]
elif score == max_ratio:
ret.append((t, t + len(word)))
else:
pass
t += len(word)
return ret if max_ratio > 85 else []
示例14: check_name
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def check_name(self, ctx, team, name):
"""Checks the name of the team"""
tba_parser = ctx.cog.tba_parser
ftc_teams = ctx.cog.ftc_teams
actual_name = ""
if self.mode == "frc":
# check for existence
team_data = tba_parser.get_team(team)
try:
getattr(team_data, "Errors")
except tbapi.InvalidKeyError:
"""There is no error, so do nothing"""
else:
return -1
actual_name = team_data.nickname
elif self.mode == "ftc":
if team not in ftc_teams:
return -1
actual_name = ftc_teams[team]
self.last_name = actual_name
self.last_team = team
return fuzz.ratio(actual_name.lower(), name.lower())
示例15: fitness
# 需要導入模塊: from fuzzywuzzy import fuzz [as 別名]
# 或者: from fuzzywuzzy.fuzz import ratio [as 別名]
def fitness(agents):
for agent in agents:
agent.fitness = fuzz.ratio(agent.string, in_str)
return agents