LoginSignup
3
2

More than 3 years have passed since last update.

kuromoji.jsをNode.jsなしでローカルブラウザで扱う

Posted at

Node.js不要。

GitHub - takuyaa/kuromoji.js: JavaScript implementation of Japanese morphological analyzer

Browser

You only need the build/kuromoji.js and dict/*.dat.gz files

とあるように
kuromoji.js/kuromoji.js at master · takuyaa/kuromoji.js · GitHub

kuromoji.js/dict at master · takuyaa/kuromoji.js · GitHub
を落として設置する。

あとはHTMLで読み込んで開くだけだが、
HTMLのパスはfile:///ほげほげ。
(辞書のディレクトリパスはひとまず相対パス(./dict)などでOK)

Firefoxでは
TypeError: this.character_definition is null
Chromeでは
*.dat.gzの読み込みで
from origin 'null' has been blocked by CORS policy
が出るかもしれない。
ファイルから他のファイルへのアクセスを禁止するセキュリティが働いているので、Chromeならば起動オプションに
--allow-file-access-from-files
を追加してやれば機能する1

辞書の扱いは調べるとcsvがどうとでるが、追加しようと考えなければとりあえず落としたままの*.dat.gzだけでOK。

<script src="./kuromoji.js"></script>
<script>
var builder = kuromoji.builder({ dicPath: "dict/" })

var buildFunction = (string) => {
  return (err, tokenizer) => {
    //console.log('string',string)
    var path = tokenizer.tokenize(speech);
    console.log('path',path)
  }
}

builder.build(buildFunction("すもももももももものうち"))
</script>

参考


  1. もしまだエラーが出るなら--unsafely-treat-insecure-origin-as-secureも足せば成功した環境に近づくはず。 

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2