unicodedataを使えばよい。
$ python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import unicodedata >>> print unicodedata.normalize(u'NFKC', u'テスト①%@') テスト1%@
Challange IT For Future
unicodedataを使えばよい。
$ python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import unicodedata >>> print unicodedata.normalize(u'NFKC', u'テスト①%@') テスト1%@
ElasticsearchのPython用クライアントであるelasticsearch-pyを試してみる。
pipが利用できる環境であれば、
$ sudo pip install elasticsearch
とすれば利用できるようになる。
あとは、以下のような感じでes.pyとか適当に作って、
# -*- coding: utf-8 -*- from datetime import datetime from elasticsearch import Elasticsearch es = Elasticsearch("localhost:9200") # データの登録 res1 = es.index(index="sample", doc_type="data", body={"msg":"Hello", "timestamp": datetime.now()}, id="1") print(res1) # 検索 res2 = es.search(index="sample", doc_type="data", body={"query":{"match_all":{}}}) print(res2) # インデックスの削除 res3 = es.indices.delete(index="sample") print(res3)
これを実行すれば、アクセスすることができます。
結構、簡単に使えますね。
形態素解析器IgoをPythonで利用する方法をまとめます。Mecabとかもありますが、クロスプラットフォームとかで環境準備しようとすると、面倒な部分もあるので…。
今回は、Ubuntu 14.04にインストールします。pipコマンドでインストールしようと思いますが、インストールしていなければ、インストールしておきます。
$ sudo apt-get install python-pip
そして、IgoをPythonで利用するためにigo-pythonをインストールします。
$ sudo pip install igo-python
インストールしたら、辞書を準備します。MecabのIPA辞書を以下のコマンドでIgo用の辞書に変換します。
$ wget -O igo-0.4.5.jar "http://sourceforge.jp/frs/redir.php?m=iij&f=%2Figo%2F55029%2Figo-0.4.5.jar" $ wget -O mecab-ipadic-2.7.0-20070801.tar.gz "http://sourceforge.jp/frs/g_redir.php?m=jaist&f=%2Fmecab%2Fmecab-ipadic%2F2.7.0-20070801%2Fmecab-ipadic-2.7.0-20070801.tar.gz" $ tar zxvf mecab-ipadic-2.7.0-20070801.tar.gz $ java -cp igo-0.4.5.jar net.reduls.igo.bin.BuildDic ipadic mecab-ipadic-2.7.0-20070801 EUC_JP
igoのjarファイルとIPA辞書を取得して、igoのjarファイルを用いて、IPA辞書をipadicディレクトリに出力しています。
ここまでできたら、あとは動作確認します。たとえば、以下のようなtest.pyを作成して実行します。
# -*- coding: utf-8 -*- from igo.tagger import Tagger if __name__ == '__main__': tagger = Tagger('ipadic') words = tagger.parse(u'明日東京へ行く。') for word in words: print word.surface, word.feature, word.start
実行結果は以下のようになります。
明日 名詞,副詞可能,*,*,*,*,明日,アシタ,アシタ 0 東京 名詞,固有名詞,地域,一般,*,*,東京,トウキョウ,トーキョー 2 へ 助詞,格助詞,一般,*,*,*,へ,ヘ,エ 4 行く 動詞,自立,*,*,五段・カ行促音便,基本形,行く,イク,イク 5 。 記号,句点,*,*,*,*,。,。,。 7
以上のような感じで、Pythonで手軽に形態素解析を利用することができます。