Token Filter

The token filter japanese_dependency runs the analysis inside a Lindera tokenizer: it groups the tokens of a text into bunsetsu, finds the bunsetsu each one depends on, and writes the result on every token. It is what the lindera command, a configuration file and a program that uses the Tokenizer of Lindera call. To parse text into the lattice format of CaboCha, see Command Line.

The filter adds, removes and rewrites no token. The MeCab and wakati outputs are the ones without it, and the JSON output has one key more for each token.

A build with the filter

The filter is in lindera-analysis, behind the dependency feature, which lindera, lindera-cli and the bindings forward. The lindera command and the five bindings have the feature in their default builds, from 7.1.0 on, so the packages you install have the filter. The libraries lindera and lindera-analysis do not have it by default:

[dependencies]
lindera = { version = "7.1", features = ["dependency"] }

A build without the feature knows the name of the filter and says what is missing:

the token filter japanese_dependency needs a build with the feature `dependency`

The filter needs a model, which is not in a package: see The model.

From the bindings

Python, Node.js, Ruby and PHP append the filter as the others, by its kind and the path of the model, and a token has a dependency: an object with the five fields below, or nothing (None, nil, null, no property) without the filter. In WebAssembly a path cannot be read, and appendDependencyFilter takes the bytes of the model. The distributed packages have the filter, from 7.1.0 on. The API page of each binding has an example: Python, Node.js, Ruby, PHP, WebAssembly.

The model

The filter takes one argument, model: the path of a model file with a chunker and a parser, for the dictionary of the tokenizer; or embedded://ipadic, the model that a build with the feature embed-dependency-ipadic holds. A release of Lindera publishes the model for IPADIC, from 7.1.0 on, and lindera dependency download ipadic installs it and prints its path. The same goes for the three UniDic dictionaries, with unidic, unidic-cwj or unidic-csj in place of ipadic (The models for UniDic). Parsing shows how to train one, and Model File what the file holds.

The model is read once, when the filter is made, and shared by the copies of the tokenizer. A file that cannot be read, that is not a model file, or that lacks the chunker or the parser is an error then, before any text is read.

From the command line

echo '彼は赤い花を見た。' | lindera tokenize --dict embedded://ipadic --output json \
    --token-filter 'japanese_dependency:{"model":"ipadic.model"}'

Each token of the output has a dependency. The first one:

{
  "base_form": "彼",
  "byte_end": 3,
  "byte_start": 0,
  "conjugation_form": "*",
  "conjugation_type": "*",
  "dependency": {
    "bunsetsu": 0,
    "head": 3,
    "role": "head",
    "score": 0.0,
    "sentence": 0
  },
  "part_of_speech": "名詞",
  "part_of_speech_subcategory_1": "代名詞",
  "part_of_speech_subcategory_2": "一般",
  "part_of_speech_subcategory_3": "*",
  "pronunciation": "カレ",
  "reading": "カレ",
  "surface": "彼",
  "word_id": 211398
}

And all eight, as the surface and the dependency of each:

彼    {"bunsetsu": 0, "head": 3, "role": "head", "score": 0.0, "sentence": 0}
は    {"bunsetsu": 0, "head": 3, "role": "function", "score": 0.0, "sentence": 0}
赤い  {"bunsetsu": 1, "head": 2, "role": "head_function", "score": 0.6995669465539138, "sentence": 0}
花    {"bunsetsu": 2, "head": 3, "role": "head", "score": 0.0, "sentence": 0}
を    {"bunsetsu": 2, "head": 3, "role": "function", "score": 0.0, "sentence": 0}
見    {"bunsetsu": 3, "head": null, "role": "head", "score": 0.0, "sentence": 0}
た    {"bunsetsu": 3, "head": null, "role": "function", "score": 0.0, "sentence": 0}
。    {"bunsetsu": 3, "head": null, "role": "other", "score": 0.0, "sentence": 0}

That is 彼は → 見た。, 赤い → 花を and 花を → 見た。, the analysis lindera dependency parse writes for the same sentence.

From a configuration file

segmenter:
  mode: "normal"
  dictionary: "embedded://ipadic"

token_filters:
  - kind: "japanese_dependency"
    args:
      model: "ipadic.model"
  - kind: "japanese_stop_tags"
    args:
      tags:
        - "助詞"
        - "助動詞"
        - "記号"

A path that is not absolute is read from the directory the program runs in, not from the directory of the configuration file.

From Rust

use std::path::Path;

use lindera::analysis::token_filter::BoxTokenFilter;
use lindera::analysis::token_filter::japanese_dependency::JapaneseDependencyTokenFilter;
use lindera::analysis::tokenizer::Tokenizer;
use lindera::dictionary::load_dictionary;
use lindera::mode::Mode;
use lindera::segmenter::Segmenter;

