Command Line

lindera dependency parse takes plain text, tokenizes it with a Lindera dictionary, and writes the bunsetsu of each sentence and their dependencies in the lattice format, which is what cabocha -f1 writes.

It needs a model file with a chunker and a parser: the model for IPADIC or one of the models for UniDic, which a release of Lindera publishes from 7.1.0 on, or one you train.

Building

lindera dependency is a part of the lindera command, in its default build: the binaries of a release have it, and so does cargo install lindera-cli. It is behind the dependency feature of lindera-cli. embed-ipadic embeds IPADIC in the command, and embed-dependency-ipadic the model for IPADIC, which --model embedded://ipadic then names (embed-dependency-unidic-cwj and --model embedded://unidic-cwj for UniDic CWJ, and the same for unidic and unidic-csj, with the dictionary given as a directory):

cargo build --release -p lindera-cli --features embed-ipadic

The binary is target/release/lindera. lindera dependency has two subcommands for a user, parse and chunk; the train feature, which is in the default build too, adds the ones that make training data, train and evaluate. The crate lindera-dependency itself is a library, and has no command.

Parsing text

A line of the input is cut into its sentences, and each sentence is parsed on its own. A line with a single sentence:

echo "彼は赤い花を見た。" | lindera dependency parse \
    --model ipadic.model --dict embedded://ipadic
* 0 3D 0/1 0.000000
彼	名詞,代名詞,一般,*,*,*,彼,カレ,カレ
は	助詞,係助詞,*,*,*,*,は,ハ,ワ
* 1 2D 0/0 0.699567
赤い	形容詞,自立,*,*,形容詞・アウオ段,基本形,赤い,アカイ,アカイ
* 2 3D 0/1 0.000000
花	名詞,一般,*,*,*,*,花,ハナ,ハナ
を	助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
* 3 -1D 0/1 0.000000
見	動詞,自立,*,*,一段,連用形,見る,ミ,ミ
た	助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
。	記号,句点,*,*,*,*,。,。,。
EOS

A line that starts with * opens a bunsetsu: its number, the number of the bunsetsu it depends on (-1 for the last one) with the letter D, the positions of its head word and function word, and the score of the decision that attached it. The score is 0 where the parser had nothing to decide. The morphemes follow with the features of the dictionary, and EOS ends the sentence.

  • The text is tokenized with the default settings of Lindera, as the training data is. The tokenizer gives no morpheme for a space.
  • A line without a morpheme, such as an empty line, gives EOS alone, as in CaboCha.
  • A line is cut into sentences at its sentence ends outside brackets, by the rule of the token filter: the models are trained on single sentences. See A line of several sentences.
  • A sentence is read, parsed and written before the next one is read, so the command can stand in a pipe. When a sentence fails, what was written before it stays, and the message names the line.

A line of several sentences

Each sentence of a line is written with an EOS of its own, and its last bunsetsu depends on nothing:

echo "雨が降った。試合は中止になった。" | lindera dependency parse \
    --model ipadic.model --dict embedded://ipadic
* 0 1D 0/1 0.000000
雨	名詞,一般,*,*,*,*,雨,アメ,アメ
が	助詞,格助詞,一般,*,*,*,が,ガ,ガ
* 1 -1D 0/1 0.000000
降っ	動詞,自立,*,*,五段・ラ行,連用タ接続,降る,フッ,フッ
た	助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
。	記号,句点,*,*,*,*,。,。,。
EOS
* 0 2D 0/1 0.000000
試合	名詞,サ変接続,*,*,*,*,試合,シアイ,シアイ
は	助詞,係助詞,*,*,*,*,は,ハ,ワ
* 1 2D 0/1 0.000000
中止	名詞,サ変接続,*,*,*,*,中止,チュウシ,チューシ
に	助詞,格助詞,一般,*,*,*,に,ニ,ニ
* 2 -1D 0/1 0.000000
なっ	動詞,自立,*,*,五段・ラ行,連用タ接続,なる,ナッ,ナッ
た	助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
。	記号,句点,*,*,*,*,。,。,。
EOS

