LoginSignup
7
4

More than 5 years have passed since last update.

PyKNPをWindows10にインストールする(お土産付き)

Posted at

自然言語処理の強力なツールであるKNPやjuman++用のpythonバインディングであるPyKNPですが、Windowsネイティブではそのままは使えないようでした。ググっても他に情報がなかったのですが、少しいじれば動くようになりましたので参考までに投稿しておきます。
(わざわざWindowsネイティブにPyKNPを入れようという人なんてあまりいないでしょうけど)

自分でいじるのは面倒だと思いますのでこちらにzipも置いておきました。(PyKNPのバージョンは0.3)ご利用については元のPyKNPの基準に即してお願いしますが、ono0708は利用に際して発生する一切の事に関して責任を持ちませんので御了承の上でお使い下さい。
(zipの公開に問題があればご連絡ください)

当然ですがjuman,KNP,juman++がWindowsにインストールされておりパスが通っている必要があります。jupyterなどでjumanとjuman++を行ったり来たりしているとエラーを吐くことがあるようなのでご注意を。

以下変更箇所。全てpyknp/juman/内のファイルです。

juman.py,52行目
#変更前
self.process = subprocess.Popen('bash -c "%s"' % command, env=env,

#変更後
self.process = subprocess.Popen('%s' % command, env=env,
juman.py,71行目~81行目
#変更前
    def query(self, sentence, pattern):
        assert(isinstance(sentence, six.text_type))
        self.process.stdin.write(sentence.encode('utf-8')+six.b('\n'))
        self.process.stdin.flush()
        result = ""
        while True:
            line = self.stdouterr.readline()[:-1].decode('utf-8')
            if re.search(pattern, line):
                break
            result = "%s%s\n" % (result, line)
        return result

#変更後
    def query(self, sentence, pattern, encoding="ms932"):
        assert(isinstance(sentence, six.text_type))
        self.process.stdin.write(sentence.encode(encoding)+six.b('\n'))
        self.process.stdin.flush()
        result = ""
        while True:
            line = self.stdouterr.readline()[:-1].decode(encoding)
            if re.search(pattern, line):
                break
            result = "%s%s\n" % (result, line)
        return result
jumanpp.py,37行目
#変更前
return self.subprocess.query(input_str, pattern=self.pattern)

#変更後
return self.subprocess.query(input_str, pattern=self.pattern, encoding="utf8")

以上です。良いNLPライフを。

7
4
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
7
4