deeptrans/
detection.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//use serde::{Deserialize, Serialize};
use reqwest::blocking as sync;
use serde_json::Value;

//#[derive(Serialize, Deserialize)]
pub enum QText {
    Str(String),
    ListStr(Vec<String>),
}

impl QText {
    fn is_empty(&self) -> bool {
        match self {
            QText::Str(value) => value.is_empty(),
            QText::ListStr(value) => value.is_empty(),
        }
    }
}
impl From<String> for QText {
    fn from(value: String) -> QText {
        QText::Str(value)
    }
}

impl From<Vec<String>> for QText {
    fn from(value: Vec<String>) -> QText {
        QText::ListStr(value)
    }
}

impl ToString for QText {
    fn to_string(&self) -> String {
        match self {
            QText::Str(value) => value.clone(),
            QText::ListStr(value) => format!("{:?}", &value),
        }
    }
}

/// send a request and return the response body parsed as dictionary
fn get_request_body<T: Into<QText>>(text: T, api_key: &str) -> Value {
    debug_assert!(
        !api_key.is_empty(),
        "you need to get an API_KEY for this to work. \n\
        Get one for free here: https://detectlanguage.com/documentation"
    );

    let text = T::into(text);

    debug_assert!(!text.is_empty(), "Please provide an input text");

    let req: Value = sync::Client::builder()
        .user_agent("Detect Language API Rust Client 1.4.0")
        .build()
        .unwrap()
        .post("https://ws.detectlanguage.com/0.2/detect")
        .header("User-Agent", format!("Bearer {api_key}").as_str())
        .json(&serde_json::json!({
            "q": text.to_string()
        }))
        .send()
        .unwrap()
        .json()
        .unwrap();

    println!("req -> {:?}", &req);
    req["data"].clone()
}

/// function responsible for detecting the language from a text
pub fn single(text: &str, api_key: &str, detailed: bool) -> Value {
    let body = get_request_body(text.to_string(), api_key);
    println!("body -> {:?}", &body);
    let detections = &body["detections"];

    if detailed {
        return detections[0].clone();
    }

    detections[0]["language"].clone()
}

/// function responsible for detecting the language from a text
pub fn batch(text_list: Vec<String>, api_key: &str, detailed: bool) -> Vec<Value> {
    let body = get_request_body(text_list, api_key);
    println!("body -> {:?}", &body);
    let detections = &body["detections"];

    let Value::Array(detections) = detections else { unreachable!() };
    let res = detections.iter().map(|obj| obj[0].clone()).collect();

    if detailed {
        res
    } else {
        res.iter().map(|obj| obj["language"].clone()).collect()
    }
}