use crate::error::{Error, Result};
use crate::morpheme::Morpheme;
use crate::schema::Schema;
const MISSING: &str = "*";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Column {
Surface,
PartOfSpeech,
ConjugationType,
ConjugationForm,
BaseForm,
}
impl Column {
const ALL: [(Column, &'static str, u8); 5] = [
(Column::Surface, "surface", 0),
(Column::PartOfSpeech, "pos", 1),
(Column::ConjugationType, "ctype", 2),
(Column::ConjugationForm, "cform", 3),
(Column::BaseForm, "base", 4),
];
pub fn name(self) -> &'static str {
Self::ALL
.iter()
.find(|(column, _, _)| *column == self)
.map_or("", |(_, name, _)| name)
}
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL
.iter()
.find(|(_, other, _)| *other == name)
.map(|(column, _, _)| *column)
}
pub(crate) fn code(self) -> u8 {
Self::ALL
.iter()
.find(|(column, _, _)| *column == self)
.map_or(u8::MAX, |(_, _, code)| *code)
}
pub(crate) fn from_code(code: u8) -> Option<Self> {
Self::ALL
.iter()
.find(|(_, _, other)| *other == code)
.map(|(column, _, _)| *column)
}
pub(crate) fn value<M: Morpheme>(self, schema: &Schema, morpheme: &M) -> String {
let feature = |index: Option<usize>| {
index
.and_then(|index| morpheme.feature(index))
.unwrap_or(MISSING)
.to_string()
};
match self {
Column::Surface => schema.surface_of(morpheme).into_owned(),
Column::PartOfSpeech => schema.part_of_speech_of(morpheme),
Column::ConjugationType => feature(schema.conjugation_type),
Column::ConjugationForm => feature(schema.conjugation_form),
Column::BaseForm => feature(schema.base_form),
}
}
}
pub fn parse_columns(text: &str) -> Result<Vec<Column>> {
text.split(',')
.map(|name| {
let name = name.trim();
Column::from_name(name).ok_or_else(|| {
Error::Chunker(format!(
"unknown column `{name}`: expected surface, pos, ctype, cform or base"
))
})
})
.collect()
}
pub(crate) fn rows<M: Morpheme>(
schema: &Schema,
columns: &[Column],
morphemes: &[M],
) -> Vec<Vec<String>> {
morphemes
.iter()
.map(|morpheme| {
columns
.iter()
.map(|column| column.value(schema, morpheme))
.collect()
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lattice::LatticeMorpheme;
fn values(surface: &str, features: &str) -> Vec<String> {
let morpheme = LatticeMorpheme::new(surface, features);
let all: Vec<Column> = Column::ALL.iter().map(|(column, _, _)| *column).collect();
rows(&Schema::ipadic(), &all, &[morpheme]).remove(0)
}
#[test]
fn the_columns_of_a_verb() {
assert_eq!(
values("読ん", "動詞,自立,*,*,五段・マ行,連用タ接続,読む,ヨン,ヨン"),
["読ん", "動詞-自立", "五段・マ行", "連用タ接続", "読む"]
);
}
#[test]
fn the_part_of_speech_stops_at_the_first_missing_level() {
assert_eq!(
values("田中", "名詞,固有名詞,人名,姓,*,*,田中,タナカ,タナカ")[1],
"名詞-固有名詞-人名-姓"
);
assert_eq!(values("x", "名詞,*,一般,*")[1], "名詞");
assert_eq!(values("x", "*,一般")[1], "");
}
#[test]
fn the_columns_come_from_the_schema() {
let schema = Schema {
part_of_speech: 0..2,
conjugation_type: None,
conjugation_form: None,
base_form: Some(2),
..Schema::default()
};
let morpheme = LatticeMorpheme::new("読ん", "動詞,自立,読む,連用");
let columns = [
Column::PartOfSpeech,
Column::ConjugationForm,
Column::BaseForm,
];
assert_eq!(
rows(&schema, &columns, &[morpheme]),
[["動詞-自立", "*", "読む"]]
);
}
#[test]
fn the_surface_is_the_normalized_one_of_the_schema() {
let schema = Schema {
normalization: crate::schema::Normalization::new(&[("1", "1"), ("ガ", "ガ")]).unwrap(),
..Schema::ipadic()
};
let morpheme = LatticeMorpheme::new("1ガ", "名詞,数,*,*,*,*,*");
let columns = [Column::Surface, Column::PartOfSpeech];
assert_eq!(
rows(&schema, &columns, std::slice::from_ref(&morpheme)),
[["1ガ", "名詞-数"]]
);
assert_eq!(
rows(&Schema::ipadic(), &columns, &[morpheme]),
[["1ガ", "名詞-数"]]
);
}
#[test]
fn a_morpheme_with_too_few_columns_does_not_panic() {
assert_eq!(values("x", "名詞"), ["x", "名詞", "*", "*", "*"]);
assert_eq!(values("x", ""), ["x", "", "*", "*", "*"]);
}
#[test]
fn columns_have_names_and_codes() {
for (column, name, code) in Column::ALL {
assert_eq!(column.name(), name);
assert_eq!(Column::from_name(name), Some(column));
assert_eq!(column.code(), code);
assert_eq!(Column::from_code(code), Some(column));
}
assert_eq!(Column::from_name("lemma"), None);
assert_eq!(Column::from_code(9), None);
}
#[test]
fn reads_a_list_of_columns() {
assert_eq!(
parse_columns("surface, pos,cform").unwrap(),
[
Column::Surface,
Column::PartOfSpeech,
Column::ConjugationForm
]
);
assert_eq!(
parse_columns("surface,lemma").unwrap_err().to_string(),
"unknown column `lemma`: expected surface, pos, ctype, cform or base"
);
}
}