A
A
Alexander2022-02-04 14:38:16
Python
Alexander, 2022-02-04 14:38:16

How to find the percentage of occurrence of a phrase in a string in python?

Hello
I have the phrase 'building chatbots'.

I need to find out the percentage of occurrence of this phrase in some text, for example

"Good afternoon, I need to create a telegram bot for posting ads."

Or please offer your own versions of the search in the text for keywords / phrases

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Leonid, 2022-02-04
@bravebug

You can, for example, use fuzzywuzzy
The link has examples of use:
https://pypi.org/project/fuzzywuzzy/

D
dmshar, 2022-02-04
@dmshar

If you strictly follow what you wrote, then the percentage of the occurrence of the phrase
'creating chatbots' in your string is 0. Since the phrase is the occurrence of all the words provided as a sample, and the word "chat" or "chatbot" in the given offer is missing.
If you are interested in the percentage of the occurrence of words from the example in the set of words of the target sentence, then this is easier. True, if the words are in the same word form.

ex='создать чат бота'
ex_set=set(ex.split(' '))
sent="Добрый день, Требуется создать телеграм бота для размещения объявлений"
sent_set=set(sent.split(' '))
print(1-len(sent_set.difference(ex_set))/len(sent_set))

Result:
0.2222222222222222
But if the word forms are different, then you have to sweat. Bringing words to the same word form is a separate and very non-trivial task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question