analysis-kuromoji.asciidoc 13 KB

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