简体   繁体   中英

How can I know through similarity how specific string is included in the sentence?

How can I know through similarity how specific string is included in the sentence??


  • for example..

ex) sentence : The community is here to help you with specific coding, algorithm, or language problems.

specific string : algorism

  • after run

similarity : 0.8248242 (algorism - algorithm)


Now, I'm using Python&jellyfish. What I am planning is to check the mail subject in Outlook and classify it based on a specific string list.

HELP ME.................

Try this ?

import string
from difflib import SequenceMatcher

def similarity(a, b): # EDIT WITH YOU OWN SIMILARITY OF NOT CORRECT
    return SequenceMatcher(None, a, b).ratio()

def max_similar(sentence, string_to_find):
    result = ["", 0]
    # Remove punctuation
    sentence = sentence.translate(str.maketrans('', '', string.punctuation))
    # split to list
    sentence = sentence.split()
    for word in sentence:
        coeff = similarity(word, string_to_find)
        if coeff > result[1]:
            result[0] = word
            result[1] = coeff
    return result

print(max_similar("The community is here to help you with specific coding, algorithm, or language problems.", "algorism"))

Result :

['algorithm', 0.8235294117647058]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM