1use langdetect_rs::detector_factory::DetectorFactory;
2use langdetect_rs::utils::lang_profile::{LangProfileJson, LangProfile};
3use std::path::Path;
4
5fn main() {
6 let mut factory = DetectorFactory::new().build();
8
9 let profiles_dir = Path::new("./").join("profiles");
11
12 println!("Read JSON profiles from {}", profiles_dir.display());
13
14 let ru_json = LangProfileJson::new_from_file(profiles_dir.join("ru"));
16 match &ru_json {
17 Ok(_) => println!("\tRead Russian JSON profile"),
18 Err(e) => {
19 println!("Error reading Russian JSON profile: {:?}", e);
20 return;
21 }
22 }
23 let ru_profile = match LangProfile::from_json(ru_json.unwrap()) {
24 Ok(profile) => profile,
25 Err(e) => {
26 println!("Error creating Russian LangProfile: {}", e);
27 return;
28 }
29 };
30
31 let en_json = LangProfileJson::new_from_file(profiles_dir.join("en"));
33 match &en_json {
34 Ok(_) => println!("\tRead English JSON profile"),
35 Err(e) => {
36 println!("Error reading English JSON profile: {:?}", e);
37 return;
38 }
39 }
40 let en_profile = match LangProfile::from_json(en_json.unwrap()) {
41 Ok(profile) => profile,
42 Err(e) => {
43 println!("Error creating English LangProfile: {}", e);
44 return;
45 }
46 };
47
48 println!("Adding custom language profiles to the factory...");
49 let final_size = 2; if let Err(e) = factory.add_profile(ru_profile, 0, final_size
54 ) {
55 println!("Error adding Russian profile: {:?}", e);
56 return;
57 }
58 println!("\tLoaded Russian profile");
59 if let Err(e) = factory.add_profile(en_profile, 1, final_size) {
60 println!("Error adding English profile: {:?}", e);
61 return;
62 }
63 println!("\tLoaded English profile");
64
65 println!("Factory loaded with {} languages: {:?}", factory.get_lang_list().len(), factory.get_lang_list());
66
67 println!("Testing language detection...");
68
69 match factory.detect("Привет, меня зовут Дима, и я разработчик", None) {
71 Ok(lang) => println!("\tRussian text detected as: {}", lang),
72 Err(e) => println!("Detection error: {:?}", e),
73 }
74
75 match factory.detect("Hello world! My name is Dima and I am a developer", None) {
77 Ok(lang) => println!("\tEnglish text detected as: {}", lang),
78 Err(e) => println!("Detection error: {:?}", e),
79 }
80
81 match factory.detect("Bonjour tout le monde! Je m'appelle Dima et je suis développeur", None) {
86 Ok(lang) => println!("\tFrench text detected as: {} (closest match from ru/en)", lang),
87 Err(e) => println!("Detection error: {:?}", e),
88 }
89
90 match factory.get_probabilities("Bonjour tout le monde! Je m'appelle Dima et je suis développeur", None) {
92 Ok(probs) => {
93 println!("\tFrench text probabilities:");
94 for lang in probs {
95 println!("\t\t{}: {:.3}", lang.lang.unwrap_or_default(), lang.prob);
96 }
97 }
98 Err(e) => println!("Probability error: {:?}", e),
99 }
100}