2018年2月16日金曜日

AtomにおけるPythonのインデント

プログラムを書くのにインデントはスペース2つに設定しているのだが、AtomでPythonのコードを書いているときに反映されていなかった。
PreferencesからtabTypeはsoft、tabLengthは2に設定しているにもかかわらず、スペース4つでインデントされる。
シェルスクリプトや他の言語では問題ない。

解決策は Atom -> Config... を開いて、config.csonに
".python.source":
  editor:
    tabLength: 2
を追記。
"*"と同じ階層に書けば、pythonでもインデントが2になる。

2017年9月16日土曜日

OpenNMTで英日翻訳できるようにする

OpenNMTをインストールして機械翻訳(英日)を使えるようにする

環境

  • Python 3.5.4
  • Linux

手順

$ git clone https://github.com/OpenNMT/OpenNMT-py.git
$ pip3 install -r requirements.txt

実行しようとすると、torchがないと怒られる…
$ python translate.py -model demo-model_epochX_PPL.pt -src data/src-test.txt -output pred.txt -replace_unk -verbose
Traceback (most recent call last):
  File "translate.py", line 7, in <module>
    import torch

ImportError: No module named torch

torchのインストール

pyTorchを見てみる
  • http://pytorch.org
pytthon 3.6.*のときとかは、cp36とかに勝手に変更すると入る
$ sudo pip3 install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whl 
$ sudo pip3 install torchvision

プログラムの実行

以下のコマンドで実行できるが、これは英独の翻訳
$ python3 preprocess.py -train_src data/src-train.txt -train_tgt data/tgt-train.txt -valid_src data/src-val.txt -valid_tgt data/tgt-val.txt -save_data data/demo

英日にしたいので、新しくディレクトリを作成し、その下に英語と日本語のパラレルコーパスを置いた。
行数は英独のものと同様にしたが、データ量としては少ないことに留意する。
*-train.txtが10000行、*-val.txtが3000行。
日本語のコーパスはmecabを用いて、単語をspace区切りに加工している。

モデルの学習

以下のコマンドでデフォルト設定で動く
python3 train.py -data data/demo -save_model demo-model

学習するのに時間がかかるので、nohupで動かすのが良い
$ nohup python3 -u train.py -data data/demo -save_model demo-model > log.txt &

翻訳の実行

モデルのファイルは1番最後に出力されているのを指定した。
src-test.txtは自分で作っても良いかも
$ python3 translate.py -model demo-model_acc_26.58_ppl_194.39_e13.pt -src data/src-test.txt -output pred.txt -replace_unk -verbose

ex. )
SENT 1: please, don't forget me.
PRED 1: 「 それ は どう し た ん です か ? 」
PRED SCORE: -16.1014

ひどさしかない。
gitにも小さいデータでやると恐ろしい結果になると書いてあるので、次は大きいコーパスで試してみる。

なんか見てる人が結構いるっぽいので続きの記事
こちらは大きいコーパスで試してみた

2017年8月4日金曜日

Swagger+nodejsの導入

APIを作って管理するさい、ドキュメントを作るのが面倒
そんな場合、Swaggerを使うと楽できる

パッケージのインストール

mkdir swagger_sample
cd swagger_sample
$ npm init  #適当に情報を入れてpackage.jsonを作成
$ npm install express --save
$ npm install swagger-express --save-dev

Swagger-ui

cd ../    #先ほど作ったディレクトリから出る
$ git clone https://github.com/swagger-api/swagger-ui.git
$ cd swagger-ui
$ mv dist ../swagger_sample

You can use the swagger-ui code AS-IS! No need to build or recompile--just clone this repo and use the pre-built files in the dist folder. If you like swagger-ui as-is, stop here.
とあるので、distを利用する

Code

app.js
var express = require('express');
var app = express();
var swagger = require('swagger-express');
app.use(swagger.init(app, {
  apiVersion: '1.0',
  swaggerVersion: '1.0',
  swaggerURL: '/docs',           // swaggerページのパス
  swaggerJSON: '/api-docs',      // swagger表示用のデータアクセス先 
  swaggerUI: './dist',           // swagger-uiが置いてあるパス
  basePath: 'http://localhost:3000',
  apis: ['./api.js'],            // ドキュメントが記載されているファイル
  middleware: function(req, res){}
}));

app.listen(3000);

urlの書き換え

dist/index.htmlのL78を上記で指定したパス「http://localhost:3000/api-docs」に書き換え

2017年7月26日水曜日

Sinsyのインストール

LinuxにSinsyをインストールしてみる
Sinsyの利用に必要なもの


【準備】
// hts_engine_APIのダウンロードと解凍
$ wget http://downloads.sourceforge.net/hts-engine/hts_engine_API-1.10.tar.gz
$ tar -zxvf hts_engine_API-1.10.tar.gz

// インストール
$ ./configure
$ make
$ sudo make install

// Sinsyのダウンロードと解凍
$ wget http://downloads.sourceforge.net/sinsy/sinsy-0.92.tar.gz
$ tar -zxvf sinsy-0.92.tar.gz 

// インストール(pathは適宜変更すること)
$ ./configure \
     --with-hts-engine-header-path=/usr/local/include \
     --with-hts-engine-library-path=/usr/local/lib
$ make
$ sudo make install

【利用】
// xmlのダウンロード
$ wget http://sinsy.sp.nitech.ac.jp/sample/song070_f001_063.xml

// htsvoiceの用意
SinsyのページからHTS voiceのバイナリをダウンロード

// 実行
$ sinsy -x /usr/local/dic/ -m nitech_jp_song070_f001.htsvoice -o out.wav song070_f001_063.xml

htsvoiceファイルは、歌声の学習済みのものでないと、とんでもなくおかしなことになる

2017年5月20日土曜日

Operation not permitted

pythonでauthのmoduleをインストールしようとしたら以下のエラーになった。
error: could not create '/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/greenlet': Operation not permitted

mac OS X 10.11以上になってから、いろいろ変わった模様
system integrity protection

$ sudo pip install --user auth

で解決した

2017年4月29日土曜日

input-buffer overflow. The line is split. use -b #SIZE option.

jawikiなどの大きなファイルをわかち書きしようとすると、以下のようなエラーが出る
$ input-buffer overflow. The line is split. use -b #SIZE option.

-b オプションを付けることで解決できる
$ mecab -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd/ -Owakati -b 781361152 jawiki.txt > jawiki_wakachi.txt

指定するサイズはwcコマンドで確認
$ wc jawiki.txt 
  2351310   3832483 781361152 jawiki.txt