Check if the characters are similar in Python

As the title says I tried to make it as an anti-spam measure A function that returns True if it has the specified number of characters Forgive spaghetti source code You may not be able to use it so much w After powering up, edit to make a new one

code

Take out character by character and add it to the one to compare it and check if it exists

The one I made first

def similer(b,a,m):
    al = list(a)
    cc = ""
    cu = 0
    co = 0
    cuo = 0
    for c in al:
        co += 1
        for ck in al:
            cuo += 1
            if cuo-1 < co-1:
                continue
            cc = cc + ck
            cu += 1
            if cu == m:
                break
        if cu == m:
            if cc in b:
                return True
        cc = ""
        cuo = 0
        cu = 0
    return False
#Spaghetti source code w
#Maybe it can be made more compact, but please forgive me
#It was a hassle

The one made by another person in the comment section (additional note)

This one is overwhelmingly easy to understand and compact I learned something Thank you

def similer(b,a,m):
    return any(a[i:i+m] in b for i in range(len(a) - m))
#Take it with a slice and confirm it and return with any
#It will be a great study

Example of use

similer (characters to be checked, characters to be checked, length of words to check if they are worn) Can be used in.

spam

A spam message that sends a message with random characters that was a chat. True is back.

b = """Hello. This is Flage Chat
嘮"""
a = """Hello. This is Flage Chat
蕻"""

print(similer(b,a,5))
# True

Ordinary message

False is back because it's not spam.

b = """i-FILTER false positive w"""
a = """Was put in a long time ago"""

print(similer(b,a,5))
# False

Look if you're trying to use

Note! When using it in a chat app, check it several times or repeatedly. If you send it with a person's name, it will be misjudged as spam. Sometimes short sentences cannot be detected. Let's repeat it by increasing the last argument of the function in order.

False positive example

If you compare these things, the name of the person tasuren will be True.

b = """Did tasuren do it?"""
a = """Because tassuren did hugahuga"""

print(similer(b,a,5))
# True

So When True is returned as spam five times in a row when sending a message five times It's spam! It is better to make it feel like letting you know.

the end

It's the end!

Recommended Posts

Check if the characters are similar in Python
Check if the URL exists in Python
[python] How to check if the Key exists in the dictionary
Check the behavior of destructor in Python
python Note: Determine if command line arguments are in the list
Check if the password hash generated by PHP matches in Python
How to check if the contents of the dictionary are the same in Python by hash value
Determining if there are birds in the image
Check if the expected column exists in Pandas DataFrame
Check if it is Unix in the scripting language
Shift the alphabet string by N characters in Python
Check if it is Unix in the scripting language
Using the LibreOffice app in Python (1) Where are the macros?
Check the asymptotic nature of the probability distribution in Python
Download the file in Python
Find the difference in Python
[Python] Check the installed libraries
How to check in Python if one of the elements of a list is in another list
Make sure all the elements in the list are the same in Python
Check the operation of Python for .NET in each environment
Master the type in Python? (When should type check be done)
Check for memory leaks in Python
[Python] Check the current directory, move the directory
Getting the arXiv API in Python
Check for external commands in python
Check the behavior when assigning Python
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Display characters like AA in python
Check and move directories in Python
Hit the web API in Python
Check the data summary in CASTable
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
Count the number of Thai and Arabic characters well in Python
Check if the configuration file is read in an easy-to-understand manner
How to check the memory size of a variable in Python
python: Use the variables defined in the string format as they are
Delete a particular character in Python if it is the last
How to check the memory size of a dictionary in Python
Determine if all list elements are present in the dict key
Check if you can connect to a TCP port in Python
Verification of the theory that "Python and Swift are quite similar"
Have python check if the string can be converted / converted to int
[Golang] Check if a specific character string is included in the character string
If you think that the person you put in with pip doesn't work → Maybe you are using python3?
Learn the design pattern "Prototype" in Python
Fill the string with zeros in python and count some characters from the string
Learn the design pattern "Builder" in Python