當前位置: 首頁>>代碼示例>>Python>>正文


Python fuzz.ratio方法代碼示例

本文整理匯總了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) 
開發者ID:zake7749,項目名稱:Chatbot,代碼行數:25,代碼來源:fuzzyMatcher.py

示例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 
開發者ID:item4,項目名稱:yui,代碼行數:19,代碼來源:fuzz_test.py

示例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 
開發者ID:RobinL,項目名稱:fuzzymatcher,代碼行數:27,代碼來源:tokencomparison.py

示例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 
開發者ID:jonian,項目名稱:kickoff-player,代碼行數:23,代碼來源:streams.py

示例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 
開發者ID:cltk,項目名稱:cltk,代碼行數:21,代碼來源:levenshtein.py

示例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 
開發者ID:phage-nz,項目名稱:ph0neutria,代碼行數:19,代碼來源:string_utils.py

示例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 
開發者ID:chubin,項目名稱:cheat.sh,代碼行數:20,代碼來源:internal.py

示例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 
開發者ID:openhab,項目名稱:openhab-mycroft,代碼行數:18,代碼來源:__init__.py

示例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] 
開發者ID:huseinzol05,項目名稱:Python-DevOps,代碼行數:21,代碼來源:main.py

示例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] 
開發者ID:huseinzol05,項目名稱:Python-DevOps,代碼行數:22,代碼來源:main.py

示例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 
開發者ID:neurostuff,項目名稱:NiMARE,代碼行數:27,代碼來源:utils.py

示例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) 
開發者ID:zake7749,項目名稱:CIKM-AnalytiCup-2018,代碼行數:18,代碼來源:feature_engineering.py

示例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 [] 
開發者ID:THUDM,項目名稱:CogQA,代碼行數:19,代碼來源:process_train.py

示例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()) 
開發者ID:FRCDiscord,項目名稱:Dozer,代碼行數:27,代碼來源:namegame.py

示例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 
開發者ID:tmsquill,項目名稱:simple-ga,代碼行數:9,代碼來源:simple-ga.py


注:本文中的fuzzywuzzy.fuzz.ratio方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。