analysis-kuromoji.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. [[analysis-kuromoji]]
  2. === Japanese (kuromoji) Analysis Plugin
  3. The Japanese (kuromoji) Analysis plugin integrates Lucene kuromoji analysis
  4. module into elasticsearch.
  5. [[analysis-kuromoji-install]]
  6. [float]
  7. ==== Installation
  8. This plugin can be installed using the plugin manager:
  9. [source,sh]
  10. ----------------------------------------------------------------
  11. sudo bin/elasticsearch-plugin install analysis-kuromoji
  12. ----------------------------------------------------------------
  13. The plugin must be installed on every node in the cluster, and each node must
  14. be restarted after installation.
  15. [[analysis-kuromoji-remove]]
  16. [float]
  17. ==== Removal
  18. The plugin can be removed with the following command:
  19. [source,sh]
  20. ----------------------------------------------------------------
  21. sudo bin/elasticsearch-plugin remove analysis-kuromoji
  22. ----------------------------------------------------------------
  23. The node must be stopped before removing the plugin.
  24. [[analysis-kuromoji-analyzer]]
  25. ==== `kuromoji` analyzer
  26. The `kuromoji` analyzer consists of the following tokenizer and token filters:
  27. * <<analysis-kuromoji-tokenizer,`kuromoji_tokenizer`>>
  28. * <<analysis-kuromoji-baseform,`kuromoji_baseform`>> token filter
  29. * <<analysis-kuromoji-speech,`kuromoji_part_of_speech`>> token filter
  30. * {ref}/analysis-cjk-width-tokenfilter.html[`cjk_width`] token filter
  31. * <<analysis-kuromoji-stop,`ja_stop`>> token filter
  32. * <<analysis-kuromoji-stemmer,`kuromoji_stemmer`>> token filter
  33. * {ref}/analysis-lowercase-tokenfilter.html[`lowercase`] token filter
  34. It supports the `mode` and `user_dictionary` settings from
  35. <<analysis-kuromoji-tokenizer,`kuromoji_tokenizer`>>.
  36. [[analysis-kuromoji-charfilter]]
  37. ==== `kuromoji_iteration_mark` character filter
  38. The `kuromoji_iteration_mark` normalizes Japanese horizontal iteration marks
  39. (_odoriji_) to their expanded form. It accepts the following settings:
  40. `normalize_kanji`::
  41. Indicates whether kanji iteration marks should be normalize. Defaults to `true`.
  42. `normalize_kana`::
  43. Indicates whether kana iteration marks should be normalized. Defaults to `true`
  44. [[analysis-kuromoji-tokenizer]]
  45. ==== `kuromoji_tokenizer`
  46. The `kuromoji_tokenizer` accepts the following settings:
  47. `mode`::
  48. +
  49. --
  50. The tokenization mode determines how the tokenizer handles compound and
  51. unknown words. It can be set to:
  52. `normal`::
  53. Normal segmentation, no decomposition for compounds. Example output:
  54. 関西国際空港
  55. アブラカダブラ
  56. `search`::
  57. Segmentation geared towards search. This includes a decompounding process
  58. for long nouns, also including the full compound token as a synonym.
  59. Example output:
  60. 関西, 関西国際空港, 国際, 空港
  61. アブラカダブラ
  62. `extended`::
  63. Extended mode outputs unigrams for unknown words. Example output:
  64. 関西, 国際, 空港
  65. ア, ブ, ラ, カ, ダ, ブ, ラ
  66. --
  67. `discard_punctuation`::
  68. Whether punctuation should be discarded from the output. Defaults to `true`.
  69. `user_dictionary`::
  70. +
  71. --
  72. The Kuromoji tokenizer uses the MeCab-IPADIC dictionary by default. A `user_dictionary`
  73. may be appended to the default dictionary. The dictionary should have the following CSV format:
  74. [source,csv]
  75. -----------------------
  76. <text>,<token 1> ... <token n>,<reading 1> ... <reading n>,<part-of-speech tag>
  77. -----------------------
  78. --
  79. As a demonstration of how the user dictionary can be used, save the following
  80. dictionary to `$ES_HOME/config/userdict_ja.txt`:
  81. [source,csv]
  82. -----------------------
  83. 東京スカイツリー,東京 スカイツリー,トウキョウ スカイツリー,カスタム名詞
  84. -----------------------
  85. `nbest_cost`/`nbest_examples`::
  86. +
  87. --
  88. Additional expert user parameters `nbest_cost` and `nbest_examples` can be used
  89. to include additional tokens that most likely according to the statistical model.
  90. If both parameters are used, the largest number of both is applied.
  91. `nbest_cost`::
  92. The `nbest_cost` parameter specifies an additional Viterbi cost.
  93. The KuromojiTokenizer will include all tokens in Viterbi paths that are
  94. within the nbest_cost value of the best path.
  95. `nbest_examples`::
  96. The `nbest_examples` can be used to find a `nbest_cost` value based on examples.
  97. For example, a value of /箱根山-箱根/成田空港-成田/ indicates that in the texts,
  98. 箱根山 (Mt. Hakone) and 成田空港 (Narita Airport) we'd like a cost that gives is us
  99. 箱根 (Hakone) and 成田 (Narita).
  100. --
  101. Then create an analyzer as follows:
  102. [source,js]
  103. --------------------------------------------------
  104. PUT kuromoji_sample
  105. {
  106. "settings": {
  107. "index": {
  108. "analysis": {
  109. "tokenizer": {
  110. "kuromoji_user_dict": {
  111. "type": "kuromoji_tokenizer",
  112. "mode": "extended",
  113. "discard_punctuation": "false",
  114. "user_dictionary": "userdict_ja.txt"
  115. }
  116. },
  117. "analyzer": {
  118. "my_analyzer": {
  119. "type": "custom",
  120. "tokenizer": "kuromoji_user_dict"
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. POST kuromoji_sample/_analyze?analyzer=my_analyzer&text=東京スカイツリー
  128. --------------------------------------------------
  129. // CONSOLE
  130. The above `analyze` request returns the following:
  131. [source,js]
  132. --------------------------------------------------
  133. # Result
  134. {
  135. "tokens" : [ {
  136. "token" : "東京",
  137. "start_offset" : 0,
  138. "end_offset" : 2,
  139. "type" : "word",
  140. "position" : 0
  141. }, {
  142. "token" : "スカイツリー",
  143. "start_offset" : 2,
  144. "end_offset" : 8,
  145. "type" : "word",
  146. "position" : 1
  147. } ]
  148. }
  149. --------------------------------------------------
  150. // TESTRESPONSE
  151. [[analysis-kuromoji-baseform]]
  152. ==== `kuromoji_baseform` token filter
  153. The `kuromoji_baseform` token filter replaces terms with their
  154. BaseFormAttribute. This acts as a lemmatizer for verbs and adjectives. Example:
  155. [source,js]
  156. --------------------------------------------------
  157. PUT kuromoji_sample
  158. {
  159. "settings": {
  160. "index": {
  161. "analysis": {
  162. "analyzer": {
  163. "my_analyzer": {
  164. "tokenizer": "kuromoji_tokenizer",
  165. "filter": [
  166. "kuromoji_baseform"
  167. ]
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. POST kuromoji_sample/_analyze?analyzer=my_analyzer&text=飲み
  175. --------------------------------------------------
  176. // CONSOLE
  177. which responds with:
  178. [source,js]
  179. --------------------------------------------------
  180. {
  181. "tokens" : [ {
  182. "token" : "飲む",
  183. "start_offset" : 0,
  184. "end_offset" : 2,
  185. "type" : "word",
  186. "position" : 0
  187. } ]
  188. }
  189. --------------------------------------------------
  190. // TESTRESPONSE
  191. [[analysis-kuromoji-speech]]
  192. ==== `kuromoji_part_of_speech` token filter
  193. The `kuromoji_part_of_speech` token filter removes tokens that match a set of
  194. part-of-speech tags. It accepts the following setting:
  195. `stoptags`::
  196. An array of part-of-speech tags that should be removed. It defaults to the
  197. `stoptags.txt` file embedded in the `lucene-analyzer-kuromoji.jar`.
  198. For example:
  199. [source,js]
  200. --------------------------------------------------
  201. PUT kuromoji_sample
  202. {
  203. "settings": {
  204. "index": {
  205. "analysis": {
  206. "analyzer": {
  207. "my_analyzer": {
  208. "tokenizer": "kuromoji_tokenizer",
  209. "filter": [
  210. "my_posfilter"
  211. ]
  212. }
  213. },
  214. "filter": {
  215. "my_posfilter": {
  216. "type": "kuromoji_part_of_speech",
  217. "stoptags": [
  218. "助詞-格助詞-一般",
  219. "助詞-終助詞"
  220. ]
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. POST kuromoji_sample/_analyze?analyzer=my_analyzer&text=寿司がおいしいね
  228. --------------------------------------------------
  229. // CONSOLE
  230. Which responds with:
  231. [source,js]
  232. --------------------------------------------------
  233. {
  234. "tokens" : [ {
  235. "token" : "寿司",
  236. "start_offset" : 0,
  237. "end_offset" : 2,
  238. "type" : "word",
  239. "position" : 0
  240. }, {
  241. "token" : "おいしい",
  242. "start_offset" : 3,
  243. "end_offset" : 7,
  244. "type" : "word",
  245. "position" : 2
  246. } ]
  247. }
  248. --------------------------------------------------
  249. // TESTRESPONSE
  250. [[analysis-kuromoji-readingform]]
  251. ==== `kuromoji_readingform` token filter
  252. The `kuromoji_readingform` token filter replaces the token with its reading
  253. form in either katakana or romaji. It accepts the following setting:
  254. `use_romaji`::
  255. Whether romaji reading form should be output instead of katakana. Defaults to `false`.
  256. When using the pre-defined `kuromoji_readingform` filter, `use_romaji` is set
  257. to `true`. The default when defining a custom `kuromoji_readingform`, however,
  258. is `false`. The only reason to use the custom form is if you need the
  259. katakana reading form:
  260. [source,js]
  261. --------------------------------------------------
  262. PUT kuromoji_sample
  263. {
  264. "settings": {
  265. "index":{
  266. "analysis":{
  267. "analyzer" : {
  268. "romaji_analyzer" : {
  269. "tokenizer" : "kuromoji_tokenizer",
  270. "filter" : ["romaji_readingform"]
  271. },
  272. "katakana_analyzer" : {
  273. "tokenizer" : "kuromoji_tokenizer",
  274. "filter" : ["katakana_readingform"]
  275. }
  276. },
  277. "filter" : {
  278. "romaji_readingform" : {
  279. "type" : "kuromoji_readingform",
  280. "use_romaji" : true
  281. },
  282. "katakana_readingform" : {
  283. "type" : "kuromoji_readingform",
  284. "use_romaji" : false
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. POST kuromoji_sample/_analyze?analyzer=katakana_analyzer&text=寿司 <1>
  292. POST kuromoji_sample/_analyze?analyzer=romaji_analyzer&text=寿司 <2>
  293. --------------------------------------------------
  294. // CONSOLE
  295. <1> Returns `スシ`.
  296. <2> Returns `sushi`.
  297. [[analysis-kuromoji-stemmer]]
  298. ==== `kuromoji_stemmer` token filter
  299. The `kuromoji_stemmer` token filter normalizes common katakana spelling
  300. variations ending in a long sound character by removing this character
  301. (U+30FC). Only full-width katakana characters are supported.
  302. This token filter accepts the following setting:
  303. `minimum_length`::
  304. Katakana words shorter than the `minimum length` are not stemmed (default
  305. is `4`).
  306. [source,js]
  307. --------------------------------------------------
  308. PUT kuromoji_sample
  309. {
  310. "settings": {
  311. "index": {
  312. "analysis": {
  313. "analyzer": {
  314. "my_analyzer": {
  315. "tokenizer": "kuromoji_tokenizer",
  316. "filter": [
  317. "my_katakana_stemmer"
  318. ]
  319. }
  320. },
  321. "filter": {
  322. "my_katakana_stemmer": {
  323. "type": "kuromoji_stemmer",
  324. "minimum_length": 4
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. POST kuromoji_sample/_analyze?analyzer=my_analyzer&text=コピー <1>
  332. POST kuromoji_sample/_analyze?analyzer=my_analyzer&text=サーバー <2>
  333. --------------------------------------------------
  334. // CONSOLE
  335. <1> Returns `コピー`.
  336. <2> Return `サーバ`.
  337. [[analysis-kuromoji-stop]]
  338. ===== `ja_stop` token filter
  339. The `ja_stop` token filter filters out Japanese stopwords (`_japanese_`), and
  340. any other custom stopwords specified by the user. This filter only supports
  341. the predefined `_japanese_` stopwords list. If you want to use a different
  342. predefined list, then use the
  343. {ref}/analysis-stop-tokenfilter.html[`stop` token filter] instead.
  344. [source,js]
  345. --------------------------------------------------
  346. PUT kuromoji_sample
  347. {
  348. "settings": {
  349. "index": {
  350. "analysis": {
  351. "analyzer": {
  352. "analyzer_with_ja_stop": {
  353. "tokenizer": "kuromoji_tokenizer",
  354. "filter": [
  355. "ja_stop"
  356. ]
  357. }
  358. },
  359. "filter": {
  360. "ja_stop": {
  361. "type": "ja_stop",
  362. "stopwords": [
  363. "_japanese_",
  364. "ストップ"
  365. ]
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. POST kuromoji_sample/_analyze?analyzer=analyzer_with_ja_stop&text=ストップは消える
  373. --------------------------------------------------
  374. // CONSOLE
  375. The above request returns:
  376. [source,js]
  377. --------------------------------------------------
  378. {
  379. "tokens" : [ {
  380. "token" : "消える",
  381. "start_offset" : 5,
  382. "end_offset" : 8,
  383. "type" : "word",
  384. "position" : 2
  385. } ]
  386. }
  387. --------------------------------------------------
  388. // TESTRESPONSE
  389. [[analysis-kuromoji-number]]
  390. ===== `kuromoji_number` token filter
  391. The `kuromoji_number` token filter normalizes Japanese numbers (kansūji)
  392. to regular Arabic decimal numbers in half-width characters. For example:
  393. [source,js]
  394. --------------------------------------------------
  395. PUT kuromoji_sample
  396. {
  397. "settings": {
  398. "index": {
  399. "analysis": {
  400. "analyzer": {
  401. "my_analyzer": {
  402. "tokenizer": "kuromoji_tokenizer",
  403. "filter": [
  404. "kuromoji_number"
  405. ]
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. POST kuromoji_sample/_analyze?analyzer=my_analyzer&text=一〇〇〇
  413. --------------------------------------------------
  414. // CONSOLE
  415. Which results in:
  416. [source,js]
  417. --------------------------------------------------
  418. {
  419. "tokens" : [ {
  420. "token" : "1000",
  421. "start_offset" : 0,
  422. "end_offset" : 4,
  423. "type" : "word",
  424. "position" : 0
  425. } ]
  426. }
  427. --------------------------------------------------
  428. // TESTRESPONSE