analysis-kuromoji.asciidoc 15 KB

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