| Crates.io | terraphim_automata |
| lib.rs | terraphim_automata |
| version | 1.5.2 |
| created_at | 2024-04-30 17:55:14.199073+00 |
| updated_at | 2026-01-20 10:45:49.512952+00 |
| description | Automata for searching and processing knowledge graphs |
| homepage | https://terraphim.ai |
| repository | https://github.com/terraphim/terraphim-ai |
| max_upload_size | |
| id | 1225352 |
| size | 199,260 |
Fast text matching and autocomplete engine for knowledge graphs.
terraphim_automata provides high-performance text processing using Aho-Corasick automata and finite state transducers (FST). It powers Terraphim's autocomplete and knowledge graph linking features with sub-millisecond performance.
[dependencies]
terraphim_automata = "1.0.0"
With remote loading support:
[dependencies]
terraphim_automata = { version = "1.0.0", features = ["remote-loading", "tokio-runtime"] }
For WASM/browser usage:
[dependencies]
terraphim_automata = { version = "1.0.0", features = ["wasm", "typescript"] }
use terraphim_automata::{build_autocomplete_index, fuzzy_autocomplete_search};
use terraphim_types::{Thesaurus, NormalizedTermValue, NormalizedTerm};
// Create a thesaurus
let mut thesaurus = Thesaurus::new("programming".to_string());
thesaurus.insert(
NormalizedTermValue::from("rust"),
NormalizedTerm { id: 1, value: NormalizedTermValue::from("rust"), url: None }
);
thesaurus.insert(
NormalizedTermValue::from("rust async"),
NormalizedTerm { id: 2, value: NormalizedTermValue::from("rust async"), url: None }
);
// Build autocomplete index
let index = build_autocomplete_index(thesaurus, None).unwrap();
// Fuzzy search (handles typos)
let results = fuzzy_autocomplete_search(&index, "rast", 0.8, Some(5)).unwrap();
println!("Found {} matches", results.len());
use terraphim_automata::{load_thesaurus_from_json, replace_matches, LinkType};
let json = r#"{
"name": "programming",
"data": {
"rust": {
"id": 1,
"nterm": "rust programming",
"url": "https://rust-lang.org"
}
}
}"#;
let thesaurus = load_thesaurus_from_json(json).unwrap();
let text = "I love rust programming!";
// Replace with Markdown links
let linked = replace_matches(text, thesaurus.clone(), LinkType::MarkdownLinks).unwrap();
println!("{}", String::from_utf8(linked).unwrap());
// Output: "I love [rust](https://rust-lang.org) programming!"
// Or HTML links
let html = replace_matches(text, thesaurus.clone(), LinkType::HTMLLinks).unwrap();
// Output: 'I love <a href="https://rust-lang.org">rust</a> programming!'
// Or Wiki links
let wiki = replace_matches(text, thesaurus, LinkType::WikiLinks).unwrap();
// Output: "I love [[rust]] programming!"
use terraphim_automata::{AutomataPath, load_thesaurus};
# #[cfg(feature = "remote-loading")]
# async fn example() {
// From local file
let local_path = AutomataPath::from_local("thesaurus.json");
let thesaurus = load_thesaurus(&local_path).await.unwrap();
// From remote URL
let remote_path = AutomataPath::from_remote("https://example.com/thesaurus.json").unwrap();
let thesaurus = load_thesaurus(&remote_path).await.unwrap();
# }
Build for the browser:
# Install wasm-pack
cargo install wasm-pack
# Build for web
wasm-pack build --target web --features wasm
# Build for Node.js
wasm-pack build --target nodejs --features wasm
Use in JavaScript/TypeScript:
import init, { build_autocomplete_index, fuzzy_autocomplete_search } from './pkg';
await init();
const thesaurus = {
name: "programming",
data: {
"rust": { id: 1, nterm: "rust", url: null },
"rust async": { id: 2, nterm: "rust async", url: null }
}
};
const index = build_autocomplete_index(thesaurus, null);
const results = fuzzy_autocomplete_search(index, "rast", 0.8, 5);
console.log("Matches:", results);
See wasm-test/ for a complete example.
| Feature | Description |
|---|---|
remote-loading |
Enable async HTTP loading of thesaurus files |
tokio-runtime |
Add tokio runtime support (required for remote-loading) |
typescript |
Generate TypeScript definitions via tsify |
wasm |
Enable WebAssembly compilation |
build_autocomplete_index() - Build FST index from thesaurusautocomplete_search() - Exact prefix matchingfuzzy_autocomplete_search() - Fuzzy matching with Jaro-Winklerfuzzy_autocomplete_search_levenshtein() - Fuzzy matching with Levenshteinserialize_autocomplete_index() / deserialize_autocomplete_index() - Index serializationfind_matches() - Find all pattern matches in textreplace_matches() - Replace matches with linksextract_paragraphs_from_automata() - Extract context around matchesload_thesaurus() - Load from file or URL (async)load_thesaurus_from_json() - Parse from JSON string (sync)[term](url)<a href="url">term</a>[[term]]See the examples/ directory for:
This crate requires Rust 1.70 or later.
Licensed under Apache-2.0. See LICENSE for details.