simple/
main.rs

1use langdetect_rs::detector_factory::DetectorFactory;
2
3fn main() {
4    let factory = DetectorFactory::default().build();
5
6    // let mut detector = factory.create(None);
7    match factory.detect("War doesn't show who's right, just who's left.", None) {
8        Ok(lang) => println!("Detected language: {}", lang),
9        Err(e) => println!("Detection error: {:?}", e),
10    }
11
12    // let mut detector = factory.create(None);
13    match factory.detect("Ein, zwei, drei, vier", None) {
14        Ok(lang) => println!("Detected language: {}", lang),
15        Err(e) => println!("Detection error: {:?}", e),
16    }
17
18    match factory.get_probabilities("Otec matka syn.", None) {
19        Ok(probs) => println!("Language probabilities: {:?}", probs),
20        Err(e) => println!("Detection error: {:?}", e),
21    }
22
23    // For reproducibility use a fixed seed within explicitly defined detector
24    let mut detector = factory.create(None);
25    detector.seed = Some(42);
26    detector.append("Otec matka syn.");
27    match detector.get_probabilities() {
28        Ok(probs) => println!("Language probabilities with seed: {:?}", probs),
29        Err(e) => println!("Detection error: {:?}", e),
30    }
31
32    // Or you can set the seed for the factory itself and it will be inherited by detectors
33    let factory_with_seed = DetectorFactory::default()
34        .with_seed(Some(43))
35        .build();
36    match factory_with_seed.get_probabilities("Otec matka syn.", None) {
37        Ok(probs) => println!("Language probabilities with seed: {:?}", probs),
38        Err(e) => println!("Detection error: {:?}", e),
39    }
40}