Skip to content

Commit

Permalink
fix: +12 languages
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-du committed Jan 16, 2021
1 parent 41b69e5 commit 5bddd4e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod network;
pub mod semantics;

pub mod conf {}

Expand Down
28 changes: 28 additions & 0 deletions src/common/semantics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use whatlang::{detect, Lang};

/// Identify word language
pub fn identify(words: String) -> &'static str {
match detect(words.as_str()) {
None => "en",
Some(lang) => match lang.lang() {
Lang::Eng => "en",
Lang::Cmn => "zh",
Lang::Spa => "es",
Lang::Ita => "it",
Lang::Fra => "fr",
Lang::Jpn => "ja",
Lang::Deu => "de",
Lang::Kor => "ko",
Lang::Rus => "ru",
Lang::Vie => "vi",
Lang::Tha => "th",
Lang::Ukr => "uk",
Lang::Por => "pt",
Lang::Ara => "ar",
Lang::Hin => "hi",
Lang::Tur => "tr",
Lang::Ind => "id",
_ => "en",
},
}
}
28 changes: 14 additions & 14 deletions src/handler/get_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use log::{error, info};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use tide::Request;
use whatlang::{detect, Lang};
use wikipedia::Wikipedia;

use crate::common::network::ProxyClient;
use crate::common::semantics;

#[derive(Debug, Deserialize)]
struct Search {
lang: Option<String>,
words: String,
limit: Option<u32>,
}
Expand Down Expand Up @@ -50,27 +51,26 @@ pub async fn search(req: Request<()>) -> tide::Result {
WikiGraphInfo::new(0, query.words.clone(), 3, query.words.clone(), vec![]);

let mut wiki = Wikipedia::<ProxyClient>::default();
// limit page results
if query.limit.is_some() {
wiki.search_results = query.limit.unwrap();
} else {
wiki.search_results = 10
}

let language = match detect(query.words.as_str()) {
None => "en",
Some(lang) => match lang.lang() {
Lang::Eng => "en",
Lang::Cmn => "zh",
Lang::Spa => "es",
Lang::Ita => "it",
Lang::Fra => "fr",
_ => "en",
},
};
// limit results
if query.lang.is_some() {
wiki.set_base_url(
format!("https://{}.wikipedia.org/w/api.php", query.lang.unwrap()).as_str(),
);
} else {
let language = semantics::identify(query.words.clone());
wiki.set_base_url(format!("https://{}.wikipedia.org/w/api.php", language).as_str());
}
info!("Base URL => {}", wiki.base_url());

// data processing
let words = query.words.as_str();
wiki.set_base_url(format!("https://{}.wikipedia.org/w/api.php", language).as_str());
info!("Base URL => {}", wiki.base_url());
match wiki.search(words) {
Ok(results) => {
for res in results {
Expand Down

0 comments on commit 5bddd4e

Please sign in to comment.