Skip to main content

Module parser

Module parser 

Source
Expand description

Parsing: finding, for each bunsetsu of a sentence, the bunsetsu it depends on.

The parser goes through the bunsetsu from left to right with a stack (shift-reduce), and asks one question at a time: does this bunsetsu, the source, depend on that one, the target? It is what CaboCha and J.DepP do. Every head is to the right of its bunsetsu, no two arcs cross, and the last bunsetsu is the root; the time is linear in the number of bunsetsu.

It is in three parts, so that each can be replaced:

  • The features of a decision are strings, with the names CaboCha gives them. They are made from the head and the function word of each bunsetsu, through the Schema of the dictionary. A FeatureSet says which Groups of features are made.
  • The classifier (Classifier) scores the features of a decision: the source depends on the target when the score is above 0. Every classifier here adds the weights of the features and of their pairs: ParserPart reads them on the bytes of a model file, ParserModel holds them in memory as training gives them, and LinearClassifier takes weights made by hand.
  • The loop asks the decisions. With a classifier it parses (Parser::parse); with the answer of a sentence it gives the training instances of a classifier (instances).

Parsing needs nothing but this crate. Training, behind the train feature, is train: dual coordinate descent as CaboCha does it, or passive-aggressive as J.DepP does.

The parser of a model file comes from Model::parser.

§Examples

A classifier made by hand: a bunsetsu that ends in を depends on the next one, and nothing else is attached before the end of the sentence.

use std::collections::HashMap;

use lindera_dependency::lattice::LatticeMorpheme;
use lindera_dependency::parser::{FeatureSet, LinearClassifier, Parser};
use lindera_dependency::Schema;

// SFS is the surface of the function word of the source, and DIST:1
// says that the target is the next bunsetsu.
let pairs = HashMap::from([(("SFS:を".to_string(), "DIST:1".to_string()), 2.0)]);
let classifier = LinearClassifier::new(-1.0, HashMap::new(), pairs)?;
let parser = Parser::new(Schema::ipadic(), FeatureSet::cabocha(), classifier);

// 彼 は / 本 を / 読ん だ / と / 言う
let morphemes = [
    LatticeMorpheme::new("彼", "名詞,代名詞,一般,*,*,*,彼,カレ,カレ"),
    LatticeMorpheme::new("は", "助詞,係助詞,*,*,*,*,は,ハ,ワ"),
    LatticeMorpheme::new("本", "名詞,一般,*,*,*,*,本,ホン,ホン"),
    LatticeMorpheme::new("を", "助詞,格助詞,一般,*,*,*,を,ヲ,ヲ"),
    LatticeMorpheme::new("読ん", "動詞,自立,*,*,五段・マ行,連用タ接続,読む,ヨン,ヨン"),
    LatticeMorpheme::new("だ", "助動詞,*,*,*,特殊・タ,基本形,だ,ダ,ダ"),
    LatticeMorpheme::new("と", "助詞,格助詞,引用,*,*,*,と,ト,ト"),
    LatticeMorpheme::new("言う", "動詞,自立,*,*,五段・ワ行促音便,基本形,言う,イウ,イウ"),
];
let bunsetsu = parser.parse(&morphemes, &[0..2, 2..4, 4..7, 7..8])?;

let heads: Vec<Option<usize>> = bunsetsu.iter().map(|one| one.head).collect();
assert_eq!(heads, [Some(3), Some(2), Some(3), None]);
// 本 を: the head word is 本, the function word を, and the score is
// the bias and the weight of the pair.
assert_eq!(bunsetsu[1].positions, Some((0, 1)));
assert_eq!(bunsetsu[1].score, Some(1.0));

Structs§

FeatureSet
The groups of features a model uses.
Instance
One decision of the parser, with its answer.
LinearClassifier
A linear classifier over features and pairs of features, with its weights in memory.
Parser
Finds the head of each bunsetsu of a sentence.
ParserModel
The model of the parser, in memory.
ParserPart
The parser part of a model file, read on the bytes of the file: the classifier of the parser that Model::parser gives.

Enums§

Group
A group of features of a decision.
PairWeights
The weights of the pairs of features of a model.
Quantization
How the weights are written to a model file.

Traits§

Classifier
Scores the features of a decision: a source depends on a target when the score is above 0.

Functions§

instances
The training instances of a sentence.
write_instances
Writes the training instances of a sentence.