hexenq / kuroshiro

Japanese language library for converting Japanese sentence to Hiragana, Katakana or Romaji with furigana and okurigana modes supported.
https://kuroshiro.org
MIT License
822 stars 93 forks source link

Problem running in browser #38

Open keldenl opened 6 years ago

keldenl commented 6 years ago

I copied the ReadMe for running in the browser, and below is the code in my .js file: kuroshiro.init(new KuromojiAnalyzer({ dictPath: "/dict" })) .then(function () { return kuroshiro.convert("手を繋ご", { to: "hiragana" }); }) .then(function(result){ console.log(result); })

I've imported the .min.js file <script src="kuroshiro.min.js"></script>

It works with Node.JS, but I can't seem to have it working via the browser version of it. var kuroshiro = new Kuroshiro(); works but I it returns "Uncaught ReferenceError: KuromojiAnalyzer is not defined" on the browser when I try to initiate. Is this related to the browser spec under "test".

hexenq commented 6 years ago

Hello keldenl, thanks for feedback. You should go for kuroshiro-analyzer-kuromoji, build it and get the dist file KuromojiAnalyzer.min.js, then import it into your project. And now everything should be ok. Try it and feel free to contact me later if you have other questions. And I'll later update READMEs to make the procedure clear.

hexenq commented 6 years ago

Hello @keldenl . I have updated kuroshiro and several analyzers. The document of kuroshiro has been revised. Please notice that, from the new version (1.1.0) of kuroshiro-analyzer-kuromoji, the dist file is renamed to kuroshiro-analyzer-kuromoji.min.js.

keldenl commented 6 years ago

I see. Thank you so much! Things are fixed. Another question though, I seem to be able to run this in a local server (via http-server), but it doesn't work when I upload it online (there's an error when initializing the dict in kuromoji-analyzer, it can't read the dict files – Uncaught Error: invalid file signature:1,1 at $.g (kuroshiro-analyzer-kuromoji.js:9225) at XMLHttpRequest.xhr.onload (kuroshiro-analyzer-kuromoji.js:7700), and this happens around 20-30 times in a row)

hexenq commented 6 years ago

Hello @keldenl . The question you raised is similar with #27 and #24 . You could check these two issues. I think this issue is duplicated with one of them. Whether If you had a different situation or not, please describe your situation in detail, including but not limited to the info of project structure (using any bundlers or not), browser and hosting service provider. A live demo that can reproduce this issue is better.

lvl99 commented 5 years ago

Just a note to others that might waste 3 days on this like I did: I had to copy the dat files in the kuromoji dict folder to my React app public folder, then configured the analyzer to point to the files relative to the homepage URL:

  const kuroshiro = new Kuroshiro();
  const analyzer = new KuromojiAnalyzer({
    dictPath: "/data/ja-dict"
  });
RobertB4 commented 5 years ago

I also posted this in an issue of the kuroshiro-analyzer-kuromoji since it's related to that plugin, but since this question seems to pop up here a lot I'll post it here too.

I am using Next.js, which can serve static files only from a specific directory /static. the /dict directory of the Kuromoji analyzer is outside the /static directory by default (/node_modules/kuromoji/dict), which results in the following console error: GET http://localhost:3000/node_modules/kuromoji/dict/base.dat.gz 404 (Not Found) (the same error is repeated for every other file in the /dict directory)

I solved this issue by copying the /dict directory from /node_modules/kuromoji, pasting it inside the /static directory of my project and specifying the new dictPath like this.

const analyzer = new KuromojiAnalyzer({
        dictPath: '/static/dict/',
      });

This way the browser can server the files correctly.

This doesn't feel like a good solution though. It would be nice to have this fixed so it works out of the box.

lvl99 commented 5 years ago

@RobertB4 I think it does work out of the box. The manual (or automatic, if you script it) copying files to a statically served area of the site is a good idea. I think the kuromoji dat files total ~18mb, so best is to manage them outside the app codebase (if it is a client-side app) and to serve them statically with longer cache expiry dates from the server to reduce users needing to download them again or frequently.

Alternatively, I've also created a simple express API app that performs the translation, since I had to also translate from chinese to latin characters using the pinyin library.

TomMarvolo commented 5 years ago

Hi, I have some issue... I'm testing kuroshiro using https://npm.runkit.com/kuroshiro/lib/core.js?t=1552138294313

and it returns an error on the convert...

this is my code:

var Kuroshiro = require("kuroshiro")
var KuroshiroAnalyzer = require("kuroshiro-analyzer-kuromoji")
const kuroshiro = new Kuroshiro();

kuroshiro.init(new KuroshiroAnalyzer());

//console.log(Kuroshiro.Util.isHiragana('た'));
const result = await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"furigana", to:"hiragana"});
VelenAranha commented 4 years ago

The following solved for me in a Vue project.

const kuroshiro = new Kuroshiro.default();

chinenvinicius commented 2 years ago

im trying to use kursohiro on my browser but it seem to work only on one page. and on the other page i get the error /distfile/dict/tid.dat.gz 404 (Not Found). even i copied the dict file from kuromoji inside node_modules. this thing is not finding it. i put dict file inside this distfile like this.

let kuroshiro = new Kuroshiro(); kuroshiro.init(new KuromojiAnalyzer({ dictPath: 'distfile/dict/', })); please can anyone help im not undestanding how kuroshiro library actually works on the browser . im encountering many problem not only this.