Skip to content

Commit

Permalink
added modifier and stem parsing to get list
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Feb 21, 2024
1 parent dbfd08d commit fd66013
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/use_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use self::parsers::attachment_parser::parse_attachments;
use self::parsers::english_dictionary_parser::parse_english_dictionary;
use self::parsers::latin_dictionary_parser::parse_latin_dictionary;
use self::parsers::latin_inflection_parser::parse_latin_inflections;
use self::parsers::modifiers_parser::parse_modifiers;
use self::parsers::stem_parser::parse_latin_stems;
use self::parsers::unique_latin_dictionary_parser::parse_unique_latin_words;
use crate::dictionary_structures::dictionary_keys::PartOfSpeech;
use crate::dictionary_structures::dictionary_values::{
Attachment, EnglishWordInfo, Inflection, LatinWordInfo,
Attachment, EnglishWordInfo, Inflection, LatinWordInfo, Modifier, Stem,
};
use crate::utils::data::{
get_latin_not_packons, get_latin_packons, get_latin_tackons, get_latin_tickons,
get_latin_not_packons, get_latin_packons, get_latin_prefixes, get_latin_suffixes,
get_latin_tackons, get_latin_tickons,
};
use serde::Serialize;
use serde_json;
Expand All @@ -18,6 +21,8 @@ mod parsers {
pub mod english_dictionary_parser;
pub mod latin_dictionary_parser;
pub mod latin_inflection_parser;
pub mod modifiers_parser;
pub mod stem_parser;
pub mod unique_latin_dictionary_parser;
}

Expand Down Expand Up @@ -64,6 +69,8 @@ pub enum OutputList {
English(Vec<EnglishWordInfo>),
Inflections(Vec<Inflection>),
Attachment(Vec<Attachment>),
Modifiers(Vec<Modifier>),
Stems(Vec<Stem>),
}

pub fn get_list(
Expand Down Expand Up @@ -100,6 +107,20 @@ pub fn get_list(
let list = parse_attachments(attachments, None, max, min, exact, amount, random);
OutputList::Attachment(list)
}
WordType::Prefixes => {
let modifiers = get_latin_prefixes();
let list = parse_modifiers(modifiers, pos_list, max, min, exact, amount, random);
OutputList::Modifiers(list)
}
WordType::Stems => {
let list = parse_latin_stems(pos_list, max, min, exact, amount, random);
OutputList::Stems(list)
}
WordType::Suffixes => {
let modifiers = get_latin_suffixes();
let list = parse_modifiers(modifiers, pos_list, max, min, exact, amount, random);
OutputList::Modifiers(list)
}
WordType::Tackons => {
let attachments = get_latin_tackons();
let list = parse_attachments(attachments, None, max, min, exact, amount, random);
Expand All @@ -114,7 +135,6 @@ pub fn get_list(
let list = parse_unique_latin_words(pos_list, max, min, exact, amount, random);
OutputList::Latin(list)
}
_ => unimplemented!(),
};
if display {
println!("{}", serde_json::to_string_pretty(&list).unwrap());
Expand Down
72 changes: 72 additions & 0 deletions src/use_data/parsers/modifiers_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::dictionary_structures::dictionary_keys::PartOfSpeech;
use crate::dictionary_structures::dictionary_values::Modifier;
use crate::use_data::utils::word_fits_filters;
use rand::Rng;

pub fn parse_modifiers(
modifiers: Vec<Modifier>,
pos_list: Option<Vec<PartOfSpeech>>,
max: Option<i32>,
min: Option<i32>,
exact: Option<i32>,
amount: Option<i32>,
random: bool,
) -> Vec<Modifier> {
let mut modifier_list: Vec<Modifier> = Vec::new();

if let Some(amount) = amount {
if random {
let mut rng = rand::thread_rng();
while modifier_list.len() as i32 != amount {
let random_index = rng.gen_range(0..modifiers.len());
let modifier_at_index = modifiers[random_index].clone();
if !word_fits_filters(
&modifier_at_index.orth,
&modifier_at_index.pos,
&pos_list,
&max,
&min,
&exact,
) {
continue;
}
modifier_list.push(modifier_at_index);
}
} else {
for modifier in modifiers {
if !word_fits_filters(
&modifier.orth,
&modifier.pos,
&pos_list,
&max,
&min,
&exact,
) {
continue;
}

modifier_list.push(modifier);
if modifier_list.len() as i32 == amount {
break;
}
}
}
} else {
for modifier in modifiers {
if !word_fits_filters(
&modifier.orth,
&modifier.pos,
&pos_list,
&max,
&min,
&exact,
) {
continue;
}

modifier_list.push(modifier);
}
}

modifier_list
}
59 changes: 59 additions & 0 deletions src/use_data/parsers/stem_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::dictionary_structures::dictionary_keys::PartOfSpeech;
use crate::dictionary_structures::dictionary_values::Stem;
use crate::use_data::utils::word_fits_filters;
use crate::utils::data::get_latin_stems;
use rand::Rng;

pub fn parse_latin_stems(
pos_list: Option<Vec<PartOfSpeech>>,
max: Option<i32>,
min: Option<i32>,
exact: Option<i32>,
amount: Option<i32>,
random: bool,
) -> Vec<Stem> {
let latin_stems = get_latin_stems();
let mut stem_list: Vec<Stem> = Vec::new();

if let Some(amount) = amount {
if random {
let mut rng = rand::thread_rng();
while stem_list.len() as i32 != amount {
let random_index = rng.gen_range(0..latin_stems.len());
let stem_at_index = latin_stems[random_index].clone();
if !word_fits_filters(
&stem_at_index.orth,
&stem_at_index.pos,
&pos_list,
&max,
&min,
&exact,
) {
continue;
}
stem_list.push(stem_at_index);
}
} else {
for stem in latin_stems {
if !word_fits_filters(&stem.orth, &stem.pos, &pos_list, &max, &min, &exact) {
continue;
}

stem_list.push(stem);
if stem_list.len() as i32 == amount {
break;
}
}
}
} else {
for stem in latin_stems {
if !word_fits_filters(&stem.orth, &stem.pos, &pos_list, &max, &min, &exact) {
continue;
}

stem_list.push(stem);
}
}

stem_list
}

0 comments on commit fd66013

Please sign in to comment.