Skip to content

Commit 3ab1c74

Browse files
committed
Merge branch 'release/0.1.2'
2 parents bd7f019 + f4017b1 commit 3ab1c74

15 files changed

+696
-673
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pom.xml.asc
99
/.nrepl-port
1010
.hgignore
1111
.hg/
12-
system_full.dic
12+
system_full.dic
13+
dict.edn

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Change Log
22

33
## [Unreleased]
4+
5+
## [0.1.2] - 2020-05-14
6+
### Changed
7+
- 形態素解析ライブラリを Sudachi から Kuromoji へ変更
8+
49
### Changed
510
- 辞書の読み込みに edn を使用するよう変更
611

@@ -12,5 +17,6 @@
1217
### Published
1318
- Initial Release
1419

15-
[Unreleased]: https://github.com/sandmark/unmo-clojure/compare/0.1.1...HEAD
20+
[Unreleased]: https://github.com/sandmark/unmo-clojure/compare/0.1.2...HEAD
21+
[0.1.2]: https://github.com/sandmark/unmo-clojure/compare/0.1.1...0.1.2
1622
[0.1.1]: https://github.com/sandmark/unmo-clojure/compare/0.1.0...0.1.1

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@ Unmo は日本語チャットボットプログラムです。機械学習では
1616
### 必要なもの
1717
実行には JVM(Java 仮想マシン)が必要です。https://java.com/ja/download/ からダウンロード・インストールしてください。
1818

