Skip to main content
QUICK REVIEW

[Paper Review] Replace or Retrieve Keywords In Documents at Scale

Vikash Singh|arXiv (Cornell University)|Oct 31, 2017
Algorithms and Data CompressionComputer Science4 references29 citations
TL;DR

This paper introduces FlashText, a fast, linear-time algorithm for scalable keyword search and replacement in large text documents using a trie-based data structure. Unlike regex, which scales poorly with large keyword sets, FlashText achieves O(N) complexity regardless of the number of keywords, outperforming regex by up to 82x when processing 15,000 keywords in a single document.

ABSTRACT

In this paper we introduce, the FlashText algorithm for replacing keywords or finding keywords in a given text. FlashText can search or replace keywords in one pass over a document. The time complexity of this algorithm is not dependent on the number of terms being searched or replaced. For a document of size N (characters) and a dictionary of M keywords, the time complexity will be O(N). This algorithm is much faster than Regex, because regex time complexity is O(MxN). It is also different from Aho Corasick Algorithm, as it doesn't match substrings. FlashText is designed to only match complete words (words with boundary characters on both sides). For an input dictionary of {Apple}, this algorithm won't match it to 'I like Pineapple'. This algorithm is also designed to go for the longest match first. For an input dictionary {Machine, Learning, Machine learning} on a string 'I like Machine learning', it will only consider the longest match, which is Machine Learning. We have made python implementation of this algorithm available as open-source on GitHub, released under the permissive MIT License.

Motivation & Objective

  • To address the performance bottleneck of regex-based keyword search and replacement when handling large-scale keyword dictionaries (e.g., 10,000+ terms).
  • To design a solution that scales linearly with document size, independent of the number of keywords.
  • To provide a practical, open-source tool for real-world information retrieval workloads such as resume parsing and synonym normalization.
  • To benchmark FlashText against regex across varying keyword set sizes and document lengths to quantify performance gains.

Proposed method

  • Build a trie dictionary from input keywords, where each node represents a character and the end-of-term marker (eot) stores the standardized replacement.
  • Use word boundary markers (\b) to ensure only complete words are matched, avoiding partial substring matches (e.g., 'Apple' not matching 'Pineapple').
  • Perform a single-pass character-by-character scan of the input document, traversing the trie to detect complete keyword matches.
  • For keyword search, collect all matched terms with their standardized forms; for replacement, substitute matched terms with their standardized equivalents in the output.
  • Prioritize the longest match first (e.g., 'Machine Learning' over 'Machine' or 'Learning') to avoid ambiguous or overlapping replacements.
  • Implement the algorithm in Python and release it under the MIT license for open-source use.

Experimental results

Research questions

  • RQ1How does FlashText’s performance compare to regex when searching for a large number of keywords in a single document?
  • RQ2Can FlashText maintain constant-time performance as the number of keywords increases, unlike regex which scales linearly with keyword count?
  • RQ3Does FlashText effectively handle longest-match priority and word boundary constraints without false positives?
  • RQ4What is the real-world performance gain of FlashText over regex in large-scale document processing pipelines?
  • RQ5Can FlashText be efficiently used for both keyword extraction and replacement in information retrieval workloads?

Key findings

  • FlashText reduces search time for 15,000 keywords in a 10,000-word document from 0.165 seconds (regex) to 0.002 seconds, achieving an 82x speedup.
  • FlashText’s time complexity is O(N), where N is the document length, and is independent of the number of keywords, making it highly scalable.
  • Regex performance grows linearly with the number of keywords, while FlashText maintains near-constant runtime regardless of keyword set size.
  • The algorithm correctly handles word boundaries and ensures only complete words are matched, preventing false positives like 'Apple' in 'Pineapple'.
  • For keyword replacement, FlashText outperforms regex by up to 82x on the same benchmark, with consistent performance across increasing keyword sets.
  • The open-source FlashText implementation enables efficient, production-ready keyword processing in applications like resume parsing, synonym normalization, and data deduplication.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.