Scrapboxの記法を維持して形態素解析
2022-04-07
https://gyazo.com/05d8ee0d4c4a5f47ace96e804e2cb77a
ただしScrapboxの場合は保護したい記法が複数あるので前処理を正規表現での置換で済ませるのは困難
code:ts
data.pages.forEach((page: ScrapboxPage) => {
const text = page.lines.map((line: ScrapboxLine) => line.text).join("\n");
const blocks = parse(text);
const mecab = [] as string[];
blocks.forEach((block) => {
if (block.type === "line") {
const indent = " ".repeat(block.indent);
mecab.push([${indent}]\tSCRAPBOX_INDENT);
let trailing_space = "";
block.nodes.forEach((node) => {
if (node.type === "plain") {
const m = node.text.match(/(.*?)(\s*)$/);
} else if (node.type === "link" || node.type === "icon") {
const tag = node.type === "link" ? "SCRAPBOX_LINK" : "SCRAPBOX_ICON";
mecab.push(${trailing_space}${node.raw}\t${tag});
trailing_space = "";
}
});
}
});
page.mecab = mecab.join("\n");
});
---
old version
code:ts
const blocks = parse(text);
const mecab = [] as string[];
blocks.forEach((block) => {
if (block.type === "line") {
const indent = " ".repeat(block.indent);
mecab.push([${indent}]\tSCRAPBOX_INDENT);
block.nodes.forEach((node) => {
console.log(node);
if (node.type === "plain") {
mecab.push(node.text);
} else if (node.type === "link") {
mecab.push(${node.raw}\tSCRAPBOX_LINK);
} else if (node.type === "icon") {
mecab.push(${node.raw}\tSCRAPBOX_ICON);
}
});
}
});
error tokenizer.cpp(368) [new_node->feature]
下流のMeCabの仕様によりリンク記法やアイコン記法の直前のテキストが空白文字で終わってはいけない
制約ありテキスト列の 直前 に 半角スペース があるときだけ起こるエラーです。
単に削ると復元できなくなるので、今回はその空白文字を記法の側に追加することにする