Skip to content

Commit

Permalink
get list for attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Feb 21, 2024
1 parent 56b9ac4 commit dbfd08d
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 10 deletions.
27 changes: 27 additions & 0 deletions src/dictionary_structures/dictionary_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,33 @@ impl Attachment {
}
}

impl Serialize for Attachment {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
let mut map = serde_json::Map::new();
map.insert(
"pos".to_string(),
serde_json::Value::String(self.pos.as_str().to_string()),
);
map.insert(
"senses".to_string(),
serde_json::Value::Array(
self.senses
.iter()
.map(|s| serde_json::Value::String(s.to_string()))
.collect(),
),
);
map.insert(
"orth".to_string(),
serde_json::Value::String(self.orth.to_string()),
);
serde_json::Value::Object(map).serialize(serializer)
}
}

impl<'de> Deserialize<'de> for Attachment {
fn deserialize<D>(deserializer: D) -> Result<Attachment, D::Error>
where
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,21 @@ fn main() {
if type_of_words != "english"
&& type_of_words != "latin"
&& type_of_words != "inflections"
&& type_of_words != "inflection"
&& type_of_words != "not_packons"
&& type_of_words != "not_packon"
&& type_of_words != "packon"
&& type_of_words != "packons"
&& type_of_words != "prefixes"
&& type_of_words != "prefix"
&& type_of_words != "stems"
&& type_of_words != "stem"
&& type_of_words != "suffixes"
&& type_of_words != "suffix"
&& type_of_words != "tackons"
&& type_of_words != "tackon"
&& type_of_words != "tickons"
&& type_of_words != "tickon"
&& type_of_words != "unique_latin"
{
println!(
Expand Down
55 changes: 45 additions & 10 deletions src/use_data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
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::unique_latin_dictionary_parser::parse_unique_latin_words;
use crate::dictionary_structures::dictionary_keys::PartOfSpeech;
use crate::dictionary_structures::dictionary_values::{EnglishWordInfo, LatinWordInfo};
use crate::dictionary_structures::dictionary_values::{
Attachment, EnglishWordInfo, Inflection, LatinWordInfo,
};
use crate::utils::data::{
get_latin_not_packons, get_latin_packons, get_latin_tackons, get_latin_tickons,
};
use serde::Serialize;
use serde_json;

mod parsers {
pub mod attachment_parser;
pub mod english_dictionary_parser;
pub mod latin_dictionary_parser;
pub mod latin_inflection_parser;
pub mod unique_latin_dictionary_parser;
}

Expand All @@ -33,15 +42,15 @@ impl WordType {
pub fn from_str(s: &str) -> Result<WordType, String> {
match s {
"english" => Ok(WordType::English), // done
"latin" => Ok(WordType::Latin), // done
"inflections" => Ok(WordType::Inflections),
"not_packons" => Ok(WordType::NotPackons),
"packons" => Ok(WordType::Packons),
"prefixes" => Ok(WordType::Prefixes),
"stems" => Ok(WordType::Stems),
"suffixes" => Ok(WordType::Suffixes),
"tackons" => Ok(WordType::Tackons),
"tickons" => Ok(WordType::Tickons),
"latin" => Ok(WordType::Latin), // done
"inflections" | "inflection" => Ok(WordType::Inflections),
"not_packons" | "not_packon" => Ok(WordType::NotPackons),
"packons" | "packon" => Ok(WordType::Packons),
"prefixes" | "prefix" => Ok(WordType::Prefixes),
"stems" | "stem" => Ok(WordType::Stems),
"suffixes" | "suffix" => Ok(WordType::Suffixes),
"tackons" | "tackon" => Ok(WordType::Tackons),
"tickons" | "tickon" => Ok(WordType::Tickons),
"unique_latin" => Ok(WordType::UniqueLatin), // done
_ => Err(format!("Invalid word type: {}", s)),
}
Expand All @@ -53,6 +62,8 @@ impl WordType {
pub enum OutputList {
Latin(Vec<LatinWordInfo>),
English(Vec<EnglishWordInfo>),
Inflections(Vec<Inflection>),
Attachment(Vec<Attachment>),
}

pub fn get_list(
Expand All @@ -75,6 +86,30 @@ pub fn get_list(
let list = parse_latin_dictionary(pos_list, max, min, exact, amount, random);
OutputList::Latin(list)
}
WordType::Inflections => {
let list = parse_latin_inflections(pos_list, max, min, exact, amount, random);
OutputList::Inflections(list)
}
WordType::NotPackons => {
let attachments = get_latin_not_packons();
let list = parse_attachments(attachments, None, max, min, exact, amount, random);
OutputList::Attachment(list)
}
WordType::Packons => {
let attachments = get_latin_packons();
let list = parse_attachments(attachments, None, max, min, exact, amount, random);
OutputList::Attachment(list)
}
WordType::Tackons => {
let attachments = get_latin_tackons();
let list = parse_attachments(attachments, None, max, min, exact, amount, random);
OutputList::Attachment(list)
}
WordType::Tickons => {
let attachments = get_latin_tickons();
let list = parse_attachments(attachments, None, max, min, exact, amount, random);
OutputList::Attachment(list)
}
WordType::UniqueLatin => {
let list = parse_unique_latin_words(pos_list, max, min, exact, amount, random);
OutputList::Latin(list)
Expand Down
72 changes: 72 additions & 0 deletions src/use_data/parsers/attachment_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::Attachment;
use crate::use_data::utils::word_fits_filters;
use rand::Rng;

pub fn parse_attachments(
attachments: Vec<Attachment>,
pos_list: Option<Vec<PartOfSpeech>>,
max: Option<i32>,
min: Option<i32>,
exact: Option<i32>,
amount: Option<i32>,
random: bool,
) -> Vec<Attachment> {
let mut attachment_list: Vec<Attachment> = Vec::new();

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

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

attachment_list.push(attachment);
}
}

attachment_list
}
73 changes: 73 additions & 0 deletions src/use_data/parsers/latin_inflection_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::dictionary_structures::dictionary_keys::PartOfSpeech;
use crate::dictionary_structures::dictionary_values::Inflection;
use crate::use_data::utils::word_fits_filters;
use crate::utils::data::get_latin_inflections;
use rand::Rng;

pub fn parse_latin_inflections(
pos_list: Option<Vec<PartOfSpeech>>,
max: Option<i32>,
min: Option<i32>,
exact: Option<i32>,
amount: Option<i32>,
random: bool,
) -> Vec<Inflection> {
let latin_inflections = get_latin_inflections();
let mut inflection_list: Vec<Inflection> = Vec::new();

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

inflection_list.push(inflection);
if inflection_list.len() as i32 == amount {
break;
}
}
}
} else {
for inflection in latin_inflections {
if !word_fits_filters(
&inflection.ending,
&inflection.pos,
&pos_list,
&max,
&min,
&exact,
) {
continue;
}

inflection_list.push(inflection);
}
}

inflection_list
}

0 comments on commit dbfd08d

Please sign in to comment.