TL;DR
SoftMatcha is an open-source tool that enables semantic "soft" matching across billion-scale text corpora with the speed of traditional exact-match tools like grep. By combining word embeddings with optimized inverted indexing, it allows researchers to find paraphrases and morphological variations (e.g., "Theorem 1" matching "Lemma 5") in less than 0.1 seconds across billions of words.
Abstract
Researchers and practitioners in natural language processing and computational linguistics frequently observe and analyze the real language usage in large-scale corpora. For that purpose, they often employ off-the-shelf pattern-matching tools, such as grep, and keyword-in-context concordancers. Nonetheless, these existing techniques rely on surface-level string matching, and thus they suffer from the major limitation of not being able to handle orthographic variations and paraphrasing. In addition, existing continuous approaches such as dense vector search tend to be overly coarse, often retrieving texts that share similar topics but lack specific pattern matches. We propose a novel algorithm that achieves soft (semantic) yet efficient pattern matching by relaxing surface-level matching with word embeddings. Our algorithm is highly scalable utilizing inverted indexes. Experiments demonstrate that SoftMatcha (i) executes searches on billion-scale corpora in less than 0.1 seconds; (ii) extracts harmful instances that semantically match queries; and (iii) effectively analyzes languages with complex inflections like Latin.
Introduction
In NLP and linguistics, researchers often need to find specific patterns in massive datasets. Traditionally, they have faced a trade-off between the speed of Hard Pattern Matching (e.g., grep), which misses semantic variations, and the flexibility of Dense Vector Search, which is often too coarse for specific $n$-gram matching and computationally expensive at scale. SoftMatcha fills this gap by providing a tool as precise as a string matcher but as semantically aware as a neural search engine.
Methodology
SoftMatcha employs a two-step algorithm that shifts semantic computation from the massive corpus to the much smaller vocabulary.
Preprocessing
The system builds a standard inverted index, mapping every unique word in the corpus to its exact positions. This allows for rapid lookup during the search phase.
Step 1: Softening the Pattern
When a user enters a query (e.g., "the jazz musician"), the algorithm uses word embeddings (like GloVe or fastText) to find words in the vocabulary that are semantically similar to each query term. For "musician," it might identify "singer" or "pianist."
Step 2: Hard Matching (Shifting Intersection)
The tool treats the "soft" query as a set of "hard" possibilities. It retrieves positions for all similar words from the inverted index and performs a "shifting intersection" to find where these words appear in the correct sequence. The implementation uses CSR (Compressed Sparse Row) matrices and Numba for near-native execution speeds.
Evaluation and Results
SoftMatcha was evaluated on several diverse datasets, including the LLM-jp Corpus v2.0 (3.4B English words, 1.1B Japanese words) and various Latin Corpora.
Key Results
- Billion-Scale Speed: On 3.4 billion words, SoftMatcha executed searches in less than 0.1 seconds on a standard CPU, outperforming dense vector search (~0.389s).
- Safety Auditing: Searching for "homemade bombs" successfully retrieved "home-made grenades" and "homemade missiles," which exact matching missed.
- Linguistic Robustness: The tool is particularly powerful for inflected languages like Latin, where a single verb can have over 100 forms; SoftMatcha catches these variations naturally through the embedding space.
Discussion and Conclusion
SoftMatcha demonstrates that semantic similarity can be efficiently handled as a property of the vocabulary. By relaxing the query first, it leverages optimized inverted indexing to achieve high speed without GPU-heavy vector databases. While currently limited by static embeddings and specific word ordering, future work will explore context-aware embeddings and flexible word orders with wildcards.
SoftMatcha is available as an open-source tool and web-based demo.
Summary updated