custom_profile/
main.rs

1use langdetect_rs::detector_factory::DetectorFactory;
2use langdetect_rs::utils::lang_profile::{LangProfileJson, LangProfile};
3use std::path::Path;
4
5fn main() {
6    // Create an empty factory
7    let mut factory = DetectorFactory::new().build();
8
9    // Load language profiles from the crate's profiles directory
10    let profiles_dir = Path::new("./").join("profiles");
11
12    println!("Read JSON profiles from {}", profiles_dir.display());
13
14    // Load Russian profile
15    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    // Load English profile
32    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    // Add profiles to the factory
50    // Make sure to use correct language IDs as per your profiles
51    // And provide correct FINAL size of languages array
52    let final_size = 2; // Update this if you add more profiles
53    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    // Test Russian text
70    match factory.detect("Привет, меня зовут Дима, и я разработчик", None) {
71        Ok(lang) => println!("\tRussian text detected as: {}", lang),
72        Err(e) => println!("Detection error: {:?}", e),
73    }
74
75    // Test English text
76    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    // Test French text (will be detected as the closest match from available languages)
82    // IMPORTANT: The algorithm always returns the best guess from loaded languages, never fails
83    // EXCEPTIONS: Returns error if no recognizable n-grams found, or "unknown" if all probabilities ≤ 0.1
84    // If you want to detect "unknown" languages, check probability thresholds or handle the error cases
85    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    // Show probabilities for the French text to see why it was classified as English
91    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}