Japanese text search with lingrep (Python and the ling command)

Install the ling command · Try the Python package · Search a corpus with ling · Find similar words

In 池に落ちた。 (“fell into a pond”) and 眠りに落ちる。 (“fall asleep”), a search for the exact string 落ちる finds only the second example. With lingrep, you can search for the verb by its dictionary form and retrieve the original wording from both sentences. Their contexts let you decide which describes physical movement.

Tokenize a sentence

The lingrep 0.10.0 Python package includes a tokenizer. With uv installed, run this in a terminal on Linux, macOS, or Ubuntu on WSL. On first use, the tokenizer downloads and verifies the default UniDic CWJ dictionary (about 1.3 GB installed). It reuses the dictionary offline.

uvx --from lingrep==0.10.0 lingrep tokenize '池に落ちた。'
["池", "に", "落ち", "た", "。"]

For a batch, pipe one sentence per line. Each output line is a JSON array in the same order as the input.

printf '%s\n' '池に落ちた。' '眠りに落ちる。' | uvx --from lingrep==0.10.0 lingrep tokenize --workers 2

To use an installed Vibrato dictionary, pass its file path with --dict-path or set LINGREP_DICTIONARY_PATH. Python uses the same lookup and also accepts Tokenizer(dict_path=...).

Inspect a verb with Python

In 階段を上った。 (“climbed the stairs”), how is the verb 上る written? Save the code below as inspect_verb.py. The tokenizer’s analyze method analyzes one sentence. Calling column on the result returns one value per token, in reading order.

from lingrep import Tokenizer

tokenizer = Tokenizer()
text = "階段を上った。"
sentence = tokenizer.analyze(text)
print(sentence.column("surface"))
print(sentence.column("lemma"))

In a terminal, run the script from the folder where you saved it.

uv run --no-project --python 3.14 --with lingrep==0.10.0 python inspect_verb.py

In Jupyter or Colab, run %pip install lingrep==0.10.0 once in a setup cell, then run the Python code in the next cell. With the default UniDic CWJ dictionary, both routes print these two lines.

['階段', 'を', '上っ', 'た', '。']
['階段', 'を', '上る', 'た', '。']

In the third token, the surface form is 上っ and the lemma is 上る. Append the code below to the same file, or run it in the next notebook cell. The pattern looks for “stairs,” then the particle を, then “climb.” The parentheses (?<v> ...) name the verb capture v.

from lingrep import Query

pattern = Query("[lemma=階段] [lemma=を] (?<v> [lemma=上る]).")
query = pattern.compile()
for hit in query.find_matches(sentence):
    verb = hit["captures"][0]
    span = verb["char_span"]
    print(text[span["start"]:span["end"]])

Run it to print 上っ. The two character offsets select the verb’s original spelling. Now change text in the first block to "階段を見て、坂を上った。" (“looked at the stairs and climbed the slope”). The lemma 上る still occurs. Predict whether the pattern matches, then rerun both blocks.

Find inflected forms across sentences

Save this example as find_ochiru.py. One Tokenizer shares the dictionary across the three sentences. match_pipe returns one list of matches per sentence, including an empty list when the verb is absent.

from lingrep import Tokenizer

sentences = ["池に落ちた。", "眠りに落ちる。", "池を見た。"]
tokenizer = Tokenizer()
for sentence, hits in zip(sentences, tokenizer.match_pipe(sentences, "[lemma=落ちる].")):
    forms = []
    for hit in hits:
        span = hit["char_span"]
        forms.append(sentence[span["start"]:span["end"]])
    print(forms)
uv run --no-project --python 3.14 --with lingrep==0.10.0 python find_ochiru.py

The three lines are ['落ち'], ['落ちる'], and []. Try [surface=落ちる]. in the same script. The first list becomes empty because the original spelling there is 落ち.

Follow a dependency in a corpus

In 坂をゆっくり上る (“slowly climb a slope”), an adverb separates 坂を from 上る. To find the verb governing 坂, use an annotated corpus with dependency links. The small five-sentence CoNLL-U sample already contains those links.

Python’s Ling.search() calls the full ling executable. Install it with the following commands, open a new terminal, and check that ling --version prints ling 0.10.0.

curl -fsSLO https://github.com/borh/lingrep/releases/download/v0.10.0/install.sh
LING_VERSION=0.10.0 sh install.sh

Save the sample as class6-practice-a.conllu. In that folder, build a prepared corpus once. This reads the supplied annotations without a dictionary download.

ling corpus build --input-format conllu --output slopes.lingcorpus class6-practice-a.conllu

Save the following as find_slopes.py. The first line of the pattern captures the adjacent noun and particle. The where clause follows the verb’s object link to the noun and the noun’s case link to the particle.

from lingrep import Ling

pattern = """
(?<n> [lemma=坂]) (?<p> [lemma=を])
where (?<v> [lemma=上る & upos=VERB]) -[obj]-> $n, $n -[case]-> $p.
"""
with Ling().search(pattern, corpus="slopes.lingcorpus") as hits:
    for hit in hits:
        print(hit["document_sentence_ordinal"] + 1, hit["surface"])
uv run --no-project --python 3.14 --with lingrep==0.10.0 python find_slopes.py

Sentences 1, 2 and 3 each yield the span 坂を. Only sentence 1 has the consecutive sequence 坂を上る. An adjacency search misses sentences 2 and 3 because ゆっくり and 一つ intervene.

To inspect the annotations behind those matches, export the prepared corpus back to CoNLL-U. Open slopes.conllu and look for the head and dependency relation of 坂 in the first sentence.

ling tokenize --corpus slopes.lingcorpus --output-format conllu --limit all > slopes.conllu

Read the matches in a browser

To see what the dependency condition excluded, search for the noun alone in the same corpus.

ling matches --corpus slopes.lingcorpus --pattern '[lemma=坂].' --serve

Open the local address printed in the terminal and select a match to read its sentence. The noun search also finds sentence 5, 坂は急だ。 (“The slope is steep”). Here 坂 describes the terrain and has no movement verb governing it. Press Ctrl+C in the terminal when you finish.

For more examples, see the Python API examples, pattern language reference, and 0.10.0 release notes.

Try a word-vector search

In Kunikida Doppo’s 「わかれ」, sentence 143, some leaves sink suddenly while others float like boats. To find other descriptions of sinking, which word would you search alongside 沈む? Download the Ruri 70M word table (532 MB ZIP, 1.35 GB unpacked) and run these commands in the download folder with ling 0.10.0. The table is ready to use offline on a CPU.

unzip ruri-v3-70m-unidic-cwj-2512-v1.zip
ling neighbors --vectors ruri-v3-70m-unidic-cwj-2512 --min-similarity 0.7 沈む

At 0.7 the table returns 23 forms, including 沈み込む (“sink into”) and 沈みかける (“begin to sink”). Lower the cutoff to 0.6 and run the command again. Among the 46 results are 沈没 (“sinking”) and 浮く (“float”). Which would you keep for a search about downward movement?

Cosine similarity measures how closely the two vectors point in the same direction. Here even a verb for motion in the opposite direction appears among the neighbors. Check the list before using [unidic.orthbase near 沈む >= 0.7] with the same table and a UniDic corpus.

The table contains 369,571 written base forms from UniDic CWJ 2025.12. We encoded each form once with Ruri, subtracted the vocabulary mean and normalized each 384-dimensional vector. The ZIP includes the table digest and model revision. Ruri v3 70M model card.