Skip to the content.

📢 filters

The spamfilter module comes with many built-in filtering algorithms that can determine whether a string is spam or rather not. In this document, all of these filters will be listed.

How to import filters

There are a lot of ways to import the filters you need. If you just want to use a pre-made model for filtering, you don’t need to import them at all, they are going to be imported into the model by itself.

If you want to import the built-in filters into your script, do it using one of the following ways:

For one single filter:

from spamfilter.filters import Filter

For several filters:

from spamfilter.filters import (
    Capitals,
    Length,
    Symbols
)

For all filters

from spamfilter.filters import *

All filters explained

Generally, all filters are stacked onto each other using a machine object which will then check them one after each other.

You construct a filter like this:

Filter(**options, mode = "normal")

Each filter also has a check(string: str) method which accepts a string as an input and will return the filter’s assesment of it using the options given at construction as a tuple.

The tuple is built as following:

(passed: int, output_string: str)

filters.Filter

Base class. No functionality.

filters.Filter(mode : str = "default")

The Filter class is the class all other filters inherit from. It does not contain any further functionality and will always return the string it was given as input, marked as passed.

args:

Filter.mode: the mode of the filter.


filters.Capitals

Filters out texts with too many capital letters.

filters.Capitals(percentage : float = 0.3, mode : str = "normal")

Check if a string contains too much capitals.

args:

Capitals.percentage: how many percent of the text need to be in capital for it to fail.

Capitals.mode: how to handle a failing string.


filters.Length

Filters out too short/long texts.

filters.Length(min_length : int = 10, max_length : int = 200, padding : str = " ", mode : str = "normal")

Checks if a string matches given length requirements.

args:

Length.min: The inclusive minimum length.

Length.max: The inclusive maximum length.

Length.padding: A character used to fill up strings that are too short in the crop mode.

Length.mode: How to handle failing strings.


filters.Symbols

Filters for too many symbols.

filters.Symbols(percentage : float = 0.15, mode : str = "normal", symboldef : str = "explicit", abs_safe_min : int = 3)

Check if a string contains too many symbols.

args:

Symbols.percentage: how many percent of the text need to be symbols for it to fail.

Symbols.mode: how to handle a failing string.

Symbols.symboldef: what to identify as a symbol

Symbols.abs_safe_min: absolute amount of symbols that are always okay to use.


filters.WordLength

Filters for too long, standalone words.

filters.WordLength(max_length : int = 20, mode : str = "absolute", max_abs_population : int = 1, max_perc_population : float = 0.1)

Checks if the words in a string match given length requirements.

args:

Length.max: The inclusive maximum length of a single word.

Length.max_abs_population: The maximum amount of too long words to make the string fail.

Length.max_perc_population: The maximum percentage of too long words to make the string fail.

Length.mode: How to detect failing strings.

Length.split_regex: The regex used to split into standalone words.


filters.PersonalInformation

Personal information detection base class.

filters.PersonalInformation(regex : str, mode : str = "normal", replacement: str = r"***")

Base class other filters for personal information inherit from.

args:

PersonalInformation.mode: how to handle a failing string.

PersonalInformation.regex: the regex used to check for info.

PersonalInformation.replacement: what regex to replace info with.


filters.Email

Filters for accidentally exposed email addresses in texts to filter them out before they reach the public.

filters.Email(regex : str = r"([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)", mode : str = "normal", replacement: str = r"***")

Check if a string contains an email address.

args:

Email.mode: how to handle a failing string.

Email.regex: the regex used to check for email addresses.

Email.replacement: what regex to replace email addresses with.


filters.BypassDetector

Detects fraudulently constructed strings which try to bypass the filters using spaces between the characters.

filters.BypassDetector(percentage = 0.4, max_findings = 5)

Detect if a string is written to bypass filtering.

This is achieved by checking if characters are commonly adjacent to characters that could be used to confuse the filters. The safe characters are called “isles” and defined as a list of characters in BypassDetector.isles.

args:

BypassDetector.percentage: percentage of suspicious adjacencies needed to fail.

BypassDetector.max_findings: absolute number of suspicious adjacencies needed to fail.


filters.Blocklist

Filters for words in a string using a black-/blocklist.

filters.Blocklist(blocklist : set, mode : str = "normal")

Filter text for blocked words. Works better in combination with BypassDetector.

args:

Blocklist.mode: How to handle incoming text.

Blocklist.blocklist: a python set() with words as str objects that shall be blocked.

Blocklist.ignore_regex: a regular expression that matches punctuation characters for splitting the string in non-strict mode.

Blocklist.profanity_replacement: what to replace profanity with.


filters.BlocklistFromJSON

Filters for words in a string using a black-/blocklist loaded from a JSON file list.

filters.BlocklistFromJSON(file : str, mode : str = "normal")

Behaves just like the Blocklist class. Reads a JSON list and inserts it’s content into the Blocklist.blocklist property.

args:

BlocklistFromJSON.file: filename of JSON file.

Incorporating these filters

If you want to use these filters, please don’t use them roughly as Filter instance but rather wrapped into a Machine object.