Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: now entirely uses new affix format
Elijah Potter67a021f66134
diff --git a/harper-core/affixes.json b/harper-core/affixes.jsonnew file mode 100644index 000000000..578ac65f5--- /dev/null+++ b/harper-core/affixes.json@@ -0,0 +1,514 @@+{+ "affixes": {+ "K": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "pro",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "L": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "ment",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "E": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "dis",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "Y": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "ly",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "U": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "un",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "H": {+ "suffix": true,+ "cross_product": false,+ "replacements": [+ {+ "remove": "y",+ "add": "ieth",+ "condition": "y"+ },+ {+ "remove": "",+ "add": "th",+ "condition": "[^y]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "T": {+ "suffix": true,+ "cross_product": false,+ "replacements": [+ {+ "remove": "",+ "add": "st",+ "condition": "e"+ },+ {+ "remove": "y",+ "add": "iest",+ "condition": "[^aeiou]y"+ },+ {+ "remove": "",+ "add": "est",+ "condition": "[aeiou]y"+ },+ {+ "remove": "",+ "add": "est",+ "condition": "[^ey]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "R": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "r",+ "condition": "e"+ },+ {+ "remove": "y",+ "add": "ier",+ "condition": "[^aeiou]y"+ },+ {+ "remove": "",+ "add": "er",+ "condition": "[aeiou]y"+ },+ {+ "remove": "",+ "add": "er",+ "condition": "[^ey]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "C": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "de",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "V": {+ "suffix": true,+ "cross_product": false,+ "replacements": [+ {+ "remove": "e",+ "add": "ive",+ "condition": "e"+ },+ {+ "remove": "",+ "add": "ive",+ "condition": "[^e]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "N": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "e",+ "add": "ion",+ "condition": "e"+ },+ {+ "remove": "y",+ "add": "ication",+ "condition": "y"+ },+ {+ "remove": "",+ "add": "en",+ "condition": "[^ey]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "A": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "re",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "Z": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "rs",+ "condition": "e"+ },+ {+ "remove": "y",+ "add": "iers",+ "condition": "[^aeiou]y"+ },+ {+ "remove": "",+ "add": "ers",+ "condition": "[aeiou]y"+ },+ {+ "remove": "",+ "add": "ers",+ "condition": "[^ey]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "P": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "y",+ "add": "iness",+ "condition": "[^aeiou]y"+ },+ {+ "remove": "",+ "add": "ness",+ "condition": "[aeiou]y"+ },+ {+ "remove": "",+ "add": "ness",+ "condition": "[^y]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "M": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "'s",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "F": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "con",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "B": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "able",+ "condition": "[^aeiou]"+ },+ {+ "remove": "",+ "add": "able",+ "condition": "ee"+ },+ {+ "remove": "e",+ "add": "able",+ "condition": "[^aeiou]e"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "S": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "y",+ "add": "ies",+ "condition": "[^aeiou]y"+ },+ {+ "remove": "",+ "add": "s",+ "condition": "[aeiou]y"+ },+ {+ "remove": "",+ "add": "es",+ "condition": "[sxzh]"+ },+ {+ "remove": "",+ "add": "s",+ "condition": "[^sxzhy]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "D": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "d",+ "condition": "e"+ },+ {+ "remove": "y",+ "add": "ied",+ "condition": "[^aeiou]y"+ },+ {+ "remove": "",+ "add": "ed",+ "condition": "[^ey]"+ },+ {+ "remove": "",+ "add": "ed",+ "condition": "[aeiou]y"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "G": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "e",+ "add": "ing",+ "condition": "e"+ },+ {+ "remove": "",+ "add": "ing",+ "condition": "[^e]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "Q": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "ally",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "O": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "ful",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "I": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "in",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "X": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "e",+ "add": "ions",+ "condition": "e"+ },+ {+ "remove": "y",+ "add": "ications",+ "condition": "y"+ },+ {+ "remove": "",+ "add": "ens",+ "condition": "[^ey]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "J": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "e",+ "add": "ings",+ "condition": "e"+ },+ {+ "remove": "",+ "add": "ings",+ "condition": "[^e]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ }+ }+}\ No newline at end of filediff --git a/harper-core/dictionary.aff b/harper-core/dictionary.affdeleted file mode 100644index 5c6acba93..000000000--- a/harper-core/dictionary.aff+++ /dev/null@@ -1,211 +0,0 @@-SET UTF-8-TRY esianrtolcdugmphbyfvkwzESIANRTOLCDUGMPHBYFVKWZ'-ICONV 1-ICONV ’ '-NOSUGGEST !--# ordinal numbers-COMPOUNDMIN 1-# only in compounds: 1th, 2th, 3th-ONLYINCOMPOUND c-# compound rules:-# 1. [0-9]*1[0-9]th (10th, 11th, 12th, 56714th, etc.)-# 2. [0-9]*[02-9](1st|2nd|3rd|[4-9]th) (21st, 22nd, 123rd, 1234th, etc.)-COMPOUNDRULE 2-COMPOUNDRULE n*1t-COMPOUNDRULE n*mp-WORDCHARS 0123456789--PFX A Y 1-PFX A 0 re .--PFX I Y 1-PFX I 0 in .--PFX U Y 1-PFX U 0 un .--PFX C Y 1-PFX C 0 de .--PFX E Y 1-PFX E 0 dis .--PFX F Y 1-PFX F 0 con .--PFX K Y 1-PFX K 0 pro .--SFX V N 2-SFX V e ive e-SFX V 0 ive [^e]--SFX N Y 3-SFX N e ion e-SFX N y ication y-SFX N 0 en [^ey]--SFX X Y 3-SFX X e ions e-SFX X y ications y-SFX X 0 ens [^ey]--SFX H N 2-SFX H y ieth y-SFX H 0 th [^y]--SFX Y Y 1-SFX Y 0 ly .--SFX G Y 2-SFX G e ing e-SFX G 0 ing [^e]--SFX J Y 2-SFX J e ings e-SFX J 0 ings [^e]--SFX D Y 4-SFX D 0 d e-SFX D y ied [^aeiou]y-SFX D 0 ed [^ey]-SFX D 0 ed [aeiou]y--SFX T N 4-SFX T 0 st e-SFX T y iest [^aeiou]y-SFX T 0 est [aeiou]y-SFX T 0 est [^ey]--SFX R Y 4-SFX R 0 r e-SFX R y ier [^aeiou]y-SFX R 0 er [aeiou]y-SFX R 0 er [^ey]--SFX Z Y 4-SFX Z 0 rs e-SFX Z y iers [^aeiou]y-SFX Z 0 ers [aeiou]y-SFX Z 0 ers [^ey]--SFX S Y 4-SFX S y ies [^aeiou]y-SFX S 0 s [aeiou]y-SFX S 0 es [sxzh]-SFX S 0 s [^sxzhy]--SFX P Y 3-SFX P y iness [^aeiou]y-SFX P 0 ness [aeiou]y-SFX P 0 ness [^y]--SFX M Y 1-SFX M 0 's .--SFX B Y 3-SFX B 0 able [^aeiou]-SFX B 0 able ee-SFX B e able [^aeiou]e--SFX L Y 1-SFX L 0 ment .--SFX O Y 1-SFX O 0 ful .--SFX Q Y 1-SFX Q 0 ally .--REP 90-REP a ei-REP ei a-REP a ey-REP ey a-REP ai ie-REP ie ai-REP alot a_lot-REP are air-REP are ear-REP are eir-REP air are-REP air ere-REP ere air-REP ere ear-REP ere eir-REP ear are-REP ear air-REP ear ere-REP eir are-REP eir ere-REP ch te-REP te ch-REP ch ti-REP ti ch-REP ch tu-REP tu ch-REP ch s-REP s ch-REP ch k-REP k ch-REP f ph-REP ph f-REP gh f-REP f gh-REP i igh-REP igh i-REP i uy-REP uy i-REP i ee-REP ee i-REP j di-REP di j-REP j gg-REP gg j-REP j ge-REP ge j-REP s ti-REP ti s-REP s ci-REP ci s-REP k cc-REP cc k-REP k qu-REP qu k-REP kw qu-REP o eau-REP eau o-REP o ew-REP ew o-REP oo ew-REP ew oo-REP ew ui-REP ui ew-REP oo ui-REP ui oo-REP ew u-REP u ew-REP oo u-REP u oo-REP u oe-REP oe u-REP u ieu-REP ieu u-REP ue ew-REP ew ue-REP uff ough-REP oo ieu-REP ieu oo-REP ier ear-REP ear ier-REP ear air-REP air ear-REP w qu-REP qu w-REP z ss-REP ss z-REP shun tion-REP shun sion-REP shun cion-REP size cisediff --git a/harper-core/src/spell/full_dictionary.rs b/harper-core/src/spell/full_dictionary.rsindex db7530b0c..c264176ac 100644--- a/harper-core/src/spell/full_dictionary.rs+++ b/harper-core/src/spell/full_dictionary.rs@@ -23,12 +23,12 @@ pub struct FullDictionary {/// that has a word whose index is that length.word_len_starts: Vec<usize>,/// All English words- word_set: HashSet<CharString>+ word_set: HashSet<CharString>,}fn uncached_inner_new() -> FullDictionary {let word_list = parse_default_word_list().unwrap();- let attr_list = parse_default_attribute_list().unwrap();+ let attr_list = parse_default_attribute_list();// There will be at _least_ this number of wordslet mut words = Vec::with_capacity(word_list.len());@@ -41,7 +41,7 @@ fn uncached_inner_new() -> FullDictionary {FullDictionary {word_set: HashSet::from_iter(words.iter().cloned()),word_len_starts: FullDictionary::create_len_starts(&mut words),- words+ words,}}@@ -52,7 +52,7 @@ impl FullDictionary {Self {words: Vec::new(),word_len_starts: Vec::new(),- word_set: HashSet::new()+ word_set: HashSet::new(),}}diff --git a/harper-core/src/spell/hunspell/attribute_list.rs b/harper-core/src/spell/hunspell/attribute_list.rsindex 7b521b0f9..0626663c4 100644--- a/harper-core/src/spell/hunspell/attribute_list.rs+++ b/harper-core/src/spell/hunspell/attribute_list.rs@@ -15,22 +15,6 @@ pub struct AttributeList {}impl AttributeList {- pub fn parse(file: &str) -> Result<Self, Error> {- let mut output = Self {- affixes: HashMap::default(),- };-- for line in file.lines() {- if line.chars().filter(|c| !c.is_whitespace()).count() == 0 {- continue;- }-- output.parse_line(line)?;- }-- Ok(output)- }-pub fn to_human_readable(&self) -> HumanReadableAttributeList {HumanReadableAttributeList {affixes: self@@ -41,66 +25,6 @@ impl AttributeList {}}- fn parse_line(&mut self, line: &str) -> Result<(), Error> {- if line.len() < 4 {- return Ok(());- }-- let mut parser = AttributeArgParser::new(line);-- let suffix = match parser.parse_arg()? {- "PFX" => false,- "SFX" => true,- _ => return Ok(()),- };-- let flag = {- let flag_arg = parser.parse_arg()?;- if flag_arg.len() != 1 {- return Err(Error::MultiCharacterFlag);- };-- flag_arg.chars().next().unwrap()- };-- if let Some(expansion) = self.affixes.get_mut(&flag) {- let remove_arg = parser.parse_arg()?;-- let remove: Vec<_> = remove_arg.chars().collect();- let remove = if remove.len() == 1 && remove[0] == '0' {- vec![]- } else {- remove- };-- let add = parser.parse_arg()?.chars().collect();- let condition = Matcher::parse(parser.parse_arg()?)?;-- let replacement = AffixReplacement {- remove,- add,- condition,- };-- expansion.replacements.push(replacement)- } else {- let cross_product = parser.parse_bool_arg()?;- let count = parser.parse_usize_arg()?;-- self.affixes.insert(- flag,- Expansion {- suffix,- cross_product,- replacements: Vec::with_capacity(count),- adds_metadata: WordMetadata::default(),- },- );- }-- Ok(())- }-/// Expand [`MarkedWord`] into a list of full words, including itself.////// Will append to the given `dest`;@@ -250,65 +174,3 @@ impl HumanReadableAttributeList {Ok(AttributeList { affixes })}}--struct AttributeArgParser<'a> {- line: &'a str,- cursor: usize,-}--impl<'a> AttributeArgParser<'a> {- pub fn new(line: &'a str) -> Self {- Self { line, cursor: 0 }- }-- // Grab next affix argument, returning an error if it doesn't exist.- fn parse_arg(&mut self) -> Result<&'a str, Error> {- let Some((next_word_start, _)) = self.line[self.cursor..]- .char_indices()- .find(|(_i, c)| !c.is_whitespace())- else {- return Err(Error::UnexpectedEndOfLine);- };-- let next_word_end = self.line[self.cursor + next_word_start..]- .char_indices()- .find(|(_i, c)| c.is_whitespace())- .map(|(end, _)| end)- .unwrap_or(self.line.len() - self.cursor - next_word_start);-- let abs_start = next_word_start + self.cursor;- let abs_end = next_word_start + self.cursor + next_word_end;-- self.cursor = abs_end;-- Ok(&self.line[abs_start..abs_end])- }-- // Grab next affix argument, returning an error if it isn't parsable as a- // number.- fn parse_usize_arg(&mut self) -> Result<usize, Error> {- self.parse_arg()?- .parse()- .map_err(|_| Error::ExpectedUnsignedInteger)- }-- // Grab next affix argument, returning an error if it isn't Y or N.- fn parse_bool_arg(&mut self) -> Result<bool, Error> {- match self.parse_arg()? {- "Y" => Ok(true),- "N" => Ok(false),- _ => Err(Error::ExpectedBoolean),- }- }-}--#[cfg(test)]-mod tests {- use super::super::tests::ATTR_LIST;- use super::AttributeList;-- #[test]- fn can_parse_test_file() {- AttributeList::parse(ATTR_LIST).unwrap();- }-}diff --git a/harper-core/src/spell/hunspell/mod.rs b/harper-core/src/spell/hunspell/mod.rsindex 8e0ea9ef2..5a7d2c559 100644--- a/harper-core/src/spell/hunspell/mod.rs+++ b/harper-core/src/spell/hunspell/mod.rs@@ -6,6 +6,7 @@ mod matcher;mod word_list;pub use attribute_list::AttributeList;+use attribute_list::HumanReadableAttributeList;pub use error::Error;use self::word_list::parse_word_list;@@ -15,37 +16,71 @@ pub fn parse_default_word_list() -> Result<Vec<MarkedWord>, Error> {parse_word_list(include_str!("../../../dictionary.dict"))}-pub fn parse_default_attribute_list() -> Result<AttributeList, Error> {- AttributeList::parse(include_str!("../../../dictionary.aff"))+pub fn parse_default_attribute_list() -> AttributeList {+ let human_readable: HumanReadableAttributeList =+ serde_json::from_str(include_str!("../../../affixes.json"))+ .expect("The built-in affix list should always be valid.");++ human_readable+ .to_normal()+ .expect("All expressions in the built-in attribute list should be valid.")}#[cfg(test)]mod tests {- use std::fs;+ use serde_json::json;- use super::attribute_list::AttributeList;use super::word_list::parse_word_list;use super::{parse_default_attribute_list, parse_default_word_list};+ use crate::spell::hunspell::attribute_list::HumanReadableAttributeList;use crate::CharString;pub const TEST_WORD_LIST: &str = "3\nhello\ntry/B\nwork/AB";- pub const ATTR_LIST: &str =- "SET UTF-8\nTRY esianrtolcdugmphbyfvkwzESIANRTOLCDUGMPHBYFVKWZ'\n\nREP 2\nREP f ph\nREP \- ph f\n\nPFX A Y 1\nPFX A 0 re .\n\nSFX B Y 2\nSFX B 0 ed [^y]\nSFX B y ied y";-- #[test]- fn dump() {- let default = parse_default_attribute_list().unwrap();- let default_human_readable = default.to_human_readable();-- let dumped = serde_json::to_string_pretty(&default_human_readable).unwrap();- fs::write("affixes.json", dumped.as_bytes());- }#[test]fn correctly_expands_test_files() {let words = parse_word_list(TEST_WORD_LIST).unwrap();- let attributes = AttributeList::parse(ATTR_LIST).unwrap();+ let attributes: HumanReadableAttributeList = serde_json::from_value(json!({+ "affixes": {+ "A": {+ "suffix": false,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "re",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "B": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "ed",+ "condition": "[^y]"+ },+ {+ "remove": "y",+ "add": "ied",+ "condition": "y"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ }+ }+ }))+ .unwrap();+ let attributes = attributes.to_normal().unwrap();let mut expanded = Vec::new();@@ -64,12 +99,53 @@ mod tests {#[test]fn plural_giants() {let words = parse_word_list("1\ngiant/SM").unwrap();- let attributes = AttributeList::parse(- "SFX S Y 4\nSFX S y ies [^aeiou]y\nSFX S 0 s \- [aeiou]y\nSFX S 0 es [sxzh]\nSFX S 0 s [^sxzhy]\n\nSFX \- M Y 1\nSFX M 0 's .",- )++ let attributes: HumanReadableAttributeList = serde_json::from_value(json!({+ "affixes": {+ "S": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "y",+ "add": "ies",+ "condition": "[^aeiou]"+ },+ {+ "remove": "",+ "add": "s",+ "condition": "[aeiou]y"+ },+ {+ "remove": "",+ "add": "s",+ "condition": "[^sxzhy]"+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ },+ "M": {+ "suffix": true,+ "cross_product": true,+ "replacements": [+ {+ "remove": "",+ "add": "'s",+ "condition": "."+ }+ ],+ "adds_metadata": {+ "kind": null,+ "tense": null+ }+ }+ }+ })).unwrap();+ let attributes = attributes.to_normal().unwrap();let mut expanded = Vec::new();@@ -80,7 +156,7 @@ mod tests {fn build_expanded() -> Vec<CharString> {let words = parse_default_word_list().unwrap();- let attributes = parse_default_attribute_list().unwrap();+ let attributes = parse_default_attribute_list();let mut expanded = Vec::new();