19-
また、形態素解析エンジンとして [Sudachi](https://github.com/WorksApplications/Sudachi/releases) を使用しています。辞書ファイルが必要になりますので、リンク先から `sudachi-0.1.1-dictionary-full.zip` をダウンロード・展開し、 `system_full.dic``unmo-x.x.x-standalone.jar` と同じディレクトリに置いてください。
20-
2119
Unmo 本体は https://github.com/sandmark/unmo-clojure/releases からダウンロードすることができます。
2220

23-
- `system_full.dic`
24-
- `sudachi_fulldict.json`
25-
- `unmo-x.x.x-standalone.jar`
26-
27-
上記 3 つのファイルが同じディレクトリにあれば準備完了です。
28-
2921
## 使い方
3022

3123
Unmo はコンソールアプリケーションであるため、コマンドを実行する必要があります。
3224

3325
Unix 系なら端末エミュレータ、Mac OS ならターミナル、Windows ならコマンドプロンプトを起動し、 `cd` コマンドで `unmo-x.x.x-standalone.jar` のあるディレクトリへ移動します。その後、
3426

35-
$ java -jar unmo-0.1.0-standalone.jar
36-
27+
$ java -jar unmo-0.1.2-standalone.jar
28+
3729
と打ち込んで Enter キーを叩けば起動します。
3830

3931
ひとしきり会話を楽しんだら、話しかけずに Enter キーを押せば終了します。
@@ -48,7 +40,7 @@ Unix 系なら端末エミュレータ、Mac OS ならターミナル、Windows
4840

4941
## License
5042

51-
Copyright © 2018-2019 sandmark
43+
Copyright © 2018-2020 sandmark
5244

5345
Distributed under the Eclipse Public License either version 1.0 or
5446
any later version.

project.clj

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
(defproject unmo "0.1.1"
2-
:description "A japanese legacy chatbot using Sudachi, a morphological analyser for modern era."
1+
(defproject unmo "0.1.2"
2+
:description "Unmo: A simple japanese chatbot."
33
:url "https://github.com/sandmark/unmo-clojure/"
44
:license {:name "Eclipse Public License"
5-
:url "http://www.eclipse.org/legal/epl-v10.html"}
6-
:repositories [["Sonatype" "https://oss.sonatype.org/content/repositories/snapshots"]]
7-
:dependencies [[org.clojure/clojure "1.10.0"]
8-
[bigml/sampling "3.2"]
9-
[fipp "0.6.14"]
10-
[com.worksap.nlp/sudachi "0.1.1-SNAPSHOT"]]
11-
:plugins [[lein-environ "1.1.0"]
12-
[lein-eftest "0.5.3"]
5+
:url "http://www.eclipse.org/legal/epl-v10.html"}
6+
:dependencies [[org.clojure/clojure "1.10.1"]
7+
[fipp "0.6.23"]
8+
[com.atilika.kuromoji/kuromoji-ipadic "0.9.0"]
9+
[integrant "0.8.0"]]
10+
:plugins [[lein-eftest "0.5.3"]
1311
[lein-auto "0.1.3"]]
1412
:main ^:skip-aot unmo.core
1513
:target-path "target/%s"
16-
:profiles {:dev {:dependencies [[alembic "0.3.2"]
17-
[eftest "0.5.3"]]}
14+
:profiles {:dev {:dependencies [[eftest "0.5.9"]
15+
[fudje "0.9.7"]]}
1816
:uberjar {:aot :all}})

src/unmo/core.clj

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,83 @@
11
(ns unmo.core
22
(:gen-class)
3-
(:require [unmo.responder :refer [response]]
4-
[unmo.dictionary :refer [study save-dictionary load-dictionary]]
5-
[unmo.morph :refer [analyze]]
6-
[unmo.version :refer [unmo-version]]
7-
[bigml.sampling [simple :as simple]]))
3+
(:require [clojure.java.io :as io]
4+
[clojure.string :as str]
5+
[fipp.edn :as fipp]
6+
[unmo.dictionary :as dict]
7+
[unmo.morph :as morph]
8+
[unmo.responder :as resp]
9+
[unmo.version :as ver]))
810

9-
(def ^{:private true
10-
:doc "デフォルトで使用される辞書ファイル名"}
11-
dictionary-file
12-
"dict.clj")
11+
(def dictionary-file "dict.edn")
12+
13+
(def responders {:what resp/response-what
14+
:random resp/response-random
15+
:pattern resp/response-pattern
16+
:template resp/response-template
17+
:markov resp/response-markov})
18+
19+
(defn save-dictionary
20+
"Saves the result of pprint the map dictionary to the specified filename."
21+
[dictionary filename]
22+
(let [data (with-out-str
23+
(binding [*print-length* false]
24+
(fipp/pprint dictionary)))]
25+
(spit filename data :encoding "UTF-8")))
26+
27+
(defn load-dictionary
28+
"Returns a dictionary map read from the specified filename."
29+
[filename]
30+
(if (.exists (io/as-file filename))
31+
(-> filename (slurp :encoding "UTF-8") read-string)
32+
{}))
1333

1434
(defn- rand-responder
15-
"確率によって変動するResponderを返す。
35+
"Returns the one of keywords, :what :random :pattern :template :markov,
36+
which is determined by the probability.
37+
1638
:what 10%
1739
:random 20%
1840
:pattern 30%
1941
:template 20%
2042
:markov 20%"
2143
[]
22-
(-> [:what :random :pattern :template :markov]
23-
(simple/sample :weigh {:what 0.1
24-
:random 0.2
25-
:pattern 0.3
26-
:template 0.2
27-
:markov 0.2})
28-
(first)))
29-
30-
(defn- format-response
31-
"Responder からの結果を整形して返す。"
32-
[{:keys [responder response error]}]
33-
(let [responder-name (-> responder (name) (clojure.string/capitalize))]
34-
(if error
35-
(str responder-name "> 警告: " (:message error))
36-
(str responder-name "> " response))))
44+
(let [n (rand-int 100)]
45+
(cond (< 0 n 10) :what
46+
(< 11 n 30) :random
47+
(< 31 n 60) :pattern
48+
(< 61 n 80) :template
49+
:else :markov)))
3750

3851
(defn- dialogue
39-
"ユーザーからの発言、形態素解析結果、辞書を受け取り、AIの思考結果を整形した文字列を返す。"
52+
"Takes an input, its morphological analysis results, a dictionary, and
53+
returns a sentence generated by the responder. If the responder returned nil,
54+
calls :what responder and returns the result sentence."
4055
([input parts dictionary]
4156
(dialogue input parts dictionary (rand-responder)))
42-
([input parts dictionary responder]
43-
(let [res (-> {:input input
44-
:dictionary dictionary
45-
:parts parts
46-
:responder responder}
47-
(response))]
48-
(case (get-in res [:error :type])
49-
:fatal (format-response res)
50-
nil (format-response res)
51-
(recur input parts dictionary :random)))))
5257

53-
(defn -main
54-
"標準入力からユーザーの発言を受け取り、Responder の結果を表示して繰り返す。"
55-
[& args]
56-
(println (format "Unmo version %s launched." unmo-version))
58+
([input parts dictionary res-key]
59+
(if-let [res ((res-key responders) {:input input
60+
:dictionary dictionary
61+
:parts parts})]
62+
(str (str/capitalize (name res-key)) "> " res)
63+
(dialogue input parts dictionary :what))))
64+
65+
(defn -main [& args]
66+
(morph/start)
67+
(println (format "Unmo version %s launched." ver/unmo-version))
5768
(print "> ")
5869
(flush)
5970

60-
(loop [input (read-line)
71+
(loop [input (read-line)
6172
dictionary (load-dictionary dictionary-file)]
62-
(if (clojure.string/blank? input)
63-
(do (println "Saving dictionary...")
73+
(if (str/blank? input)
74+
(do (println "Shutting down...")
75+
(morph/stop)
6476
(save-dictionary dictionary dictionary-file)
6577
(println "Quit."))
66-
(let [parts (analyze input)
67-
res (dialogue input parts dictionary)]
78+
(let [parts (morph/analyze input)
79+
res (dialogue input parts dictionary)]
6880
(println res)
6981
(print "> ")
7082
(flush)
71-
(recur (read-line) (study dictionary input parts))))))
83+
(recur (read-line) (dict/study dictionary input parts))))))

0 commit comments

Comments
 (0)