Skip to content

Looking Up Entries

Lookup is the fastest way to query a dictionary — it finds entries by exact term match without requiring an index.

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(())
}

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());
}

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 entry
if let Some(from) = &results[0].directed_from {
println!("Redirected from: {}", from.term.as_str());
}

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");

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" are
let results = dict.lookup(&vec!["catdog"], options)?;
for result in &results {
println!("Found: {}", result.entry.term.as_str());
}
// Prints: "cat", "dog"

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)?;

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}")