use super::{
relation::{LexicalRelation, SemanticRelation},
PartOfSpeech, WordNet,
};
#[derive(Debug)]
pub struct SynSet {
pub lemmas: Vec<Lemma>,
pub definition: String,
pub examples: Vec<String>,
pub part_of_speech: PartOfSpeech,
pub relationships: Vec<SemanticRelationship>,
}
#[derive(Debug)]
pub struct SemanticRelationship {
pub relation: SemanticRelation,
pub synset_offset: u64,
pub part_of_speech: PartOfSpeech,
}
#[derive(Debug)]
pub struct LexicalRelationship {
pub relation: LexicalRelation,
pub synset_offset: u64,
pub part_of_speech: PartOfSpeech,
pub target: usize,
}
impl SynSet {
pub fn with_relationship(&self, relation: SemanticRelation) -> Vec<&SemanticRelationship> {
self.relationships
.iter()
.filter(|r| r.relation == relation)
.collect()
}
pub fn synonyms(&self) -> Vec<String> {
self.lemmas.iter().map(|l| l.word.to_owned()).collect()
}
}
#[derive(Debug)]
pub struct Lemma {
pub word: String,
pub part_of_speech: PartOfSpeech,
pub relationships: Vec<LexicalRelationship>,
}
impl Lemma {
pub fn with_relationship(&self, relation: LexicalRelation) -> Vec<&LexicalRelationship> {
self.relationships
.iter()
.filter(|r| r.relation == relation)
.collect()
}
pub fn antonyms(&self, wn: &WordNet) -> Vec<String> {
let mut antonyms = self
.with_relationship(LexicalRelation::Antonym)
.iter()
.map(|r| {
(
r.target,
wn.resolve(r.part_of_speech, r.synset_offset)
.expect("Failed to resolve word from lemma relationship"),
)
})
.map(|(target, mut ss)| ss.lemmas.remove(target).word)
.collect::<Vec<_>>();
antonyms.sort_unstable();
antonyms.dedup();
antonyms
}
}