QuantumMatcher
library is a fuzzy matching algorithm that leverages bitwise operations to efficiently find approximate matches within a collection of items.
Project in progress ...
-
Node.js >= 18.x
-
Yarn >= 1.x
- Fuzzy Matching: Efficiently finds approximate matches in a collection.
- Customizable Keys: Specify which keys of the items to match against.
- Match Quality Calculation: Considers match ratio, contiguity, position, and partial matches.
- Sorted Results: Returns results sorted by match score.
The QuantumMatcher
class uses a bitwise algorithm to perform fuzzy matching. Here's a breakdown of the process:
-
Normalization:
- The query string is split into parts and normalized using the
normalizeText
function to standardize the text for comparison.
- The query string is split into parts and normalized using the
-
Character Mask Creation:
- A character mask is created for each query part using the
createCharMask
method. This mask maps each character to a bitmask representing its positions in the string.
- A character mask is created for each query part using the
-
Bitwise Matching:
- Bitwise operations are used to calculate matches. Bit vectors
VP
,HN
, andHP
track matches and mismatches. - For each character in the normalized text, the algorithm updates the bit vectors based on the character mask.
- Bitwise operations are used to calculate matches. Bit vectors
-
Score Calculation:
- The
calculateMatchQuality
method computes match quality using:- Match Ratio: Ratio of matched characters to query length.
- Contiguity: Checks if matches are contiguous.
- Position Bonus: Rewards matches closer to the start of the text.
- Partial Match Bonus: Rewards partial matches if the query is found in the text.
- The
-
Result Filtering and Sorting:
- Results with a significant score (average score > 0.5 per query part) are included.
- Results are sorted by score in descending order and filtered to include only perfect matches (score of 1).
Ensure TypeScript is installed in your project. You can install it using npm:
npm install quantummatcher
# or
yarn add quantummatcher
npm run start
# or
yarn start
constructor(collection: T[], private options: { keys?: (keyof T)[] })
The QuantumMatcher class constructor takes two parameters:
- Collection : An array of items to search through. Each item should be an object with properties that can be matched against.
- Options : An object specifying the keys to match against. The keys property is an array of strings representing the keys in the items that should be considered for matching.
import { QuantumMatcher } from 'quantummatcher'
// Define a collection of items
const options = [
{
id: 1,
title: 'Pythagorean Theorem',
description:
'In mathematics, the Pythagorean theorem states that in a right-angled triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides.',
tags: ['math', 'geometry', 'theorem'],
},
// ... more items ...
]
// Create an instance of QuantumMatcher
const matcher = new QuantumMatcher(options, {
keys: ['title', 'description', 'tags'],
})
// Find matches for a query
const output = matcher.findMatches('numbers')
console.log(output)
[
{
item: {
id: 5,
title: 'Random String Example',
description: 'This is a random string that can be used for testing search functionality. It includes special characters like @#$%^&* and numbers like 12345.',
tags: [Array]
},
score: 1,
matches: [ [Array] ]
},
{
item: {
id: 7,
title: 'Fibonacci Sequence',
description: 'The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.',
tags: [Array]
},
score: 1,
matches: [ [Array] ]
},
{
item: {
id: 9,
title: 'Another Random String',
description: 'This is another random string for testing purposes. It includes mixed case letters, numbers 67890, and symbols !@#$%^&*().',
tags: [Array]
},
score: 1,
matches: [ [Array] ]
}
]
...
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
- Chris Michael (Project Leader, and Developer)
This is just a personal project created for study / demonstration purpose and to simplify my working life, it may or may not be a good fit for your project(s).
Please ⭐ this repository if you like it or this project helped you!
Feel free to open issues or submit pull-requests to help me improving my work.
Chris M. Perez
Copyright ©2025 QuantumMatcher.