192

I have two lists:

  • a list of about 750K "sentences" (long strings)
  • a list of about 20K "words" that I would like to delete from my 750K sentences

So, I have to loop through 750K sentences and perform about 20K replacements, but ONLY if my words are actually "words" and are not part of a larger string of characters.

I am doing this by pre-compiling my words so that they are flanked by the \b word-boundary metacharacter:

compiled_words = [re.compile(r'\b' + word + r'\b') for word in my20000words]

Then I loop through my "sentences":

import re

for sentence in sentences:
  for word in compiled_words:
    sentence = re.sub(word, "", sentence)
  # put sentence into a growing list

This nested loop is processing about 50 sentences per second, which is nice, but it still takes several hours to process all of my sentences.

  • Is there a way to using the str.replace method (which I believe is faster), but still requiring that replacements only happen at word boundaries?

  • Alternatively, is there a way to speed up the re.sub method? I have already improved the speed marginally by skipping over re.sub if the length of my word is > than the length of my sentence, but it's not much of an improvement.

I'm using Python 3.5.2

smci
  • 32,567
  • 20
  • 113
  • 146
pdanese
  • 2,187
  • 4
  • 15
  • 21
  • @MohammadAli No, I had not. I will look into it. If you have any suggested tutorial(s), please advise. Thank you – pdanese Mar 12 '17 at 01:51
  • 2
    The first answer here has some good sample code: http://stackoverflow.com/questions/2846653/how-to-use-threading-in-python just divide up your sentence array by the number of CPU cores you have then run that many threads – Mohammad Ali Mar 12 '17 at 01:53
  • 7
    You can also try a non-regex implementation - traverse your input word by word and match every with a set. This is single pass and hash lookups are pretty quick. – pvg Mar 12 '17 at 02:45
  • 2
    How long are these sentences, incidentally? 750k lines doesn't sound like a dataset that should be taking hours to process. – pvg Mar 12 '17 at 02:57
  • You can even use regex to the non regex solution suggested by pvg :-) Just regex replace all "\S+" with function, that returns the same string when it is not in the set or empty string when it is in the set. My test (but not in python) proceses about 10M characters per second. – Antonín Lejsek Mar 12 '17 at 03:46
  • 2
    @MohammadAli: Don't bother with that example for CPU-bound work. Python has a big lock that it takes when executing bytecode (the Global Interpreter Lock), so you can't benefit from threads for CPU work. You'd need to use `multiprocessing` (i.e. multiple Python processes). – Kevin Mar 12 '17 at 07:09
  • What do the sentences look like? Do they have punctuation? Is space the only delimiter between words? Replacing a word by an empty string will leave two spaces. – Eric Duminil Mar 12 '17 at 12:09
  • @pvg: I've only read your comment after I posted an answer. Your suggestion is good, and the corresponding code appears to be much faster than the accepted solution. – Eric Duminil Mar 12 '17 at 13:17
  • @EricDuminil ah, the match callback is a nice touch. It's definitely more explicit and clearer (to my eyes), as to faster, I didn't try it. What kind of time did you get with the concat'ed regexen in your example? – pvg Mar 12 '17 at 14:29
  • @pvg: I didn't want to mention it explicitely, because I might have missed something. By using `re.compile(r"\b(%s)\b" % '|'.join(words), re.IGNORECASE)` with 100000 english words, the concated regexen took 160s for 10000 sentences. In comparison, my code took 7s for 100 times more sentences. – Eric Duminil Mar 12 '17 at 15:13
  • @EricDuminil I think it's actually worth putting in the answer, along with the timings. The one thing I think is a little potentially confusing about it is the apparent suggestion the regex variant is a check against a list of words. – pvg Mar 12 '17 at 15:39
  • @pvg If it turns out to be the correct timing, I sure would put it in my answer. I asked for confirmation from OP and Liteye, to be sure I don't disrespect a valid answer by saying it's very slow when it actually isn't. I'll update the answer, but `"\b(word1|word2|word3)\b"` is nothing more than a check against a list of words IMHO. In C, with a regexp, but the complexity still depends linearly on the number of words in the regex. – Eric Duminil Mar 12 '17 at 15:45
  • @EricDuminil The compiled regex will short-circuit so it's not quite the same as linear list membership comparison. But that's a nit. As to the validity of the times, you don't have to claim your proposed solutions are faster for OP's specific dataset, just for the ones you tried. This question comes up a lot in various guises and could use more testable answers. The goal, after all, is 'usefulness to others' rather than simply 'usefulness to poster'. – pvg Mar 12 '17 at 15:51
  • @pvg: For a word not included in `(word1|word2|word3)`, regex would still need to check every word before saying there's no match, right? – Eric Duminil Mar 12 '17 at 16:01
  • You're doing stopword removal aren't you? sklearn has vectorizers that do this with higher performance. – smci Mar 13 '17 at 01:38
  • This looks like it could be a good question for [codereview.SE](http://codereview.stackexchange.com/) – Amani Kilumanga Mar 13 '17 at 02:43
  • @user36476: Did you try my suggestion? I'd be happy to know how long it takes to process your data. – Eric Duminil Mar 14 '17 at 09:10
  • 2
    You need an industrial [strength tool](http://www.regexformat.com/version7_files/Rx5_ScrnSht01.jpg) to do this. A regex trie is generated from a ternary tree of a list of strings. There is never more than 5 steps to failure making this the fastest method to do this type of matching. Examples: [175,000 word dictionary](http://www.regexformat.com/Dnl/_Samples/_Ternary_Tool%20(Dictionary)/___txt/_ASCII_175,000_word_Mix_A-Z_Multi_Lined.txt) or similar to your banned list just the [20,000 S-words](http://www.regexformat.com/Dnl/_Samples/_Ternary_Tool%20(Dictionary)/___txt/S-words.txt) –  Jan 03 '20 at 10:04
  • 1
    Benchmark on just the S-words `Completed iterations: 50 / 50 ( x 1 ) Matches found per iteration: 20050 Elapsed Time: 4.73 s, 4734.55 ms, 4734549 µs Matches per sec: 211,741` Also, see [US City's Samples](http://www.regexformat.com/Dnl/_Samples/US_Cities/) –  Jan 03 '20 at 10:11
  • 3 years later : thanks for your question. It allowed me to get 2 "great answer" badges :). – Eric Duminil Apr 14 '20 at 03:58
  • This is a regex optimization question in disguise; your `compiled_words` is a horrifically inefficient list of 20K regexes (instead of one single regex). If *words* is known statically, you can optimize the regex even more. – smci Dec 24 '20 at 06:43
  • Hello. What about re2 wrapper? It works faster, I'm guessing. – freeearth Jun 04 '22 at 21:34

10 Answers10

192

TLDR

Use this method if you want the fastest regex-based solution. For a dataset similar to the OP's, it's approximately 1000 times faster than the accepted answer.

If you don't care about regex, use this set-based version, which is 2000 times faster than a regex union.

Optimized Regex with Trie

A simple Regex union approach becomes slow with many banned words, because the regex engine doesn't do a very good job of optimizing the pattern.

It's possible to create a Trie with all the banned words and write the corresponding regex. The resulting trie or regex aren't really human-readable, but they do allow for very fast lookup and match.

Example

['foobar', 'foobah', 'fooxar', 'foozap', 'fooza']

Regex union

The list is converted to a trie:

{
    'f': {
        'o': {
            'o': {
                'x': {
                    'a': {
                        'r': {
                            '': 1
                        }
                    }
                },
                'b': {
                    'a': {
                        'r': {
                            '': 1
                        },
                        'h': {
                            '': 1
                        }
                    }
                },
                'z': {
                    'a': {
                        '': 1,
                        'p': {
                            '': 1
                        }
                    }
                }
            }
        }
    }
}

And then to this regex pattern:

r"\bfoo(?:ba[hr]|xar|zap?)\b"

Regex trie

The huge advantage is that to test if zoo matches, the regex engine only needs to compare the first character (it doesn't match), instead of trying the 5 words. It's a preprocess overkill for 5 words, but it shows promising results for many thousand words.

Note that (?:) non-capturing groups are used because:

Code

Here's a slightly modified gist, which we can use as a trie.py library:

import re


class Trie():
    """Regex::Trie in Python. Creates a Trie out of a list of words. The trie can be exported to a Regex pattern.
    The corresponding Regex should match much faster than a simple Regex union."""

    def __init__(self):
        self.data = {}

    def add(self, word):
        ref = self.data
        for char in word:
            ref[char] = char in ref and ref[char] or {}
            ref = ref[char]
        ref[''] = 1

    def dump(self):
        return self.data

    def quote(self, char):
        return re.escape(char)

    def _pattern(self, pData):
        data = pData
        if "" in data and len(data.keys()) == 1:
            return None

        alt = []
        cc = []
        q = 0
        for char in sorted(data.keys()):
            if isinstance(data[char], dict):
                try:
                    recurse = self._pattern(data[char])
                    alt.append(self.quote(char) + recurse)
                except:
                    cc.append(self.quote(char))
            else:
                q = 1
        cconly = not len(alt) > 0

        if len(cc) > 0:
            if len(cc) == 1:
                alt.append(cc[0])
            else:
                alt.append('[' + ''.join(cc) + ']')

        if len(alt) == 1:
            result = alt[0]
        else:
            result = "(?:" + "|".join(alt) + ")"

        if q:
            if cconly:
                result += "?"
            else:
                result = "(?:%s)?" % result
        return result

    def pattern(self):
        return self._pattern(self.dump())

Test

Here's a small test (the same as this one):

# Encoding: utf-8
import re
import timeit
import random
from trie import Trie

with open('/usr/share/dict/american-english') as wordbook:
    banned_words = [word.strip().lower() for word in wordbook]
    random.shuffle(banned_words)

test_words = [
    ("Surely not a word", "#surely_NöTäWORD_so_regex_engine_can_return_fast"),
    ("First word", banned_words[0]),
    ("Last word", banned_words[-1]),
    ("Almost a word", "couldbeaword")
]

def trie_regex_from_words(words):
    trie = Trie()
    for word in words:
        trie.add(word)
    return re.compile(r"\b" + trie.pattern() + r"\b", re.IGNORECASE)

def find(word):
    def fun():
        return union.match(word)
    return fun

for exp in range(1, 6):
    print("\nTrieRegex of %d words" % 10**exp)
    union = trie_regex_from_words(banned_words[:10**exp])
    for description, test_word in test_words:
        time = timeit.timeit(find(test_word), number=1000) * 1000
        print("  %s : %.1fms" % (description, time))

It outputs:

TrieRegex of 10 words
  Surely not a word : 0.3ms
  First word : 0.4ms
  Last word : 0.5ms
  Almost a word : 0.5ms

TrieRegex of 100 words
  Surely not a word : 0.3ms
  First word : 0.5ms
  Last word : 0.9ms
  Almost a word : 0.6ms

TrieRegex of 1000 words
  Surely not a word : 0.3ms
  First word : 0.7ms
  Last word : 0.9ms
  Almost a word : 1.1ms

TrieRegex of 10000 words
  Surely not a word : 0.1ms
  First word : 1.0ms
  Last word : 1.2ms
  Almost a word : 1.2ms

TrieRegex of 100000 words
  Surely not a word : 0.3ms
  First word : 1.2ms
  Last word : 0.9ms
  Almost a word : 1.6ms

For info, the regex begins like this:

(?:a(?:(?:\'s|a(?:\'s|chen|liyah(?:\'s)?|r(?:dvark(?:(?:\'s|s))?|on))|b(?:\'s|a(?:c(?:us(?:(?:\'s|es))?|[ik])|ft|lone(?:(?:\'s|s))?|ndon(?:(?:ed|ing|ment(?:\'s)?|s))?|s(?:e(?:(?:ment(?:\'s)?|[ds]))?|h(?:(?:e[ds]|ing))?|ing)|t(?:e(?:(?:ment(?:\'s)?|[ds]))?|ing|toir(?:(?:\'s|s))?))|b(?:as(?:id)?|e(?:ss(?:(?:\'s|es))?|y(?:(?:\'s|s))?)|ot(?:(?:\'s|t(?:\'s)?|s))?|reviat(?:e[ds]?|i(?:ng|on(?:(?:\'s|s))?))|y(?:\'s)?|\é(?:(?:\'s|s))?)|d(?:icat(?:e[ds]?|i(?:ng|on(?:(?:\'s|s))?))|om(?:en(?:(?:\'s|s))?|inal)|u(?:ct(?:(?:ed|i(?:ng|on(?:(?:\'s|s))?)|or(?:(?:\'s|s))?|s))?|l(?:\'s)?))|e(?:(?:\'s|am|l(?:(?:\'s|ard|son(?:\'s)?))?|r(?:deen(?:\'s)?|nathy(?:\'s)?|ra(?:nt|tion(?:(?:\'s|s))?))|t(?:(?:t(?:e(?:r(?:(?:\'s|s))?|d)|ing|or(?:(?:\'s|s))?)|s))?|yance(?:\'s)?|d))?|hor(?:(?:r(?:e(?:n(?:ce(?:\'s)?|t)|d)|ing)|s))?|i(?:d(?:e[ds]?|ing|jan(?:\'s)?)|gail|l(?:ene|it(?:ies|y(?:\'s)?)))|j(?:ect(?:ly)?|ur(?:ation(?:(?:\'s|s))?|e[ds]?|ing))|l(?:a(?:tive(?:(?:\'s|s))?|ze)|e(?:(?:st|r))?|oom|ution(?:(?:\'s|s))?|y)|m\'s|n(?:e(?:gat(?:e[ds]?|i(?:ng|on(?:\'s)?))|r(?:\'s)?)|ormal(?:(?:it(?:ies|y(?:\'s)?)|ly))?)|o(?:ard|de(?:(?:\'s|s))?|li(?:sh(?:(?:e[ds]|ing))?|tion(?:(?:\'s|ist(?:(?:\'s|s))?))?)|mina(?:bl[ey]|t(?:e[ds]?|i(?:ng|on(?:(?:\'s|s))?)))|r(?:igin(?:al(?:(?:\'s|s))?|e(?:(?:\'s|s))?)|t(?:(?:ed|i(?:ng|on(?:(?:\'s|ist(?:(?:\'s|s))?|s))?|ve)|s))?)|u(?:nd(?:(?:ed|ing|s))?|t)|ve(?:(?:\'s|board))?)|r(?:a(?:cadabra(?:\'s)?|d(?:e[ds]?|ing)|ham(?:\'s)?|m(?:(?:\'s|s))?|si(?:on(?:(?:\'s|s))?|ve(?:(?:\'s|ly|ness(?:\'s)?|s))?))|east|idg(?:e(?:(?:ment(?:(?:\'s|s))?|[ds]))?|ing|ment(?:(?:\'s|s))?)|o(?:ad|gat(?:e[ds]?|i(?:ng|on(?:(?:\'s|s))?)))|upt(?:(?:e(?:st|r)|ly|ness(?:\'s)?))?)|s(?:alom|c(?:ess(?:(?:\'s|e[ds]|ing))?|issa(?:(?:\'s|[es]))?|ond(?:(?:ed|ing|s))?)|en(?:ce(?:(?:\'s|s))?|t(?:(?:e(?:e(?:(?:\'s|ism(?:\'s)?|s))?|d)|ing|ly|s))?)|inth(?:(?:\'s|e(?:\'s)?))?|o(?:l(?:ut(?:e(?:(?:\'s|ly|st?))?|i(?:on(?:\'s)?|sm(?:\'s)?))|v(?:e[ds]?|ing))|r(?:b(?:(?:e(?:n(?:cy(?:\'s)?|t(?:(?:\'s|s))?)|d)|ing|s))?|pti...

It's really unreadable, but for a list of 100000 banned words, this Trie regex is 1000 times faster than a simple regex union!

Here's a diagram of the complete trie, exported with trie-python-graphviz and graphviz twopi:

Enter image description here

Just a learner
  • 26,690
  • 50
  • 155
  • 234
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • Looks that for original purpose, there is no need for a non capturing group. At least the meaning of the non capturing group should be mentionned – Xavier Combelle Mar 15 '17 at 19:48
  • 4
    @XavierCombelle: You're right that I should mention the capturing group : the answer has been updated. I see it the other way around though : parens are needed for regex alternation with `|` but capturing groups aren't needed for our purpose at all. They'd just slow down the process and use more memory without benefit. – Eric Duminil Mar 15 '17 at 21:47
  • 4
    @EricDuminil This post is perfect, thank you so much :) – Mohamed AL ANI Apr 19 '18 at 16:21
  • @MohamedALANI: My pleasure! It was a thrill to write the code and see it perform so well. Please take a look at my other [answer](https://stackoverflow.com/questions/42742810/speed-up-millions-of-regex-replacements-in-python-3/42747503#42747503), which is even faster. – Eric Duminil Apr 19 '18 at 17:14
  • @EricDuminil Thanks what I'm actually trying to do is to find all words from a list (200,000 words and group of words, example : `["elephant", "black elephant", "tiger running in field"]`) that occur in a text sample. I used your trie with a `re.findall(trie.pattern(), text)`but it doesn't seem to speed up my code :( Any idea ? – Mohamed AL ANI Apr 19 '18 at 17:21
  • 1
    @MohamedALANI: Compared to which solution? – Eric Duminil Apr 19 '18 at 17:54
  • How would one also add banned prefixes to the trie? For example, any word that starts with the prefix 'bull' would be matched by regex. – Hyrial Jan 16 '19 at 02:48
  • Great job on the code snippets! I applied the regex union and the trie on processing ~50 million text segments with 70 banned words. The code with the regex union runs with ~7500 it/s; the trie outdoes it with ~12500 it/s – tharndt Aug 07 '19 at 14:18
  • @tharndt good to know, thanks. The relative difference would be even higher with more banned words. – Eric Duminil Aug 07 '19 at 18:42
  • One question the `union`will only target excat matching of the orginal word list, so if I have ['Apple', 'Banana'] and I use a replace afterwards, it will only hit: `apple, banana`and not `app, apple, ...` right? – PV8 Oct 29 '19 at 13:00
  • 1
    @PV8: It should only match complete words, yes, thanks to the `\b` ([word boundary](https://docs.python.org/3/library/re.html#regular-expression-syntax)). If the list is `['apple', 'banana']`, it will replace words which are exactly `apple` or `banana`, but not `nana`, `bana` or `pineapple`. – Eric Duminil Oct 29 '19 at 16:14
  • Do you have a list of words I can pump into [this tool](http://www.regexformat.com/version7_files/Rx5_ScrnSht01.jpg) to see how it matches up with what you get and to do a benchmark comparison using your output. If you could post a text of it somewhere I would appretiate it. –  Jan 03 '20 at 10:19
  • @xt15: That tool looks awesome, thanks for the tip. The dictionary I used comes from Debian packages. Here's the [list](https://download.ericduminil.com/american-english.txt), which I just copied from `/usr/share/dict/american-english` – Eric Duminil Jan 03 '20 at 10:38
  • Will this work if the words in banned_words themselves contain regular expressions? – elexhobby Apr 08 '20 at 17:10
  • @elexhobby: I don't know, I'll try. – Eric Duminil Apr 08 '20 at 18:22
  • @elexhobby: I just tried. It doesn't work yet, because `re.escape` is called on every input, so for example `'.'` is converted to `'\.'`. You can try to remove it but it might lead to weird behavior, especially with complex regexen. It *might* work with very basic examples, for example `'foo...'` and `'...bar'`. A more complex logic would be required for anything else. – Eric Duminil Apr 09 '20 at 12:40
167

TLDR

Use this method (with set lookup) if you want the fastest solution. For a dataset similar to the OP's, it's approximately 2000 times faster than the accepted answer.

If you insist on using a regex for lookup, use this trie-based version, which is still 1000 times faster than a regex union.

Theory

If your sentences aren't humongous strings, it's probably feasible to process many more than 50 per second.

If you save all the banned words into a set, it will be very fast to check if another word is included in that set.

Pack the logic into a function, give this function as argument to re.sub and you're done!

Code

import re
with open('/usr/share/dict/american-english') as wordbook:
    banned_words = set(word.strip().lower() for word in wordbook)


def delete_banned_words(matchobj):
    word = matchobj.group(0)
    if word.lower() in banned_words:
        return ""
    else:
        return word

sentences = ["I'm eric. Welcome here!", "Another boring sentence.",
             "GiraffeElephantBoat", "sfgsdg sdwerha aswertwe"] * 250000

word_pattern = re.compile('\w+')

for sentence in sentences:
    sentence = word_pattern.sub(delete_banned_words, sentence)

Converted sentences are:

' .  !
  .
GiraffeElephantBoat
sfgsdg sdwerha aswertwe

Note that:

  • the search is case-insensitive (thanks to lower())
  • replacing a word with "" might leave two spaces (as in your code)
  • With python3, \w+ also matches accented characters (e.g. "ångström").
  • Any non-word character (tab, space, newline, marks, ...) will stay untouched.

Performance

There are a million sentences, banned_words has almost 100000 words and the script runs in less than 7s.

In comparison, Liteye's answer needed 160s for 10 thousand sentences.

With n being the total amound of words and m the amount of banned words, OP's and Liteye's code are O(n*m).

In comparison, my code should run in O(n+m). Considering that there are many more sentences than banned words, the algorithm becomes O(n).

Regex union test

What's the complexity of a regex search with a '\b(word1|word2|...|wordN)\b' pattern? Is it O(N) or O(1)?

It's pretty hard to grasp the way the regex engine works, so let's write a simple test.

This code extracts 10**i random english words into a list. It creates the corresponding regex union, and tests it with different words :

  • one is clearly not a word (it begins with #)
  • one is the first word in the list
  • one is the last word in the list
  • one looks like a word but isn't


import re
import timeit
import random

with open('/usr/share/dict/american-english') as wordbook:
    english_words = [word.strip().lower() for word in wordbook]
    random.shuffle(english_words)

print("First 10 words :")
print(english_words[:10])

test_words = [
    ("Surely not a word", "#surely_NöTäWORD_so_regex_engine_can_return_fast"),
    ("First word", english_words[0]),
    ("Last word", english_words[-1]),
    ("Almost a word", "couldbeaword")
]


def find(word):
    def fun():
        return union.match(word)
    return fun

for exp in range(1, 6):
    print("\nUnion of %d words" % 10**exp)
    union = re.compile(r"\b(%s)\b" % '|'.join(english_words[:10**exp]))
    for description, test_word in test_words:
        time = timeit.timeit(find(test_word), number=1000) * 1000
        print("  %-17s : %.1fms" % (description, time))

It outputs:

First 10 words :
["geritol's", "sunstroke's", 'fib', 'fergus', 'charms', 'canning', 'supervisor', 'fallaciously', "heritage's", 'pastime']

Union of 10 words
  Surely not a word : 0.7ms
  First word        : 0.8ms
  Last word         : 0.7ms
  Almost a word     : 0.7ms

Union of 100 words
  Surely not a word : 0.7ms
  First word        : 1.1ms
  Last word         : 1.2ms
  Almost a word     : 1.2ms

Union of 1000 words
  Surely not a word : 0.7ms
  First word        : 0.8ms
  Last word         : 9.6ms
  Almost a word     : 10.1ms

Union of 10000 words
  Surely not a word : 1.4ms
  First word        : 1.8ms
  Last word         : 96.3ms
  Almost a word     : 116.6ms

Union of 100000 words
  Surely not a word : 0.7ms
  First word        : 0.8ms
  Last word         : 1227.1ms
  Almost a word     : 1404.1ms

So it looks like the search for a single word with a '\b(word1|word2|...|wordN)\b' pattern has:

  • O(1) best case
  • O(n/2) average case, which is still O(n)
  • O(n) worst case

These results are consistent with a simple loop search.

A much faster alternative to a regex union is to create the regex pattern from a trie.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 1
    You were correct. My indentation was wrong. I fixed it in the original question. As for the comment that 50 sentences / second is slow, all I can say is that I am providing a simplified example. The real data-set is more complicated than I am describing, but it didn't seem relevant. Also, concatenation of my "words" into a single regex massively improved the speed. Also, I am "squeezing" out double-spaces after the replacements. – pdanese Mar 12 '17 at 12:45
  • 1
    @user36476 Thanks for the feedback, I removed the corresponding part. Could you please try my suggestion? I dare say it's much faster than the accepted answer. – Eric Duminil Mar 12 '17 at 13:04
  • @user36476: I've seen your comment update now, and changed the intro. – Eric Duminil Mar 12 '17 at 13:06
  • 3
    Since you removed that misleading `O(1)` claim, your answer definitely deserves an up vote. – idmean Mar 12 '17 at 16:16
  • 2
    @idmean: True, that wasn't very clear. It was just referring to the lookup : "Is this word a banned word?". – Eric Duminil Mar 12 '17 at 16:47
  • @EricDuminil: Liteye's solution is only O(n*m) if the regular expression engine implements the look-up of the word as O(m). It hopefully optimizes it, though it may not end being as efficient as a look-up table. – Matthieu M. Mar 13 '17 at 07:48
  • @MatthieuM. I agree on the "hopefully". I searched for a while, I couldn't find any indication that `"word1|word2|word3"` is optimized to `"word[123]"` for example. – Eric Duminil Mar 13 '17 at 08:16
  • @EricDuminil: It's the kind of things that's really internal and hardly documented unfortunately, so short of reading the code or asking someone who did I am afraid we'll never know :( – Matthieu M. Mar 13 '17 at 08:23
  • @MatthieuM. We can guess, though. I'll do some test with growing regexen, and I'll trie to build a regex trie out of the union to see if it speeds things up. – Eric Duminil Mar 13 '17 at 08:26
  • @MatthieuM. See my update. Regexp union lookup does appear to be `O(m)` – Eric Duminil Mar 13 '17 at 10:35
140

One thing you can try is to compile one single pattern like "\b(word1|word2|word3)\b".

Because re relies on C code to do the actual matching, the savings can be dramatic.

As @pvg pointed out in the comments, it also benefits from single pass matching.

If your words are not regex, Eric's answer is faster.

Community
  • 1
  • 1
Liteye
  • 2,761
  • 1
  • 16
  • 16
  • I think this is probably a more elegant implementation of my suggestion. I'm not 100% sure about the regex implementation but I think that will step through the string until it finds a boundary, and then check for each of the words at that point. So it only has to look for boundaries once. In addition I guess the C code might encode the words as a data structure with fast lookups such as a trie? – Denziloe Mar 12 '17 at 02:09
  • I don't think it uses trie because there can be more complicated patterns. However this definitely uses loop in C instead of loop in Python, which is good for performance. – Liteye Mar 12 '17 at 02:10
  • Good point, thanks. @user36476 please let us know how it performs with this code, I am interested. – Denziloe Mar 12 '17 at 02:12
  • 4
    It's not just the C impl (which makes a big difference) but you're also matching with a single pass. Variants of this question come up pretty often, it's a little odd there isn't (or maybe there is, hiding somewhere?) a canonical SO answer for it with this pretty sensible idea. – pvg Mar 12 '17 at 02:40
  • @Liteye I will give this a shot. Thank you. – pdanese Mar 12 '17 at 02:56
  • 48
    @Liteye your suggestion turned a 4-hour job into a 4-minute job! I was able to join all 20,000+ regexes into a single gigantic regex and my laptop didn't bat an eye. Thanks again. – pdanese Mar 12 '17 at 03:43
  • @user36476 Regex are *not* just a shortcut for writing loops. They actually use some clever algorithms to search way faster than what you could possibly do by using an explicit loop with one pattern at a time. – Bakuriu Mar 12 '17 at 07:51
  • 2
    @Bakuriu: `s/They actually use/They actually could in theory sometimes use/`. Do you have any reason to believe Python's implementation is doing anything other than a loop here? – user541686 Mar 12 '17 at 09:51
  • @Mehrdad Yes. Sure, it's still a backtracking engine so there are some cases where it takes a lot of time, but I'm pretty sure the simple cases like a disjunction of literal regexes should take O(n+m) time where `n` is the length of the text to be matched and `m` the length of the pattern. A plain naive loop is O(n·m). The regex the OP is uses shouldn't need any backtracking, hence I expect the regex to take linear time. – Bakuriu Mar 12 '17 at 10:09
  • 2
    @Bakuriu: I'd be really interested to know if that's the case, but I don't think the regex solution takes linear time. If it doesn't build a Trie out of the union, I don't see how it could happen. – Eric Duminil Mar 12 '17 at 16:36
  • @Liteye: Thanks for the link to my answer, that's very fair of you! – Eric Duminil Mar 12 '17 at 16:43
  • 2
    @Bakuriu: That's not a reason. I was asking if you have a reason to believe the implementation **actually** behaves that way, not whether you have a reason to believe it *could* behave that way. Personally I have yet to come across a single mainstream programming language's regex implementation that works in linear time the same way you'd expect a classical regex to, so if you know Python does this, you should show some evidence. – user541686 Mar 13 '17 at 03:30
  • 2
    Just for fun, I wrote an [answer](http://stackoverflow.com/a/42789508/6419007) with a Trie Regexp. It's approximately 1000 times faster than a regexp union. – Eric Duminil Mar 14 '17 at 15:04
  • @pvg: What do you mean with "single pass" exactly? – Eric Duminil Mar 16 '17 at 10:50
  • I guess it is building a precompiled Finite State Machine/Transducer in C. That should be fast to run. It only takes as many steps as the input string is long. – Radio Controlled Oct 24 '19 at 11:11
  • 1
    This answer should not be accepted. Eric's regex trie answer must be accepted. – Wiktor Stribiżew Sep 07 '21 at 08:05
17

One thing you might want to try is pre-processing the sentences to encode the word boundaries. Basically turn each sentence into a list of words by splitting on word boundaries.

This should be faster, because to process a sentence, you just have to step through each of the words and check if it's a match.

Currently the regex search is having to go through the entire string again each time, looking for word boundaries, and then "discarding" the result of this work before the next pass.

Denziloe
  • 7,473
  • 3
  • 24
  • 34
14

Well, here's a quick and easy solution, with test set.

Best strategy:

  • re.sub("\w+",repl,sentence) searches for words.
  • "repl" can be a callable. I used a function that performs a dict lookup, and the dict contains the words to search and replace.
  • This is the simplest and fastest solution (see function replace4 in example code below).

Second best strategy:

  • The idea is to split the sentences into words, using re.split, while conserving the separators to reconstruct the sentences later. Then, replacements are done with a simple dict lookup.
  • Implementation: (see function replace3 in example code below).

Timings for example functions:

replace1:     0.62 sentences/s
replace2:     7.43 sentences/s
replace3: 48498.03 sentences/s
replace4: 61374.97 sentences/s (...and 240,000/s with PyPy)

...and code:

#! /bin/env python3
# -*- coding: utf-8

import time, random, re

def replace1( sentences ):
    for n, sentence in enumerate( sentences ):
        for search, repl in patterns:
            sentence = re.sub( "\\b"+search+"\\b", repl, sentence )

def replace2( sentences ):
    for n, sentence in enumerate( sentences ):
        for search, repl in patterns_comp:
            sentence = re.sub( search, repl, sentence )

def replace3( sentences ):
    pd = patterns_dict.get
    for n, sentence in enumerate( sentences ):
        #~ print( n, sentence )
        # Split the sentence on non-word characters.
        # Note: () in split patterns ensure the non-word characters ARE kept
        # and returned in the result list, so we don't mangle the sentence.
        # If ALL separators are spaces, use string.split instead or something.
        # Example:
        #~ >>> re.split(r"([^\w]+)", "ab céé? . d2eéf")
        #~ ['ab', ' ', 'céé', '? . ', 'd2eéf']
        words = re.split(r"([^\w]+)", sentence)
        
        # and... done.
        sentence = "".join( pd(w,w) for w in words )

        #~ print( n, sentence )

def replace4( sentences ):
    pd = patterns_dict.get
    def repl(m):
        w = m.group()
        return pd(w,w)
    
    for n, sentence in enumerate( sentences ):
        sentence = re.sub(r"\w+", repl, sentence)



# Build test set
test_words = [ ("word%d" % _) for _ in range(50000) ]
test_sentences = [ " ".join( random.sample( test_words, 10 )) for _ in range(1000) ]

# Create search and replace patterns
patterns = [ (("word%d" % _), ("repl%d" % _)) for _ in range(20000) ]
patterns_dict = dict( patterns )
patterns_comp = [ (re.compile("\\b"+search+"\\b"), repl) for search, repl in patterns ]


def test( func, num ):
    t = time.time()
    func( test_sentences[:num] )
    print( "%30s: %.02f sentences/s" % (func.__name__, num/(time.time()-t)))

print( "Sentences", len(test_sentences) )
print( "Words    ", len(test_words) )

test( replace1, 1 )
test( replace2, 10 )
test( replace3, 1000 )
test( replace4, 1000 )

EDIT: You can also ignore lowercase when checking if you pass a lowercase list of Sentences and edit repl

def replace4( sentences ):
pd = patterns_dict.get
def repl(m):
    w = m.group()
    return pd(w.lower(),w)
smci
  • 32,567
  • 20
  • 113
  • 146
bobflux
  • 11,123
  • 3
  • 27
  • 27
  • 1
    Upvote for the tests. `replace4` and my code have similar performances. – Eric Duminil Mar 12 '17 at 21:40
  • 1
    Not sure what def `repl(m):` is doing and how you are assigning `m` in the function replace4 – StatguyUser Jun 03 '17 at 14:56
  • Also i am getting error `error: unbalanced parenthesis` for the line `patterns_comp = [ (re.compile("\\b"+search+"\\b"), repl) for search, repl in patterns ]` – StatguyUser Jun 03 '17 at 16:05
  • While replace3 and replace4 function addresses the original issue (to replace words), replace1 and replace2 are more general-purpose, as those work even if the needle is a phrase (a sequence of words) and not just a single word. – Zoltan Fedor Oct 08 '18 at 14:33
7

Perhaps Python is not the right tool here. Here is one with the Unix toolchain

sed G file         |
tr ' ' '\n'        |
grep -vf blacklist |
awk -v RS= -v OFS=' ' '{$1=$1}1'

assuming your blacklist file is preprocessed with the word boundaries added. The steps are: convert the file to double spaced, split each sentence to one word per line, mass delete the blacklist words from the file, and merge back the lines.

This should run at least an order of magnitude faster.

For preprocessing the blacklist file from words (one word per line)

sed 's/.*/\\b&\\b/' words > blacklist
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
karakfa
  • 66,216
  • 7
  • 41
  • 56
5

How about this:

#!/usr/bin/env python3

from __future__ import unicode_literals, print_function
import re
import time
import io

def replace_sentences_1(sentences, banned_words):
    # faster on CPython, but does not use \b as the word separator
    # so result is slightly different than replace_sentences_2()
    def filter_sentence(sentence):
        words = WORD_SPLITTER.split(sentence)
        words_iter = iter(words)
        for word in words_iter:
            norm_word = word.lower()
            if norm_word not in banned_words:
                yield word
            yield next(words_iter) # yield the word separator

    WORD_SPLITTER = re.compile(r'(\W+)')
    banned_words = set(banned_words)
    for sentence in sentences:
        yield ''.join(filter_sentence(sentence))


def replace_sentences_2(sentences, banned_words):
    # slower on CPython, uses \b as separator
    def filter_sentence(sentence):
        boundaries = WORD_BOUNDARY.finditer(sentence)
        current_boundary = 0
        while True:
            last_word_boundary, current_boundary = current_boundary, next(boundaries).start()
            yield sentence[last_word_boundary:current_boundary] # yield the separators
            last_word_boundary, current_boundary = current_boundary, next(boundaries).start()
            word = sentence[last_word_boundary:current_boundary]
            norm_word = word.lower()
            if norm_word not in banned_words:
                yield word

    WORD_BOUNDARY = re.compile(r'\b')
    banned_words = set(banned_words)
    for sentence in sentences:
        yield ''.join(filter_sentence(sentence))


corpus = io.open('corpus2.txt').read()
banned_words = [l.lower() for l in open('banned_words.txt').read().splitlines()]
sentences = corpus.split('. ')
output = io.open('output.txt', 'wb')
print('number of sentences:', len(sentences))
start = time.time()
for sentence in replace_sentences_1(sentences, banned_words):
    output.write(sentence.encode('utf-8'))
    output.write(b' .')
print('time:', time.time() - start)

These solutions splits on word boundaries and looks up each word in a set. They should be faster than re.sub of word alternates (Liteyes' solution) as these solutions are O(n) where n is the size of the input due to the amortized O(1) set lookup, while using regex alternates would cause the regex engine to have to check for word matches on every characters rather than just on word boundaries. My solutiona take extra care to preserve the whitespaces that was used in the original text (i.e. it doesn't compress whitespaces and preserves tabs, newlines, and other whitespace characters), but if you decide that you don't care about it, it should be fairly straightforward to remove them from the output.

I tested on corpus.txt, which is a concatenation of multiple eBooks downloaded from the Gutenberg Project, and banned_words.txt is 20000 words randomly picked from Ubuntu's wordlist (/usr/share/dict/american-english). It takes around 30 seconds to process 862462 sentences (and half of that on PyPy). I've defined sentences as anything separated by ". ".

$ # replace_sentences_1()
$ python3 filter_words.py 
number of sentences: 862462
time: 24.46173644065857
$ pypy filter_words.py 
number of sentences: 862462
time: 15.9370770454

$ # replace_sentences_2()
$ python3 filter_words.py 
number of sentences: 862462
time: 40.2742919921875
$ pypy filter_words.py 
number of sentences: 862462
time: 13.1190629005

PyPy particularly benefit more from the second approach, while CPython fared better on the first approach. The above code should work on both Python 2 and 3.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • Python 3 is a given in the question. I upvoted this but I think it might be worth sacrificing some of the detail and 'optimal' implementation in this code to make it less verbose. – pvg Mar 12 '17 at 16:00
  • If I understand it correctly, it's basically the same principle as my answer, but more verbose? Splitting and joining on `\W+` is basically like `sub` on `\w+`, right? – Eric Duminil Mar 12 '17 at 16:03
  • I wonder if my solution below (function replace4) is faster than pypy ;) I'd like to test on your files! – bobflux Mar 12 '17 at 20:06
3

Practical approach

A solution described below uses a lot of memory to store all the text at the same string and to reduce complexity level. If RAM is an issue think twice before use it.

With join/split tricks you can avoid loops at all which should speed up the algorithm.

  • Concatenate a sentences with a special delimeter which is not contained by the sentences:
  • merged_sentences = ' * '.join(sentences)
    

  • Compile a single regex for all the words you need to rid from the sentences using | "or" regex statement:
  • regex = re.compile(r'\b({})\b'.format('|'.join(words)), re.I) # re.I is a case insensitive flag
    

  • Subscript the words with the compiled regex and split it by the special delimiter character back to separated sentences:
  • clean_sentences = re.sub(regex, "", merged_sentences).split(' * ')
    

    Performance

    "".join complexity is O(n). This is pretty intuitive but anyway there is a shortened quotation from a source:

    for (i = 0; i < seqlen; i++) {
        [...]
        sz += PyUnicode_GET_LENGTH(item);
    

    Therefore with join/split you have O(words) + 2*O(sentences) which is still linear complexity vs 2*O(N2) with the initial approach.


    b.t.w. don't use multithreading. GIL will block each operation because your task is strictly CPU bound so GIL have no chance to be released but each thread will send ticks concurrently which cause extra effort and even lead operation to infinity.

    I159
    • 29,741
    • 31
    • 97
    • 132
    • In case the sentences are (were) stored in a text file, they are already separated by a newline. So the whole file could be read in as one big string (or buffer), words removed, and then written back again (or this could be done in the file directly using memory mapping). Otoh, to remove a word, the remainder of the string has to be moved back to fill the gap, so that would be a problem with one very large string. An alternative would be to write the parts between the words back to another string or file (which would include the newlines) – or just move those parts in a mmapped file (1) .. – Danny_ds Mar 14 '17 at 11:31
    • .. That last approach (moving/writing the parts between the words) combined with [Eric Duminil’s set lookup](http://stackoverflow.com/a/42747503/5708620) could be really fast, perhaps without even using regex at all. (2) – Danny_ds Mar 14 '17 at 11:31
    • .. Or maybe regex is already optimized to only move those parts when replacing multiple words, I don't know. – Danny_ds Mar 14 '17 at 11:37
    1

    Concatenate all your sentences into one document. Use any implementation of the Aho-Corasick algorithm (here's one) to locate all your "bad" words. Traverse the file, replacing each bad word, updating the offsets of found words that follow etc.

    Edi Bice
    • 566
    • 6
    • 18
    1

    Use the LRU Cache! I got a 60-fold speed-up for regex searches in a production environment. Caching (aka, memoization) works by saving the results of expensive computations, so you'll get the best speed up if there's duplication in your data set (which there likely is).

    import re
    from functools import lru_cache
    
    @lru_cache
    def expensive_regex(str):
       return re.findall('pattern', str)
    

    Check out: 127x Regex Speed Up in 2 Lines of Code, written by yours truly :)