SoftMatcha: A Soft and Fast Pattern Matcher for Billion-Scale Corpus Searches

Hiroyuki Deguchi, Go Kamoda, Yusuke Matsushita, Chihiro Taguchi, Kohei Suenaga, Masaki Waga, Sho Yokoi

Searching through massive text collections has long forced a trade-off between speed and intelligence. On one hand, "hard" pattern matchers like grep are blazingly fast but brittle, finding only exact character matches. On the other hand, dense vector search understands semantic meaning but is often too "coarse," returning entire documents that share a topic rather than the specific linguistic phrases you are looking for.

A new approach, "SoftMatcha: A Soft and Fast Pattern Matcher for Billion-Scale Corpus Searches," introduces a powerful middle ground. It allows researchers to perform "soft" (semantic) pattern matching across billions of words in under a second, entirely without the need for a GPU.

The Challenge of "Wild" Text Data

Linguists and NLP researchers frequently need to find specific language patterns in real-world data. However, natural language is inherently messy. If you search for the phrase "buy a car," an exact matcher will completely miss valid variations like "purchased a vehicle" or "bought an automobile."

Existing solutions fall short in distinct ways:

  1. Exact Matchers (grep, KWIC): These are fast but fail on paraphrases, orthographic variations (typos or alternative spellings), and complex morphological inflections (which are especially common in languages like Latin or Finnish).
  2. Dense Vector Search (RAG-style): While excellent at finding thematic "aboutness," dense retrieval struggles to locate specific n-gram patterns. A query might return a whole article about car sales when the user specifically wanted to analyze instances of a three-word verb phrase.

The core challenge is creating a tool that is soft (understands semantic similarity), precise (finds the exact n-gram structure), and scalable (works efficiently on the massive datasets used to train modern LLMs).

Methods: The SoftMatcha Algorithm

SoftMatcha's core innovation lies in how it distributes computational load. Instead of comparing a query against every word in a billion-token corpus—which would be prohibitively slow—it performs its "soft" semantic computation exclusively on the vocabulary.

The algorithm operates in four main phases:

  1. Preprocessing: The system builds a standard inverted index of the corpus, mapping every unique vocabulary word to its exact positions in the text.
  2. Step 1: Soften the Pattern: For each word in the user's query, the algorithm uses cosine similarity of pre-trained word embeddings (e.g., GloVe or fastText) to find all semantically similar words in the vocabulary.
  3. Step 2: Soft Inverted Index: It then creates a "soft" inverted list by taking the union of the position lists for all of those similar words.
  4. Step 3: Shifting Intersection: Finally, it performs a "hard" intersection of these soft lists—shifting them to account for word order—to find where the full "soft" phrase appears in the text.
The SoftMatcha matching pipeline: softening the pattern via word embeddings, merging inverted lists, and performing shifting intersection to find soft matches.

By moving the expensive vector math to the vocabulary level (typically thousands of words) and leaving the "heavy lifting" of the billion-scale search to efficient bitwise operations on indexes, SoftMatcha remains incredibly fast.

Key Insights and Advantages

  • Vocabulary-First Softening: The most significant insight is that semantic similarity doesn't need to be calculated against the corpus text itself. By calculating it against the vocabulary and then leveraging the inverted index, semantic search is achieved almost for "free" at search time.
  • Inverted Index over Suffix Arrays: While suffix arrays are popular for exact matching, they are notoriously difficult to "soften." The authors demonstrate that inverted indexes are much more naturally suited for merging sets of similar words.
  • Precision over Coarseness: Unlike dense retrieval, which often uses "top-k" document retrieval, SoftMatcha is an enumeration tool. It finds every instance of the pattern, making it highly valuable for rigorous scientific and linguistic analysis.

Experimental Results

The authors evaluated SoftMatcha across diverse linguistic contexts, including massive Wikipedia-based datasets in English (3.4 billion words) and Japanese (1.1 billion words), as well as Latin corpora (Perseus Project and ASP) to test complex morphological inflections.

  • Extreme Speed: On the 3.4-billion-word English corpus, SoftMatcha executed searches in less than 0.1 seconds on standard CPUs. This speed is comparable to exact matching and significantly faster than dense vector search.
  • Higher Recall: In a search for "homemade bombs," exact matching found only 107 hits. SoftMatcha found 1,473 hits, successfully capturing semantic variations like "home-made grenades" and "homemade missiles."
  • Morphological Mastery: In Latin, the tool successfully matched the query "factus est" (he/it was done) with variations like "facta sunt" (they are done) and "mortuus esset" (he was dead). It identified shared semantic and morphological roots that exact matchers would completely ignore.
  • IR Performance: A "Soft-BM25" implementation utilizing SoftMatcha outperformed standard BM25 on the TREC-COVID dataset, proving its utility for broader ranking tasks.

Limitations and Future Work

While powerful, SoftMatcha has a few current limitations:

  • Out-of-Vocabulary (OOV): The current version ignores words not present in the pre-trained embedding space. Future iterations may use character-level embeddings to mitigate this.
  • Static Embeddings: The tool relies on static embeddings (like GloVe). Incorporating dynamic, contextual embeddings (like BERT) into a fast, searchable index remains an open challenge.
  • Structural Flexibility: The tool currently expects a fixed number of words and does not handle word omissions or insertions (e.g., matching "buy a new car" when searching for "buy a car"). The authors propose using wildcards to solve this in the future.

By bridging the gap between the speed of exact matching and the intelligence of semantic search, SoftMatcha offers a highly promising new resource for anyone navigating the billion-scale haystacks of modern text data.

Summary updated

BibTeX
@inproceedings{deguchi2025softmatcha,
  author = {Hiroyuki Deguchi and Go Kamoda and Yusuke Matsushita and Chihiro Taguchi and Kohei Suenaga and Masaki Waga and Sho Yokoi},
  title = {{SoftMatcha: A Soft and Fast Pattern Matcher for Billion-Scale Corpus Searches}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=Q6PAnqYVpo},
}
Figure