fn main() -> lindera::LinderaResult<()> {
    let dictionary = load_dictionary("embedded://ipadic")?;
    let mut tokenizer = Tokenizer::new(Segmenter::new(Mode::Normal, dictionary, None));
    let filter = JapaneseDependencyTokenFilter::from_path(Path::new("ipadic.model"))?;
    tokenizer.append_token_filter(BoxTokenFilter::from(filter));

    for token in tokenizer.tokenize("彼は赤い花を見た。")? {
        if let Some(dependency) = token.dependency() {
            println!(
                "{}\t{}\t{:?}\t{}",
                token.surface,
                dependency.bunsetsu,
                dependency.head,
                dependency.role.as_str()
            );
        }
    }
    Ok(())
}
彼      0       Some(3) head
は      0       Some(3) function
赤い    1       Some(2) head_function
花      2       Some(3) head
を      2       Some(3) function
見      3       None    head
た      3       None    function
。      3       None    other

JapaneseDependencyTokenFilter::from_bytes makes the filter from the bytes of a model, for a model that is embedded in the program or that comes from somewhere else than a file. Token::dependency is None for a token no filter wrote on.

What is written on a token

FieldWhat it is
sentenceThe number of the sentence in the text, from 0
bunsetsuThe number of the bunsetsu of the token in its sentence, from 0
headThe number of the bunsetsu that one depends on, in the same sentence; null (None) for the last bunsetsu of a sentence
roleWhat the token is in its bunsetsu: head, function, head_function or other
scoreThe score of the decision that attached the bunsetsu to its head; 0 where nothing was decided, as for a bunsetsu that depends on the last one

The tokens with the same sentence and bunsetsu are one bunsetsu. head is the head word of the bunsetsu and function its function word; head_function is a token that is both, as 赤い above, and other is what is neither, as a symbol. Head and Function Word has the rule.

Sentences

A model is trained on sentences, so the filter cuts the text into sentences and analyzes each on its own. The bunsetsu of each sentence are numbered from 0. lindera dependency parse cuts a line of text by the same rule (Schema::sentence_ends of lindera-dependency), so a text gets the same analysis through either.

  • A sentence ends after a token of 。, ! or ? (and !, ?), when it is not inside brackets. More of them, closing brackets and spaces (a full-width space is a token) that follow go to the same sentence.
  • The full-width period . ends a sentence unless a digit follows it, as in 3.14.
  • The half-width period . ends none. It stands in numbers (4.20), in ... and in names.
  • What is left after the last end is a sentence.
  • A sentence end inside brackets ends no sentence here: a quotation is parsed with the sentence around it. Its bunsetsu are cut all the same, by the chunker (A text of several sentences).
  • A token of marks that the dictionary joined is read mark by mark, for the ends and for the brackets. With IPADIC, (笑)。 is (, 笑 and )。, whose ) closes the bracket and whose 。 ends the sentence; ?」 after 「 ends nothing and closes the bracket. Lindera 7.2.0 read neither as a bracket, and cut no sentence in the rest of the text after one.
  • A line break is not a token and ends no sentence. lindera tokenize reads its input line by line, so there a line break does separate.
彼は「駅に着いた。誰もいない!」と言った。本当?
彼    0  0  5     head
は    0  0  5     function
「    0  1  2     other
駅    0  1  2     head
に    0  1  2     function
着い  0  2  4     head
た    0  2  4     function
。    0  2  4     other
誰    0  3  4     head
も    0  3  4     function
い    0  4  5     head
ない  0  4  5     other
!    0  4  5     other
」    0  4  5     other
と    0  4  5     function
言っ  0  5  None  head
た    0  5  None  function
。    0  5  None  other
本当  1  0  None  head_function
?    1  0  None  other

The columns are the surface, the sentence, the bunsetsu, the head and the role. The period and the ! inside the brackets end nothing; the second sentence starts at 本当, and its bunsetsu is number 0 again.

