-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from BrewingWeasel/spacy
refactor: move spacy parsing into its own crate
- Loading branch information
Showing
8 changed files
with
143 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[workspace] | ||
resolver = "2" | ||
|
||
members = ["src-ui", "src-tauri", "shared"] | ||
members = ["src-ui", "src-tauri", "shared", "spacy-parsing"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "spacy-parsing" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
pyo3 = { version = "0.20.0", features = ["auto-initialize"] } | ||
shared = { path = "../shared" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use pyo3::{exceptions::PyEnvironmentError, prelude::*}; | ||
use std::{collections::HashMap, str::FromStr}; | ||
|
||
pub struct Token { | ||
pub text: String, | ||
pub lemma: String, | ||
pub pos: PartOfSpeech, | ||
pub morph: HashMap<String, String>, | ||
} | ||
|
||
pub enum PartOfSpeech { | ||
Adjective, | ||
Adposition, | ||
Adverb, | ||
Auxiliary, | ||
CoordinatingConjunction, | ||
Determiner, | ||
Interjection, | ||
Noun, | ||
Numeral, | ||
Particle, | ||
Pronoun, | ||
ProperNoun, | ||
Punctuation, | ||
SubordinatingConjunction, | ||
Symbol, | ||
Verb, | ||
Other, | ||
} | ||
|
||
impl FromStr for PartOfSpeech { | ||
type Err = (); | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"ADJ" => Ok(Self::Adjective), | ||
"ADP" => Ok(Self::Adposition), | ||
"ADV" => Ok(Self::Adverb), | ||
"AUX" => Ok(Self::Auxiliary), | ||
"CCONJ" => Ok(Self::CoordinatingConjunction), | ||
"DET" => Ok(Self::Determiner), | ||
"INTJ" => Ok(Self::Interjection), | ||
"NOUN" => Ok(Self::Noun), | ||
"NUM" => Ok(Self::Numeral), | ||
"PART" => Ok(Self::Particle), | ||
"PRON" => Ok(Self::Pronoun), | ||
"PROPN" => Ok(Self::ProperNoun), | ||
"PUNCT" => Ok(Self::Punctuation), | ||
"SCONJ" => Ok(Self::SubordinatingConjunction), | ||
"SYM" => Ok(Self::Symbol), | ||
"VERB" => Ok(Self::Verb), | ||
"X:" => Ok(Self::Other), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
pub fn get_spacy_info(sent: &str, model: &str) -> Result<Vec<Token>, String> { | ||
Python::with_gil(|py| -> PyResult<Vec<Token>> { | ||
let mut words = Vec::new(); | ||
let spacy = PyModule::import(py, "spacy")?; | ||
let morphologizer = match spacy.getattr("load")?.call1((model,)) { | ||
Ok(v) => v, | ||
Err(_) => { | ||
return Err(PyEnvironmentError::new_err(format!( | ||
"Unable to load {model}" | ||
))) | ||
} | ||
}; | ||
let total: Vec<PyObject> = morphologizer.call1((sent,))?.extract()?; | ||
for token in total { | ||
let text: String = token.getattr(py, "text")?.extract(py)?; | ||
let pos_str: String = token.getattr(py, "pos_")?.extract(py)?; | ||
let pos = PartOfSpeech::from_str(&pos_str).unwrap(); | ||
let lemma: String = token.getattr(py, "lemma_")?.extract(py)?; | ||
let morph: HashMap<String, String> = token | ||
.getattr(py, "morph")? | ||
.getattr(py, "to_dict")? | ||
.call0(py)? | ||
.extract(py)?; | ||
|
||
words.push(Token { | ||
text, | ||
lemma, | ||
pos, | ||
morph, | ||
}) | ||
} | ||
Ok(words) | ||
}) | ||
.map_err(|e| e.to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,21 @@ | ||
use pyo3::{exceptions::PyEnvironmentError, prelude::*}; | ||
use shared::*; | ||
use tauri::Window; | ||
use spacy_parsing::{get_spacy_info, PartOfSpeech}; | ||
|
||
#[tauri::command] | ||
pub async fn parse_text(_window: Window, sent: &str, model: &str) -> Result<Vec<Word>, String> { | ||
Python::with_gil(|py| -> PyResult<Vec<Word>> { | ||
let mut words = Vec::new(); | ||
let spacy = PyModule::import(py, "spacy")?; | ||
let morphologizer = match spacy.getattr("load")?.call1((model,)) { | ||
Ok(v) => v, | ||
Err(_) => { | ||
return Err(PyEnvironmentError::new_err(format!( | ||
"Unable to load {model}" | ||
))) | ||
} | ||
}; | ||
let total: Vec<PyObject> = morphologizer.call1((sent,))?.extract()?; | ||
for i in total { | ||
let text: String = i.getattr(py, "text")?.extract(py)?; | ||
let pos: String = i.getattr(py, "pos_")?.extract(py)?; | ||
let clickable = pos != "PUNCT"; | ||
let lemma: String = i.getattr(py, "lemma_")?.extract(py)?; | ||
let morph: Option<String> = match i | ||
.getattr(py, "morph") | ||
.and_then(|v| v.getattr(py, "get")?.call1(py, ("Case",))) | ||
.and_then(|v| v.extract::<Vec<String>>(py)) | ||
{ | ||
Ok(mut s) if !s.is_empty() => Some(s.remove(0)), | ||
_ => None, | ||
}; | ||
|
||
println!("{:?}", morph); | ||
words.push(Word { | ||
text, | ||
lemma, | ||
morph, | ||
clickable, | ||
}) | ||
} | ||
Ok(words) | ||
}) | ||
.map_err(|e| e.to_string()) | ||
pub async fn parse_text(sent: &str, model: &str) -> Result<Vec<Word>, String> { | ||
let mut words = Vec::new(); | ||
let parsed_words = get_spacy_info(sent, model)?; | ||
for word in parsed_words { | ||
let clickable = !matches!( | ||
word.pos, | ||
PartOfSpeech::Punctuation | PartOfSpeech::Symbol | PartOfSpeech::Numeral | ||
); | ||
words.push(Word { | ||
text: word.text, | ||
clickable, | ||
lemma: word.lemma, | ||
morph: word.morph, | ||
}); | ||
} | ||
Ok(words) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters