treebank-tokenizer
Version:
NLTK TreebankTokenizer Ported from Python to JS
47 lines (43 loc) • 1.14 kB
JavaScript
import { align_tokens } from "../src/utils";
import TreebankTokenizer from "../src/index";
test("align_tokens", () => {
const t = new TreebankTokenizer();
const s =
"The plane, bound for St Petersburg, crashed in Egypt's Sinai desert just 23 minutes after take-off from Sharm el-Sheikh on Saturday.";
const tokens = t.tokenize(s);
const expected = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
];
const aligned = align_tokens(tokens, s);
// Check that length of tokens and tuples are the same.
expect(aligned.length).toEqual(expected.length);
// Check that the output is as expected.
expect(aligned).toStrictEqual(expected);
// Check that the slices of the string corresponds to the tokens.
const sliced = aligned.map((p) => s.substring(p[0], p[1]));
expect(sliced).toStrictEqual(tokens);
});