sasara
v0.2.0
Published
A lightweight Japanese bunsetsu-level dependency parser for npm.
Maintainers
Readme
sasara(簓)
English | 日本語
A Japanese bunsetsu-level dependency parser for npm. Inference runs on a lightweight linear model (~550KB) with no neural network, so the package stays dependency-light and works in both Node.js and the browser.
Features
- Small and fast — the trained model is ~550KB. For a typical 10–20 bunsetsu sentence, parsing takes microseconds; morphological analysis dominates the runtime
- Calibrated confidence scores — each attachment decision can return a probability of being correct
- Browser-ready — dictionary and model paths are injectable, so it bundles cleanly
- Written in TypeScript — type definitions included
Installation
npm install sasaraUsage
import { parse } from 'sasara'
const bunsetsu = await parse('太郎は花子に本を渡した。')
for (const b of bunsetsu) {
console.log(b.surface, '->', b.head)
}
// 太郎は -> 3
// 花子に -> 3
// 本を -> 3
// 渡した。 -> nullparse() automatically loads the bundled trained model on first call.
Confidence scores
import { loadParser } from 'sasara'
const parser = await loadParser({ confidence: true })
const bunsetsu = await parser.parse('太郎は花子に本を渡した。')
for (const b of bunsetsu) {
console.log(b.surface, '->', b.head, b.confidence)
// { score: 12.3, probability: 0.91, forced: false }
}Parsing from a token stream (synchronous)
If you already have morphological analysis results, pass kuromojin tokens directly.
import { tokenize } from 'kuromojin'
import { loadParser } from 'sasara'
const parser = await loadParser()
const tokens = await tokenize('太郎は花子に本を渡した。')
const bunsetsu = parser.parseSync(tokens)Bunsetsu chunking only
If you don't need dependencies, the chunker is available on its own.
import { tokenize } from 'kuromojin'
import { chunk } from 'sasara'
const bunsetsu = chunk(await tokenize('太郎は花子に本を渡した。'))
// head is left unset (null)[!NOTE] Input is expected to be a single sentence. Splitting a document into sentences is the caller's responsibility.
Output format
Each bunsetsu (phrase unit) is a Bunsetsu object.
interface Bunsetsu {
index: number // position of this bunsetsu in the sentence
tokens: KuromojiToken[] // morphemes that make up the bunsetsu
surface: string // surface string
head: number | null // index of the head bunsetsu; null for the root (sentence-final)
}With confidence: true, each bunsetsu carries a confidence field (the root gets null).
interface Confidence {
score: number // raw attach-decision score (positive = attach, negative = shift)
probability: number // P(this attachment is correct), Platt-calibrated from |score|
forced: boolean // true if merged by the end-of-sentence fallback, not a classifier decision
}CLI
npx sasara "太郎は花子に本を渡した。"
# [3] 渡した。
# ├── [0] 太郎は
# ├── [1] 花子に
# └── [2] 本を-f, --format <tree|list|json> switches output format (default tree, root-down dependency tree in tree-command style); -c, --confidence annotates each bunsetsu with its attach probability, e.g. (0.91) or (0.91*) for a forced merge. Omit the text argument to read sentences from stdin, one per line:
echo "太郎は花子に本を渡した。" | npx sasara -f json -c | jq '.[].surface'See sasara --help for the full option list (--model, --dic, --chunk).
Accuracy
| Dataset | UAS (head accuracy, root excluded) | Bunsetsu boundary F1 | |---|---|---| | GSD test | 83.31% | 87.15% | | PUD (out-of-domain) | 81.41% | 89.77% |
Full evaluation conventions, confidence calibration details, and the algorithm description live in docs/evaluation.md and docs/algorithm.md.
Browser usage
Browsers have no filesystem, so fetch the model JSON yourself and pass it via the model option. The kuromoji dictionary location must also be specified with dicPath.
import { loadParser } from 'sasara'
const model = await fetch('/assets/model.json').then((r) => r.json())
const parser = await loadParser({
model,
dicPath: '/assets/dict/', // where the kuromoji dictionary files (.dat.gz) are served
})The model JSON ships in the package at sasara/model/model.json; copy it as an asset at build time.
License
- Code: MIT
model/model.jsonis a derivative of UD_Japanese-GSD (curated by Megagon Labs) and is therefore licensed CC BY-SA 4.0. Commercial use is allowed; redistribution and modification require attribution to the source corpus and the same license- UD_Japanese-PUD (CC BY-SA 4.0) is used for evaluation only and is not included in the distribution