The sentences are those the token filter japanese_dependency gives for the same text, with the same bunsetsu and the same heads. A sentence end inside brackets ends no sentence: 彼は「駅に着いた。誰もいない!」と言った。 is a single sentence.

--line-as-sentence takes a line as a single sentence. That is what CaboCha does with a line, and what Lindera 7.2.0 did. A parser makes a single tree of what it is given, so the last bunsetsu of a sentence then depends on a bunsetsu of the next: with the option, 降った。 above depends on 試合は, and the line ends with its only EOS. On 501 lines made of a sentence of the dev and test files of UD Japanese-GSD and the next, 24.32% of the bunsetsu of the first sentence (1,012 of 4,161) then get another head than in the sentence alone. Give the option when each line is a sentence already and the sentences of the output have to be the lines of the input.

Options

OptionDefaultMeaning
-m, --modelRequiredThe model file
-d, --dictRequired for textThe dictionary the text is tokenized with: a directory, or embedded://ipadic in a build with embed-ipadic
-I, --input-layertextWhat the input holds already: text, morphemes or bunsetsu
--line-as-sentenceOffFor text: a line is a single sentence, and is not cut
-i, --inputThe standard inputThe file to parse
-o, --outputThe standard outputThe file to write

The dictionary and the model

A model is trained on the morphemes of one dictionary, and gives wrong answers on those of another one without failing. The command therefore compares the name of the dictionary it loaded with the name in the model file, and stops when they differ:

Error: LinderaError { kind: Content, source: embedded://ipadic: the model is for the dictionary "unidic", and the morphemes are of "ipadic" }

Input that is tokenized already

--input-layer says what the input holds. With morphemes and bunsetsu the input is in the lattice format, no dictionary is loaded, and --dict is refused.

LayerThe inputWhat the command doesIn CaboCha
textPlain textTokenizes, cuts each line into sentences, segments into bunsetsu, parses-I0, without the cut into sentences
morphemesMorphemes in the lattice format; bunsetsu lines are not readSegments into bunsetsu, parses-I1
bunsetsuMorphemes and bunsetsu in the lattice formatParses the bunsetsu of the input-I2
lindera dependency parse --model ipadic.model --input-layer morphemes \
    --input tokens.lattice --output output.lattice
lindera dependency parse --model ipadic.model --input-layer bunsetsu \
    --input dev.lattice --output output.lattice

The three layers give the same output for a sentence, when the morphemes and the bunsetsu are the ones the command would have made. bunsetsu on the answer of a corpus measures the parser alone, and morphemes the chunker and the parser together: see Evaluation. With no dictionary to compare, what is checked on a lattice input is that each morpheme has the feature columns the schema of the model reads.

What is kept of CaboCha's options

CaboChaHere
-f1, the lattice formatThe only output format
-f0 the tree, -f2, -f3 XML, -f4 CoNLLNot kept
-I0, -I1, -I2, the input layer--input-layer
A line of -I0 is a single sentence--line-as-sentence; by default a line is cut into sentences
-O1, -O2, -O4, the output layerNot kept: lindera tokenize writes the morphemes, and chunk the bunsetsu
-n, named entitiesNot kept

CaboCha's own tools read the output (#1433): cabocha-system-eval counts on it what evaluate counts, and cabocha -I 2 takes it as its input with bunsetsu.

Speed

On the text of the test file of UD Japanese-GSD (543 sentences), with the default model of Lindera 7.2 and the embedded IPADIC, the command writes about 6,800 sentences per second, from the text to the lattice format. Loading the dictionary and the model takes about 6 ms. The numbers depend on the machine; scripts/dependency/parse_speed.sh measures them.

lindera tokenize

lindera tokenize runs the parser as a token filter: see Token Filter. It writes the result on the tokens of its JSON output, not the lattice format. The lindera command has the filter in its default build, from 7.1.0 on, as it has lindera dependency.