在macOS Sierra上使用Elasticsearch快速创建形态分析环境的步骤

准备去完成某件事或为某个活动做准备。

非常抱歉麻烦你,但是由于以后需要,麻烦你在下面的网页上安装macOS上的Jupyter Notebook。
http://qiita.com/mix_dvd/items/d915752215db67919c06

确认并安装JAVA。

为了确认是否已安装,请执行以下命令。

$ java -version

如果未安装,则会显示以下对话框,请点击“详细信息…”按钮。

スクリーンショット 2016-07-20 11.23.23.png

打开上述网站,然后下载并安装JDK。

スクリーンショット 2016-07-20 11.26.24.png

在安装后,再次运行命令以确认已完成安装。

$ java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

安装Elasticsearch

[官方网站] https://www.elastic.co/zh/products/elasticsearch

安装程序

执行以下命令

$ curl -O https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/zip/elasticsearch/2.3.4/elasticsearch-2.3.4.zip
$ unzip elasticsearch-2.3.4.zip
$ sudo mv elasticsearch-2.3.4 /usr/local/elasticsearch

确认版本

$ /usr/local/elasticsearch/bin/elasticsearch --version
Version: 2.3.4, Build: e455fd0/2016-06-30T11:24:31Z, JVM: 1.8.0_101

安装插件

执行以下指令

$ cd /usr/local/elasticsearch
$ bin/plugin install analysis-kuromoji

启动

执行以下命令

$ /usr/local/elasticsearch/bin/elasticsearch

确认操作

打开另一个终端,并执行以下命令。

$ curl localhost:9200

或者,在网络浏览器中访问下面的URL

如果有以下的回应,说明启动成功。

{
  "name" : "Akasha",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.3.4",
    "build_hash" : "Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "build_timestamp" : "2016-06-30T11:24:31Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

安装Python库。

执行以下命令

$ pip install elasticsearch

运行示例代码

将下面的代码保存为test.py。


# coding: utf-8

# # Elasticsearch

# In[1]:

from elasticsearch import Elasticsearch
es = Elasticsearch("localhost:9200")
es


# # 変数の初期化

# In[2]:

esIndex = "bot"
esType = "talks"


# # インデックスの追加

# - curl -X POST http://localhost:9200/bot/talks -d '{"mode":"あいさつ", "words":"おはようございます"}'

# In[3]:

es.index(index=esIndex, doc_type=esType, body={"mode":"あいさつ", "words":"おはようございます"})


# In[4]:

es.index(index=esIndex, doc_type=esType, body={"mode":"あいさつ", "words":"こんにちは"})
es.index(index=esIndex, doc_type=esType, body={"mode":"あいさつ", "words":"こんばんは"})
es.index(index=esIndex, doc_type=esType, body={"mode":"あいさつ", "words":"さようなら"})
es.index(index=esIndex, doc_type=esType, body={"mode":"あいさつ", "words":"おやすみなさい"})
es.index(index=esIndex, doc_type=esType, body={"mode":"名言", "words":"死して屍拾うものなし"})


# # インデックスの修正

# - curl -X POST http://localhost:9200/bot/talks?id=AVYGQm6Q8mtRod8eIWiq -d '{"mode":"あいさつ","words":"お休みなさい"}'
# 
# idが存在すれば更新、idが存在しなければ追加

# In[21]:

es.index(index=esIndex, doc_type=esType, id="AVYGQm6Q8mtRod8eIWiq", body={"mode":"あいさつ", "words":"また明日"})


# # データ取得

# - curl -X GET http://localhost:9200/bot/talks/_search?pretty -d '{"query":{"match_all":{}}}'

# In[29]:

res = es.search(index=esIndex, body={"query": {"match_all": {}}})
res


# In[23]:

len(res["hits"]["hits"])

words = []
modes = []

for i in range(len(res["hits"]["hits"])):
    row = res["hits"]["hits"][i]["_source"]
    print(row)
    words.append(row["words"])
    modes.append(row["mode"])


# # データ削除

# - curl -X DELETE http://localhost:9200/bot/

# In[8]:

#es.indices.delete(index="bot")


# # プラグインの利用

# - 形態素解析

# In[24]:

text = "今日はいい天気ですね"


# In[25]:

def analyze(es, text):

    params = {"analyzer":"kuromoji"}
    body = {"text":text}

    http_status, data = es.indices.client.transport.perform_request(
        'GET',
        '/' + esIndex + '/_analyze',
        params=params,
        body=body
    )

    return map(lambda x: x.get('token'), data.get('tokens')[0:])


# In[26]:

tokens = analyze(es, text)
print(' '.join(tokens))


# In[30]:

for word in words:
    print(' '.join(analyze(es, word)))

执行以下命令

$ python test.py

只要按照以下方式回答,就能成功!

{'mode': 'あいさつ', 'words': 'おはようございます'}
{'mode': 'あいさつ', 'words': 'こんばんは'}
{'mode': 'あいさつ', 'words': 'こんにちは'}
{'mode': 'あいさつ', 'words': 'さようなら'}
{'mode': 'あいさつ', 'words': 'おやすみなさい'}
{'mode': '名言', 'words': '死して屍拾うものなし'}
{'mode': 'あいさつ', 'words': 'また明日'}
今日 いい 天気
おはよう
こんばんは
こんにちは
さようなら
おやすみなさい
死す 屍 拾う
明日

嗯,接下来要做些什么呢(^_^;)

补充

啊,没有用Jupyter Notebook呢(汗)

广告
将在 10 秒后关闭
bannerAds