extend_default/
main.rs

1use langdetect_rs::detector_factory::DetectorFactory;
2use langdetect_rs::utils::lang_profile::{LangProfileJson, LangProfile};
3use std::path::Path;
4
5fn main() {
6    // Load language profiles from the crate's profiles directory
7    let profiles_dir = DetectorFactory::get_default_profiles_path();
8
9    println!("Read all default JSON profiles from {}", profiles_dir.display());
10
11    let mut lang_profiles = vec![];
12    // Load every profile in the directory
13    for entry in std::fs::read_dir(&profiles_dir).unwrap() {
14        let entry = entry.unwrap();
15        let path = entry.path();
16        if !path.is_file() {
17            continue;
18        }
19        let lang_json = LangProfileJson::new_from_file(path);
20        match &lang_json {
21            Ok(_) => println!("\tRead {} JSON profile", entry.file_name().to_string_lossy()),
22            Err(e) => {
23                println!("Error reading {} JSON profile: {:?}", entry.file_name().to_string_lossy(), e);
24                return;
25            }
26        }
27        let lang_profile = match LangProfile::from_json(lang_json.unwrap()) {
28            Ok(profile) => profile,
29            Err(e) => {
30                println!("Error creating {} LangProfile: {}", entry.file_name().to_string_lossy(), e);
31                return;
32            }
33        };
34        lang_profiles.push(lang_profile);
35    }
36
37    println!("Adding all default languages profiles to the factory...");
38
39    // Create an EMPTY factory
40    let mut factory = DetectorFactory::new().build();
41
42    // Get number of profiles to set final size
43    let mut profile_count = lang_profiles.len();
44    // Since we know that we are going to add another profile which is not in the default set
45    // we increase the final size by 1
46    profile_count += 1;
47
48    println!("Final size (assuming we are going to extend default set) of languages array will be: {}", profile_count);
49    
50    for (i, profile) in lang_profiles.into_iter().enumerate() {
51        let profile_name = if let Some(name) = &profile.name {
52            name.clone()
53        } else {
54            "unknown".to_string()
55        };
56        println!("\tAdding profile: {} at index {}", profile_name, i);
57        if let Err(e) = factory.add_profile(profile, i, profile_count) {
58            println!("Error adding {} profile: {:?}", profile_name, e);
59            return;
60        }
61    }
62
63    // Load another profile (in documentation for generating profiles it is Sakha (Yakut) language - "sah")
64    let sah_path = Path::new("./scripts/datasets/generated").join("sah_generated.json");
65    let sah_json = LangProfileJson::new_from_file(sah_path);
66    match &sah_json {
67        Ok(_) => println!("Read Sakha JSON profile"),
68        Err(e) => {
69            println!("Error reading Sakha JSON profile: {:?}", e);
70            return;
71        }
72    }
73    let sah_profile = match LangProfile::from_json(sah_json.unwrap()) {
74        Ok(profile) => profile,
75        Err(e) => {
76            println!("Error creating Sakha LangProfile: {}", e);
77            return;
78        }
79    };
80    println!("Adding Sakha language profile to the factory");
81    if let Err(e) = factory.add_profile(sah_profile, profile_count - 1, profile_count) {
82        println!("Error adding Sakha profile: {:?}", e);
83        return;
84    }
85
86    println!("Testing language detection...");
87    // Test Russian text
88    match factory.detect("В своём глазу бревна не замечает, а в чужом соломинку видит", None) {
89        Ok(lang) => println!("\tRussian text detected as: {}", lang),
90        Err(e) => println!("Detection error: {:?}", e),
91    }
92
93    // Test English text
94    match factory.detect("He pays no attention to the plank in his own eye", None) {
95        Ok(lang) => println!("\tEnglish text detected as: {}", lang),
96        Err(e) => println!("Detection error: {:?}", e),
97    }
98
99    // Test Sakha (Yakut) text
100    match factory.detect("Айаҕыттан тахсар сытыканы билбэт.", None) {
101        Ok(lang) => println!("\tSakha text detected as: {}", lang),
102        Err(e) => println!("Detection error: {:?}", e),
103    }
104}