diff --git a/src/database.rs b/src/database.rs index cbdfd71..7394dce 100644 --- a/src/database.rs +++ b/src/database.rs @@ -47,13 +47,18 @@ impl WordDb { transaction.execute(&format!(" CREATE TABLE {}_words ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - word TINYTEXT UNIQUE NOT NULL, + word TINYTEXT NOT NULL, type_id INTEGER NOT NULL, content MEDIUMTEXT NOT NULL, FOREIGN KEY (type_id) REFERENCES {}_types (id) )", &lang.code, &lang.code), []).unwrap(); + transaction.execute(&format!(" + CREATE INDEX word_index + ON {}_words (word) + ", &lang.code), []).unwrap(); + transaction.commit().unwrap(); } @@ -102,8 +107,7 @@ impl WordDb { } println!("Parsing data..."); - let entries = WiktionaryEntries::parse_data(data) - .merge_duplicates(); + let entries = WiktionaryEntries::parse_data(data); println!("Inserting data..."); self.insert_entries(lang, entries); diff --git a/src/main.rs b/src/main.rs index 4230471..14117db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ //mod database; use rocket::routes; use rocket::fs::FileServer; -use rocket::data::{Limits, ToByteUnit}; use clap::{App, AppSettings, Arg, SubCommand}; //use database::WordDb; mod database; @@ -83,8 +82,8 @@ async fn main() { rocket::custom(figment) .manage(db) .mount("/static", FileServer::from("static/")) - .mount("/", routes![routes::get_word, - routes::get_word_like, + .mount("/", routes![routes::get_entries, + routes::get_entries_like, routes::frontend]) .launch() .await.unwrap(); diff --git a/src/routes.rs b/src/routes.rs index edb1e7f..8dad838 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -18,34 +18,46 @@ pub fn frontend() -> Option> { } #[get("/langs//words/")] -pub fn get_word(db: &State, lang: &str, word: &str) -> Option> { - let connection = db.connect(); - - let word = connection.query_row(&format!( - "SELECT content FROM {}_words WHERE word = ?", - lang), - [word], - |row| row.get(0) - ); - - match word { - Ok(w) => Some(content::Json(w)), - Err(_) => None - } -} - -#[get("/langs//words?&&")] -pub fn get_word_like(db: &State, lang: &str, like: &str, limit: usize, offset: usize) -> Json> { +pub fn get_entries(db: &State, lang: &str, word: &str) -> status::Custom> { let connection = db.connect(); let mut statement = connection.prepare(&format!( - "SELECT word - FROM {}_words - WHERE word LIKE ? - ORDER BY length(word) ASC - LIMIT ? - OFFSET ?", - lang) + "SELECT content + FROM {}_words + WHERE word = ?", + lang) + ).unwrap(); + + let mut rows = statement.query([word]).unwrap(); + + let mut words = String::new(); + + words.push('['); + while let Some(row) = rows.next().unwrap() { + let content: String = row.get(0).unwrap(); + words.push_str(&content); + words.push(','); + } + if words.pop().unwrap() == '[' { + words.push('['); + } + words.push(']'); + + status::Custom(Status::Ok, content::Json(words)) +} + +#[get("/langs//words?&&")] +pub fn get_entries_like(db: &State, lang: &str, like: &str, limit: usize, offset: usize) -> Json> { + let connection = db.connect(); + + let mut statement = connection.prepare(&format!( + "SELECT word + FROM {}_words + WHERE word LIKE ? + ORDER BY length(word) ASC + LIMIT ? + OFFSET ?", + lang) ).unwrap(); let mut rows = statement.query(params![format!("%{}%", like), limit, offset]).unwrap(); diff --git a/static/index.js b/static/index.js index b12c64a..64dd4c7 100644 --- a/static/index.js +++ b/static/index.js @@ -31,7 +31,9 @@ function getWord(word) { success: (data) => { $('#ajax-content').html(generateHtml(data)) searchBar.autocomplete('close'); - } + }, + + error: err => console.error(err) }) } @@ -90,6 +92,12 @@ function generateTable(schemas, pos, forms) { let unusedCells = forms.filter(cell => !cell.used); + if(schema.ignoreUnused) { + unusedCells = unusedCells.filter(cell => + !schema.ignoreUnused.map(tags => tags.every(tag => cell.tags.includes(tag))) + ); + } + if(unusedCells.length > 0) { html += '

Other

'; html += generateList(unusedCells); @@ -101,10 +109,6 @@ function generateTable(schemas, pos, forms) { function generateHtml(data) { let html = ''; - if(Object.prototype.toString.call(data) !== '[object Array]') { - data = [data]; - } - data.forEach(entry => { html += `

${entry.word} (${entry.pos})

` if('senses' in entry) { diff --git a/static/schemas/polish.json b/static/schemas/polish.json index d4324e7..f396012 100644 --- a/static/schemas/polish.json +++ b/static/schemas/polish.json @@ -142,6 +142,11 @@ }, { "pos": "adj", + "ignoreUnused": [ + ["dative", "feminine", "singular"], + ["instrumental", "feminine", "singular"], + ["locative", "masculine", "neuter", "singular"] + ], "rows": [ [ {