#[cfg(feature = "ctxfreq")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::borrow::Cow;
use std::path::PathBuf;
use lindera::dictionary::load_dictionary;
use lindera::mode::Mode;
use lindera::segmenter::Segmenter;
const USAGE: &str = "usage: ctxfreq_dump <dictionary-uri> <corpus> <out>";
let mut args = std::env::args().skip(1);
let dictionary_uri = args.next().ok_or(USAGE)?;
let corpus_path = args.next().ok_or(USAGE)?;
let out_path = args.next().ok_or(USAGE)?;
let text = std::fs::read_to_string(&corpus_path)?;
let dictionary = load_dictionary(&dictionary_uri)?;
let forward_size = dictionary.connection_cost_matrix.forward_size as usize;
let backward_size = dictionary.connection_cost_matrix.backward_size as usize;
let segmenter = Segmenter::new(Mode::Normal, dictionary, None);
let tokens = segmenter.segment(Cow::Borrowed(text.as_str()))?;
lindera_dictionary::builder::context_id_remap::dump_ctx_freq(
PathBuf::from(&out_path).as_path(),
forward_size,
backward_size,
)?;
println!(
"dictionary={dictionary_uri} corpus={corpus_path} tokens={} axes: forward={forward_size} backward={backward_size} -> {out_path}",
tokens.len()
);
Ok(())
}
#[cfg(not(feature = "ctxfreq"))]
fn main() {
eprintln!(
"this example requires --features ctxfreq plus the embed-* feature for the target dictionary (e.g. --features embed-unidic,ctxfreq)"
);
}