From 2f58dfab420c1818556c51321c9dc0b4fe5e1e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20Du=C3=9Fin?= <arne.dussin@live.de> Date: Tue, 1 May 2018 14:17:49 +0200 Subject: [PATCH] Split the Word struct into the classes/types The word struct previously described every word imaginable in the German language. However moving on it became apparent that it is much more sensible to split it into Nouns/Verbs/Adjectives (possibly more in the future) since rules correspond to these classes. --- src/german/word.rs | 212 ----------------------------------- src/german/word/adjective.rs | 62 ++++++++++ src/german/word/mod.rs | 80 +++++++++++++ src/german/word/noun.rs | 71 ++++++++++++ src/german/word/verb.rs | 84 ++++++++++++++ 5 files changed, 297 insertions(+), 212 deletions(-) delete mode 100644 src/german/word.rs create mode 100644 src/german/word/adjective.rs create mode 100644 src/german/word/mod.rs create mode 100644 src/german/word/noun.rs create mode 100644 src/german/word/verb.rs diff --git a/src/german/word.rs b/src/german/word.rs deleted file mode 100644 index e82b37a..0000000 --- a/src/german/word.rs +++ /dev/null @@ -1,212 +0,0 @@ -// Copyright (C) 2018 Arne Dußin. -// -// This Program may be used by anyone in accordance with the terms of the -// German Free Software License -// -// The License may be obtained under http://www.d-fsl.org. - -use std::ops::Deref; - -use definable::Definable; - -/// Representation of a German grammatical word type. -#[allow(missing_docs)] -#[derive(Copy, Clone)] -pub enum WordType { - Noun, - Verb, - Adjective -} - -/// Representation of a German grammatical gender. -#[allow(missing_docs)] -#[derive(Copy, Clone)] -pub enum Gender { - None, - Masculine, - Feminine, - Neuter -} - -/// Representation of a German grammatical number. -#[allow(missing_docs)] -#[derive(Copy, Clone)] -pub enum Number { - None, - Singular, - Plural -} - -/// Representation of a German grammatical case. -#[allow(missing_docs)] -#[derive(Copy, Clone)] -pub enum Case { - None, - Nominative, - Genitive, - Dative, - Accusative -} - -// Representation of a German grammatical person. -#[allow(missing_docs)] -#[derive(Copy, Clone)] -pub enum Person { - None, - First, - Second, - Third -} - -/// Representation of a German grammatical mood. -#[allow(missing_docs)] -#[derive(Copy, Clone)] -pub enum Mood { - None, - Indicative, - ConjunctiveOne, - ConjunctiveTwo -} - -/// Representation of a German grammatical genera. -#[allow(missing_docs)] -#[derive(Copy, Clone)] -pub enum Genera { - None, - Active, - Passive -} - -/// Describes all parts that may or may not be known about a german word. -#[derive(Clone)] -pub struct Word { - /// The raw string that describes this word. - raw: String, - /// Contains the definition, if it is available - definition: Option<String>, - /// The type of the word. Some, if it is known - word_type: Option<WordType>, - /// The grammatical gender of the word. - gender: Option<Gender>, - /// The grammatical number of the word. - number: Option<Number>, - /// The grammatical case of the word. - case: Option<Case>, - /// The grammatical person of the word. - person: Option<Person>, - /// the grammatical mood of the word. - mood: Option<Mood>, - /// The grammatical word of the genera. - genera: Option<Genera> -} - -impl Word { - /// Create a new word from its String representation. Everything else is - /// set to unknown. - pub fn new<R: AsRef<str>>(raw: R) -> Word { - Word { - raw: raw.as_ref().to_string(), - definition: None, - word_type: None, - gender: None, - number: None, - case: None, - person: None, - mood: None, - genera: None - } - } - - /// Get the grammatical type of a word. None, if the type is unknown. - pub fn word_type(&self) -> Option<WordType> { - self.word_type - } - - /// Get the grammatical gender of a word. None, if the gender is unknown. - pub fn gender(&self) -> Option<Gender> { - self.gender - } - - /// Get the grammatical number of a word. None, if the number is unknown. - pub fn number(&self) -> Option<Number> { - self.number - } - - /// Get the grammatical case of a word. None, if the case is unknown. - pub fn case(&self) -> Option<Case> { - self.case - } - - /// Get the grammatical person of a word. None, if the case is unknown. - pub fn person(&self) -> Option<Person> { - self.person - } - - //7 Get the grammatical mood of a word. None, if the case is unknown. - pub fn mood(&self) -> Option<Mood> { - self.mood - } - - /// Get the grammatical genera of a word. None, if the case is unknown. - pub fn genera(&self) -> Option<Genera> { - self.genera - } - - /// Used to set the grammatical type of a word. Can also be used to reset it. - pub fn set_word_type(&mut self, word_type: WordType) { - self.word_type = Some(word_type); - } - - /// Used to set the grammatical gender of a word. Can also be used to reset it. - pub fn set_gender(&mut self, gender: Gender) { - self.gender = Some(gender); - } - - /// Used to set the grammatical number of a word. Can also be used to reset it. - pub fn set_number(&mut self, number: Number) { - self.number = Some(number); - } - - /// Used to set the grammatical case of a word. Can also be used to reset it. - pub fn set_case(&mut self, case: Case) { - self.case = Some(case); - } - - /// Used to set the grammatical person of a word. Can also be used to reset it. - pub fn set_person(&mut self, person: Person) { - self.person = Some(person); - } - - /// Used to set the grammatical mood of a word. Can also be used to reset it. - pub fn set_mood(&mut self, mood: Mood) { - self.mood = Some(mood); - } - - /// Used to set the grammatical genera of a word. Can also be used to reset it. - pub fn set_genera(&mut self, genera: Genera) { - self.genera = Some(genera); - } -} - -impl Definable for Word { - type Def = String; - - fn definition(&self) -> Option<&String> { - match &self.definition { - &Some(ref def) => Some(&def), - &None => None - } - } - - fn set_definition(&mut self, definition: &String) { - self.definition = Some(definition.clone()); - } -} - -impl Deref for Word { - type Target = str; - - fn deref(&self) -> &str { - &self.raw - } -} diff --git a/src/german/word/adjective.rs b/src/german/word/adjective.rs new file mode 100644 index 0000000..30e7c54 --- /dev/null +++ b/src/german/word/adjective.rs @@ -0,0 +1,62 @@ +// Copyright (C) 2018 Arne Dußin. +// +// This Program may be used by anyone in accordance with the terms of the +// German Free Software License +// +// The License may be obtained under http://www.d-fsl.org. + +use super::{Definition, Gender, Number, Word}; +use definable::Definable; + +#[derive(Clone)] +pub struct Adjective { + /// The raw String that describes this word + raw: String, + /// Contains the definition, if available + definition: Option<Definition>, + /// The Genus this adjective is in or None in case it is in the base form + gender: Option<Gender>, + /// If available or applicable, this describes if the Noun this describes is + /// Singular or Plural + number: Option<Number> +} + +impl Adjective { + /// Create a new Adjective from its String representation. Everything else + /// is set to unknown. + pub fn new<R: AsRef<str>>(raw: R) -> Verb { + Verb { + raw: raw.as_ref().to_string(), + definition: None, + gender: None, + number: None + } + } + + /// The gender of the thing this Adjective describes if it is known and + /// applicable. + pub fn gender(&self) -> Option<Gender> { self.gender } + + /// If applicable, is this verb in singular or plural + pub fn number(&self) -> Option<Number> { self.number } +} + +impl Word for Verb { + /// The word class is always Adjective. + fn class(&self) -> Class { Class::Adjective } + + fn raw(&self) -> &str { &self.raw } +} + +impl Definable for Adjective { + fn definition(&self) -> Option<&Definition> { + match &self.definition { + &Some(ref def) => Some(&def), + &None => None + } + } + + fn set_definition(&mut self, definition: &Definition) { + self.definition = Some(definition.clone()); + } +} diff --git a/src/german/word/mod.rs b/src/german/word/mod.rs new file mode 100644 index 0000000..f420413 --- /dev/null +++ b/src/german/word/mod.rs @@ -0,0 +1,80 @@ +// Copyright (C) 2018 Arne Dußin. +// +// This Program may be used by anyone in accordance with the terms of the +// German Free Software License +// +// The License may be obtained under http://www.d-fsl.org. + +//! Module for word operations in the German language. + +pub mod noun; +pub use self::noun::*; + +pub mod verb; +pub use self::verb::*; + +use std::ops::Deref; + +use definable::Definable; + +/// Representation of a German grammatical word class/type. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Class { + Noun, + Verb, + Adjective +} + +/// Representation of a German grammatical gender. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Gender { + Masculine, + Feminine, + Neuter +} + +/// Representation of a German grammatical number. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Number { + Singular, + Plural +} + +// Representation of a German grammatical person. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Person { + First, + Second, + Third +} + +/// Representation of a German grammatical mood. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Mood { + Indicative, + ConjunctiveOne, + ConjunctiveTwo +} + +pub type Definition = String; + +pub trait Word<Def=Definition>: Definable { + /// The word class aka. type this word is known as. + fn class(&self) -> Class; + + /// Get the raw string representation of the word in its current form. + fn raw(&self) -> &str; +} + +impl Deref for Word<Def=Definition> { + type Target = str; + +fn deref(&self) -> &str { + self.raw() + } +} diff --git a/src/german/word/noun.rs b/src/german/word/noun.rs new file mode 100644 index 0000000..97114ba --- /dev/null +++ b/src/german/word/noun.rs @@ -0,0 +1,71 @@ +// Copyright (C) 2018 Arne Dußin. +// +// This Program may be used by anyone in accordance with the terms of the +// German Free Software License +// +// The License may be obtained under http://www.d-fsl.org. + +use super::{Definition, Gender, Number}; +use definable::Definable; + +/// The case of a German noun +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Case { + Nominative, + Genitive, + Dative, + Accusative +} + +/// A specific German noun. Some words can be derived from this if they are +/// sufficiently similar to it, especially if they share a stem. +pub struct Noun { + /// The raw String that describes this word + raw: String, + /// Contains the definition, if it is available + definition: Option<Definition>, + /// The gender of this Noun, if it is known + gender: Option<Gender>, + /// If known, contains if this Noun is in Plural or Singular form + number: Option<Number>, + /// The grammatical case this Noun is currently in + case: Option<Case> +} + +impl Noun { + /// Create a new Noun from its String representation. Everything else is set + /// to unknown. + pub fn new<R: AsRef<str>>(raw: R) -> Noun { + Noun { + raw: raw.as_ref().to_string(), + definition: None, + gender: None, + number: None, + case: None + } + } + + /// The grammatical Gender of this Noun if known. + pub fn gender(&self) -> Option<Gender> { self.gender } + + /// Get if this noun is plural or singular. None if it is unknown. + pub fn number(&self) -> Option<Number> { self.number } + + /// The case this Noun is in if it is known. + pub fn case(&self) -> Option<Case> { self.case } +} + +impl Definable for Noun { + fn definition(&self) -> Option<&Definition> { + match &self.definition { + &Some(ref def) => Some(&def), + &None => None + } + } + + fn set_definition(&mut self, definition: &Definition) { + self.definition = Some(definition.clone()); + } +} + diff --git a/src/german/word/verb.rs b/src/german/word/verb.rs new file mode 100644 index 0000000..26d3475 --- /dev/null +++ b/src/german/word/verb.rs @@ -0,0 +1,84 @@ +// Copyright (C) 2018 Arne Dußin. +// +// This Program may be used by anyone in accordance with the terms of the +// German Free Software License +// +// The License may be obtained under http://www.d-fsl.org. + +use super::{Class, Definition, Word}; +use definable::Definable; + +// Representation of a German grammatical person. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Person { + First, + Second, + Third +} + +/// Representation of a German grammatical mood. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum Mood { + Indicative, + ConjunctiveOne, + ConjunctiveTwo +} + +/// The Genus of a verb, part of diathesis. +#[allow(missing_docs)] +#[derive(Copy, Clone)] +pub enum VerbGender { + Active, + Passive +} + +#[derive(Clone)] +pub struct Verb { + /// The raw String that describes this word + raw: String, + /// Contains the definition, if available + definition: Option<Definition>, + /// The person this verb stands in, if available + person: Option<Person>, + /// The mood of the verb, if available + mood: Option<Mood> +} + +impl Verb { + /// Create a new Verb from its String representation. Everything else is set + /// to unknown. + pub fn new<R: AsRef<str>>(raw: R) -> Verb { + Verb { + raw: raw.as_ref().to_string(), + definition: None, + person: None, + mood: None + } + } + + /// The person which is acting with this verb, if it is known + pub fn person(&self) -> Option<Person> { self.person } + + /// The mood of the verb, such as hearsay mode etc. + pub fn mood(&self) -> Option<Mood> { self.mood } +} + +impl Word for Verb { + /// The word class is always verb. + fn class(&self) -> Class { Class::Verb } +} + +impl Definable for Verb { + fn definition(&self) -> Option<&Definition> { + match &self.definition { + &Some(ref def) => Some(&def), + &None => None + } + } + + fn set_definition(&mut self, definition: &Definition) { + self.definition = Some(definition.clone()); + } +} -- GitLab