An opening bracket that nothing in the text closes is no bracket: the sentences after :( or after a ( without its ) are cut as the ones before it. Lindera 7.2.0 kept the rest of the text in one sentence after such a (, and after :( with UniDic, which gives ( as a word of its own. IPADIC gives :( as one word, which 7.2.0 read as no bracket.

Where the filter goes

Put the filter first among the token filters. The model reads the tokens as the dictionary gave them, and the filters after it see the tokens it wrote on: in the configuration above, japanese_stop_tags removes the particles, and the tokens that are left keep their dependency.

Tokens that an earlier filter removed or joined are refused:

japanese_dependency: token 1 ("赤い") is at position 2 with a length of 1: an earlier token filter removed or joined tokens; put this filter before it

This is seen from the positions of the tokens. What is not seen, and gives a wrong analysis without an error: a surface or a feature that an earlier filter rewrote (japanese_base_form, lowercase), and tokens removed from the end of the text. A character filter changes the text before the dictionary reads it, which the model was not trained on either.

Dependency pairs

The token filter japanese_dependency_pair, from 7.2.0 on, turns what japanese_dependency wrote into terms for a search engine. It goes after japanese_dependency, and replaces the tokens of a text with one token for each bunsetsu that depends on another: a dependency pair, the head word of the bunsetsu with the head word of the bunsetsu it depends on. In search: dependency pairs says what the pairs are for.

echo '頭痛を治す薬。' | lindera tokenize --dict embedded://ipadic --output wakati \
    --token-filter 'japanese_dependency:{"model":"ipadic.model"}' \
    --token-filter japanese_dependency_pair
頭痛>治す 治す>薬

The argument form chooses one of two forms:

formA pair頭痛を治す薬。 gives
word (the default)<word>><head word>頭痛>治す and 治す>薬
word_particle<word>/<particle>><head word>頭痛/を>治す
  • The word of a bunsetsu is the base form of its head word, read as the token filter japanese_base_form reads it, so that 治す and 治さ give the same pair.
  • The particle is the function word of the bunsetsu, when that is a particle (助詞). With word_particle, a bunsetsu without one gives no pair: 治す in the table.
  • A pair without a particle does not tell the roles apart, and one with a particle does. 犬が人を噛んだ。人が犬を噛んだ。 gives 犬>噛む, 人>噛む, 人>噛む and 犬>噛む with word, and 犬/が>噛む, 人/を>噛む, 人/が>噛む and 犬/を>噛む with word_particle. Two tokenizers, one for each form, fill two fields, and a search gives the second more weight.
  • No pair is made for the last bunsetsu of a sentence, which depends on nothing, for a bunsetsu without a head word, or when the bunsetsu it depends on has none. A pair that a text has twice is given twice, as the example of the dogs shows.
  • The separators > and / are written as they are, also when a word holds one of them.

A pair token is the token of the head word of the dependent bunsetsu, with four changes: its surface is the pair; its bytes go from the start of that bunsetsu to the end of the bunsetsu it depends on; its position is its number among the pairs, from 0; and its position length is 1. The details and the dependency of the token stay, so the JSON output has the part of speech of the word and the score of the dependency. The token of 頭痛/を>治す:

{
  "base_form": "頭痛",
  "byte_end": 15,
  "byte_start": 0,
  "conjugation_form": "*",
  "conjugation_type": "*",
  "dependency": {
    "bunsetsu": 0,
    "head": 1,
    "role": "head",
    "score": 1.819070260889429,
    "sentence": 0
  },
  "part_of_speech": "名詞",
  "part_of_speech_subcategory_1": "一般",
  "part_of_speech_subcategory_2": "*",
  "part_of_speech_subcategory_3": "*",
  "pronunciation": "ズツー",
  "reading": "ズツウ",
  "surface": "頭痛/を>治す",
  "word_id": 379590
}

The filter reads the tokens and needs no model, so it is in every build. Without japanese_dependency before it there is nothing to read, and the first text is an error:

Error: LinderaError { kind: Args, source: japanese_dependency_pair: token 0 ("頭痛") has no dependency; put the token filter japanese_dependency before this one }

A text that ends with a noun and has no period, a query for example, gives the pairs it gives with one: 人を噛んだ犬 and 人を噛んだ犬。 both give 人>噛む and 噛む>犬, and no 。 has to be added. The chunker of Lindera 7.1, trained on sentences alone, cut 人を噛んだ犬 into 人を / 噛んだ犬, and a 。 had to be added; from 7.2 on it is trained with texts that have no period too (#1499; see In search: dependency pairs).

In a configuration file, the filter is one more entry after japanese_dependency:

token_filters:
  - kind: "japanese_dependency"
    args:
      model: "/path/to/ipadic.model"
  - kind: "japanese_dependency_pair"
    args:
      form: "word_particle"

The dictionary

A model gives wrong answers on the tokens of another dictionary without failing. The filter compares the name of the dictionary of the tokens with the name in the model file, and stops when they differ:

the model is for the dictionary "unidic", and the morphemes are of "ipadic"

A token with fewer features than the model reads is refused as well.

Speed

On the text of the test file of UD Japanese-GSD (543 sentences, 12,644 tokens), with the default model of Lindera 7.2 and the embedded IPADIC, the filter adds about 5.2 microseconds per token to lindera tokenize: about 180,000 tokens per second with it, which is about 7,700 sentences, against about 2,900,000 tokens per second without. Making the filter, which reads the model, takes about 5 ms. The numbers depend on the machine.