Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builtin Wiktionary dictionary #31

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Dictionary {
File(String, DictFileType),
Url(String),
Command(String),
Wiktionary(String, bool),
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ spacy-parsing = { path = "../spacy-parsing" }
toml = "0.8.2"
dirs = "5.0.1"
chrono = { version = "0.4.31", features = ["serde"] }
select = "0.6.0"


[features]
Expand Down
53 changes: 53 additions & 0 deletions src-tauri/src/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use select::{
document::Document,
predicate::{Attr, Class, Name, Predicate},
};
use shared::*;
use std::{error::Error, fs};
use tauri::State;
Expand Down Expand Up @@ -51,6 +55,52 @@ async fn get_def_command(lemma: &str, cmd: &str) -> Result<String, Box<dyn Error
Ok(String::from_utf8(output.stdout)?)
}

fn to_title(language: &str) -> String {
let mut c = language.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str().to_lowercase().as_str(),
}
}

async fn get_def_wiktionary(
lemma: &str,
language: &str,
ignore_morph: bool,
) -> Result<String, Box<dyn Error>> {
let language = to_title(language);
let text = reqwest::get(format!("https://wiktionary.org/wiki/{lemma}"))
.await?
.text()
.await?;
let doc = Document::from_read(text.as_bytes())?;

let mut def = String::new();
for node in doc.find(Name("h2").descendant(Attr("id", language.as_str()))) {
let mut node = node.parent().unwrap();
while let Some(cur_node) = node.next() {
if cur_node.name() == Some("h2") {
break;
}
if cur_node.as_comment().is_none()
// TODO: this gets rid of all the labels which is maybe fine?
&& cur_node
.find(Class("mw-editsection").or(Class("mw-editsection-bracket")))
.next()
.is_none()
{
if cur_node.is(Class("NavFrame")) && ignore_morph {
node = cur_node;
continue;
}
def.push_str(&cur_node.html());
}
node = cur_node;
}
}
Ok(format!("<div>'{def}</div>"))
}

#[tauri::command]
pub async fn get_defs(
state: State<'_, SakinyjeState>,
Expand All @@ -75,6 +125,9 @@ async fn get_def(dict: &Dictionary, lemma: &str) -> Result<String, Box<dyn Error
Dictionary::File(f, dict_type) => get_def_from_file(lemma, f, dict_type),
Dictionary::Url(url) => get_def_url(lemma, url).await,
Dictionary::Command(cmd) => get_def_command(lemma, cmd).await,
Dictionary::Wiktionary(lang, ignore_morph) => {
get_def_wiktionary(lemma, lang, *ignore_morph).await
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions src-ui/src/settings/dictionary_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ fn DictionaryRepresentation(
wdict(Dictionary::Command(String::new()));
}
}
"wiktionary" => {
if !matches!(rdict(), Dictionary::Wiktionary(_, _)) {
wdict(Dictionary::Wiktionary(String::new(), false));
}
}
_ => unreachable!(),
};
view! {
Expand All @@ -88,6 +93,9 @@ fn DictionaryRepresentation(
<option value="command" selected=matches!(rdict(), Dictionary::Command(_))>
From command
</option>
<option value="wiktionary" selected=matches!(rdict(), Dictionary::Wiktionary(_, _))>
From wiktionary
</option>
</select>
</div>
{move || match rdict() {
Expand Down Expand Up @@ -135,6 +143,33 @@ fn DictionaryRepresentation(
}
.into_view()
}
Dictionary::Wiktionary(url, _) => {
let (read_url, write_url) = create_signal(url);
view! {
<div class="labeledinput">
<label for="wiktionary">Language</label>
<input
id="wiktionary"
type="text"
on:input=move |ev| {
write_url(event_target_value(&ev));
}

on:change=move |_| {
wdict
.update(|v| {
if let Dictionary::Wiktionary(_, hide_morph) = v {
*v = Dictionary::Wiktionary(read_url(), *hide_morph);
}
})
}

prop:value=read_url
/>
</div>
}
.into_view()
}
Dictionary::File(filename, dict_type) => {
view! {
<FileDictionaryRepresentation
Expand All @@ -148,6 +183,30 @@ fn DictionaryRepresentation(
}
}

// <div class="labeledcheckbox">
// <label for="hidemorph">Hide morphology</label>
// <input
// id="hidemorph"
// type="checkbox"
// on:change=move |ev| {
// // write_showmorph(event_target_checked(&ev));
//
// // FIX: writing breaks it
// if let Dictionary::Wiktionary(lang, _) = rdict() {
// console_log("got here");
// wdict(Dictionary::Wiktionary(lang, event_target_checked(&ev)));
// console_log("anndd got here");
// } else {
// console_log("didnt got here");
// }
// // write_showmorph(event_target_checked(&ev));
// // wdict.update(move |v| if let Dictionary::Wiktionary(lang, _) = v { *v = Dictionary::Wiktionary(lang.to_owned(), read_showmorph()) });
// }
//
// prop:value=move || if let Dictionary::Wiktionary(_, hide_morph) = rdict() { hide_morph } else { false }
// />
// </div>

#[component]
fn file_dictionary_representation(
filename: String,
Expand Down