1

Suppose I want to check if a string has 'pizza' in it. But my user accidentally typed 'I want piztza' by accident (a simple typo). Is there a way to check if the user input approximately contains 'pizza'? Perhaps if only off by one letter.

2
  • Check out the fuzzywuzzy package. Commented Jul 15, 2020 at 22:40
  • 1
    from fuzzywuzzy import fuzz then fuzz.partial_ratio("I want piztza", "pizza") gives 80, whereas fuzz.partial_ratio("I want fish", "pizza") gives 22. You would need to decide on an appropriate cutoff value. Commented Jul 15, 2020 at 22:46

4 Answers 4

1

Try using fuzzywuzzy from PyPI: https://pypi.org/project/fuzzywuzzy/. I am not very experienced with it, but the website should help.

Sign up to request clarification or add additional context in comments.

3 Comments

You would probably use fuzz.partial_ratio. See my comments under the question for an example.
You should just post your answer as an answer to the question instead of a comment.
The culture here is definitely that answers need to be somewhat more developed than what gets posted as comments. Mine was somewhat borderline, but I am happy to just leave it as a comment, especially in view of other answers that have since appeared. Yours, if you don't mind me saying, fits firmly into the category of what most people would post as a comment rather than an answer.
1

fuzzywuzzy would be one way to do this. fuzzywuzzy library compares the similarity between two strings. The Jaccard distance is one method that fuzzywuzzy uses. The Jaccard distance - measure any two sets.

This is what the formula looks like :

enter image description here

Here is a visual representation of the formula:

enter image description here

Here is how you could implement it:

from fuzzywuzzy import fuzz
similarity = fuzz.ratio("hello","world") # 0%

This will give you a ratio of how similar the words are. Here is another example:

from fuzzywuzzy import fuzz
similarity = fuzz.ratio("Austria","Australia") # Will give you 88%

Comments

0

you can use fuzzywuzzy package

from fuzzywuzzy import fuzz

q1 = input("type 'pizza': ")
compare = fuzz.ratio(q1, 'pizza')
if compare > 90:
    print("you type pizza")
else:
    print("you didn't type 'pizza'")

check his documentation for more uses, this is only a simple code.

1 Comment

Wow thanks so much everyone! I checked out fuzzywuzzy but it seemed like it was more useful for directly comparing two similar strings and not for finding strings in strings. I think I will use something similar to what user13914826 posted. Thanks again!
0

Try this:

Sentence = "I want piztza"
Word = "pizza"
Same = False
for Sen_Word in Sentence.lower().split(" "):
    if Sen_Word == Word:
        Same = True
    elif len(Sen_Word) == len(Word)+1:
        Left_Count=0
        Right_Count=0
        for i, x in zip(Sen_Word, Word):
            if i == x:
                Left_Count += 1
            else:
                break
        Letters_Left = min(len(Sen_Word), len(Word))-Left_Count
        for i, x in zip(Sen_Word[::-1], Word[::-1]):
            if Letters_Left - Right_Count > 0 and i == x:
                Right_Count += 1
            else:
                break
        if Left_Count + Right_Count == len(Word):
            Same = True
print(Same)

or a shorter code:

Sentence = "I like piztza"
Word = "pizza"
Same = False
for Sen_Word in Sentence.split(" "):
    if Sen_Word == Word: Same = True
    elif len(Sen_Word) == len(Word)+1:
        Left_Count=Right_Count=0
        for i, x in zip(Sen_Word, Word):
            if i == x: Left_Count += 1
            else: break
        Letters_Left = min(len(Sen_Word), len(Word))-Left_Count
        for i, x in zip(Sen_Word[::-1], Word[::-1]):
            if Letters_Left - Right_Count > 0 and i == x: Right_Count += 1
            else: break
        if Left_Count + Right_Count == len(Word): Same = True
print(Same)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.