Looking Up Entries
Lookup is the fastest way to query a dictionary — it finds entries by exact term match without requiring an index.
Basic lookup
Section titled “Basic lookup”use odict::{OpenDictionary, lookup::LookupOptions};
fn main() -> odict::Result<()> {let file = OpenDictionary::from_path("my-dictionary.odict")?;let dict = file.contents()?;
let results = dict.lookup( &vec!["cat"], LookupOptions::default(), )?;
for result in &results { println!("{}", result.entry.term.as_str()); }
Ok(())
}from theopendictionary import OpenDictionary, compile
dictionary = OpenDictionary("<dictionary>...</dictionary>")
results = dictionary.lookup("cat")print(results[0].entry.term) # "cat"import { OpenDictionary } from "@odict/node";
const dictionary = await OpenDictionary.load("./my-dictionary.odict");
const results = dictionary.lookup("cat");console.log(results[0].entry.term); // "cat"Looking up multiple terms
Section titled “Looking up multiple terms”You can look up several terms in a single call. Results are returned for each matched term.
let results = dict.lookup( &vec!["cat", "dog", "run"], LookupOptions::default(),)?;
for result in &results { println!("Found: {}", result.entry.term.as_str());}results = dictionary.lookup(["cat", "dog", "run"])
for result in results:print(f"Found: {result.entry.term}")const results = dictionary.lookup(["cat", "dog", "run"]);
for (const result of results) { console.log(`Found: ${result.entry.term}`);}Following cross-references
Section titled “Following cross-references”Entries can redirect to other entries using the see attribute (e.g. “ran” → “run”). Enable follow to automatically resolve these.
use odict::lookup::LookupOptions;
let options = LookupOptions::default().follow(true);
let results = dict.lookup(&vec!["ran"], options)?;
// "ran" redirects to "run"assert_eq!(results[0].entry.term.as_str(), "run");
// directed_from tells you the original entryif let Some(from) = &results[0].directed_from {println!("Redirected from: {}", from.term.as_str());}results = dictionary.lookup("ran", follow=True)
# "ran" redirects to "run"print(results[0].entry.term) # "run"print(results[0].directed_from.term) # "ran"const results = dictionary.lookup("ran", { follow: true });
// "ran" redirects to "run"console.log(results[0].entry.term); // "run"console.log(results[0].directedFrom?.term); // "ran"Case-insensitive lookup
Section titled “Case-insensitive lookup”By default, lookups are case-sensitive. Enable insensitive to fall back to lowercase matching when the exact case doesn’t match.
let options = LookupOptions::default().insensitive(true);
// "CAT" will match "cat"let results = dict.lookup(&vec!["CAT"], options)?;
assert_eq!(results[0].entry.term.as_str(), "cat");# "CAT" will match "cat"results = dictionary.lookup("CAT", insensitive=True)
print(results[0].entry.term) # "cat"// "CAT" will match "cat"const results = dictionary.lookup("CAT", { insensitive: true });
console.log(results[0].entry.term); // "cat"Compound word splitting
Section titled “Compound word splitting”If a term isn’t found, ODict can split it into substrings and look up each part. The split parameter sets the minimum character length for each fragment.
use odict::lookup::{LookupOptions, LookupStrategy};
let options = LookupOptions::default().strategy(LookupStrategy::Split(3));
// "catdog" isn't a word, but "cat" and "dog" arelet results = dict.lookup(&vec!["catdog"], options)?;
for result in &results {println!("Found: {}", result.entry.term.as_str());}// Prints: "cat", "dog"# "catdog" isn't a word, but "cat" and "dog" areresults = dictionary.lookup("catdog", split=3)
for result in results: print(result.entry.term)# Prints: "cat", "dog"// "catdog" isn't a word, but "cat" and "dog" areconst results = dictionary.lookup("catdog", { split: 3 });
for (const result of results) {console.log(result.entry.term);}// Prints: "cat", "dog"Combining options
Section titled “Combining options”All lookup options can be combined.
let options = LookupOptions::default() .follow(true) .insensitive(true) .strategy(LookupStrategy::Split(3));
let results = dict.lookup(&vec!["RaN"], options)?;results = dictionary.lookup("RaN", follow=True, insensitive=True, split=3)const results = dictionary.lookup("RaN", { follow: true, insensitive: true, split: 3,});Reading entry data
Section titled “Reading entry data”Once you have a LookupResult, you can traverse the entry’s structure: etymologies, senses, definitions, examples, and more.
results = dictionary.lookup("cat")entry = results[0].entry
print(f"Term: {entry.term}")
for ety in entry.etymologies:for sense in ety.senses:print(f" Part of speech: {sense.pos}")for defn in sense.definitions:print(f" {defn.value}")for example in defn.examples:print(f" e.g. {example.value}")const results = dictionary.lookup("cat");const entry = results[0].entry;
console.log(`Term: ${entry.term}`);
for (const ety of entry.etymologies) { for (const sense of ety.senses) { console.log(` Part of speech: ${sense.pos.value}`); for (const defn of sense.definitions) { if ("value" in defn) { console.log(` ${defn.value}`); for (const example of defn.examples) { console.log(` e.g. ${example.value}`); } } } }}let results = dict.lookup(&vec!["cat"], LookupOptions::default())?;
for result in &results {let entry = result.entry.deserialize()?;
println!("Term: {}", entry.term);
for ety in &entry.etymologies { for (pos, sense) in &ety.senses { println!(" Part of speech: {}", pos); for defn in &sense.definitions { println!(" {}", defn.value); for example in &defn.examples { println!(" e.g. {}", example.value); } } } }
}