analysis-nori.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. [[analysis-nori]]
  2. === Korean (nori) Analysis Plugin
  3. The Korean (nori) Analysis plugin integrates Lucene nori analysis
  4. module into elasticsearch. It uses the https://bitbucket.org/eunjeon/mecab-ko-dic[mecab-ko-dic dictionary]
  5. to perform morphological analysis of Korean texts.
  6. :plugin_name: analysis-nori
  7. include::install_remove.asciidoc[]
  8. [[analysis-nori-analyzer]]
  9. ==== `nori` analyzer
  10. The `nori` analyzer consists of the following tokenizer and token filters:
  11. * <<analysis-nori-tokenizer,`nori_tokenizer`>>
  12. * <<analysis-nori-speech,`nori_part_of_speech`>> token filter
  13. * <<analysis-nori-readingform,`nori_readingform`>> token filter
  14. * {ref}/analysis-lowercase-tokenfilter.html[`lowercase`] token filter
  15. It supports the `decompound_mode` and `user_dictionary` settings from
  16. <<analysis-nori-tokenizer,`nori_tokenizer`>> and the `stoptags` setting from
  17. <<analysis-nori-speech,`nori_part_of_speech`>>.
  18. [[analysis-nori-tokenizer]]
  19. ==== `nori_tokenizer`
  20. The `nori_tokenizer` accepts the following settings:
  21. `decompound_mode`::
  22. +
  23. --
  24. The decompound mode determines how the tokenizer handles compound tokens.
  25. It can be set to:
  26. `none`::
  27. No decomposition for compounds. Example output:
  28. 가거도항
  29. 가곡역
  30. `discard`::
  31. Decomposes compounds and discards the original form (*default*). Example output:
  32. 가곡역 => 가곡, 역
  33. `mixed`::
  34. Decomposes compounds and keeps the original form. Example output:
  35. 가곡역 => 가곡역, 가곡, 역
  36. --
  37. `discard_punctuation`::
  38. Whether punctuation should be discarded from the output. Defaults to `true`.
  39. `user_dictionary`::
  40. +
  41. --
  42. The Nori tokenizer uses the https://bitbucket.org/eunjeon/mecab-ko-dic[mecab-ko-dic dictionary] by default.
  43. A `user_dictionary` with custom nouns (`NNG`) may be appended to the default dictionary.
  44. The dictionary should have the following format:
  45. [source,txt]
  46. -----------------------
  47. <token> [<token 1> ... <token n>]
  48. -----------------------
  49. The first token is mandatory and represents the custom noun that should be added in
  50. the dictionary. For compound nouns the custom segmentation can be provided
  51. after the first token (`[<token 1> ... <token n>]`). The segmentation of the
  52. custom compound nouns is controlled by the `decompound_mode` setting.
  53. As a demonstration of how the user dictionary can be used, save the following
  54. dictionary to `$ES_HOME/config/userdict_ko.txt`:
  55. [source,txt]
  56. -----------------------
  57. c++ <1>
  58. C샤프
  59. 세종
  60. 세종시 세종 시 <2>
  61. -----------------------
  62. <1> A simple noun
  63. <2> A compound noun (`세종시`) followed by its decomposition: `세종` and `시`.
  64. Then create an analyzer as follows:
  65. [source,console]
  66. --------------------------------------------------
  67. PUT nori_sample
  68. {
  69. "settings": {
  70. "index": {
  71. "analysis": {
  72. "tokenizer": {
  73. "nori_user_dict": {
  74. "type": "nori_tokenizer",
  75. "decompound_mode": "mixed",
  76. "discard_punctuation": "false",
  77. "user_dictionary": "userdict_ko.txt"
  78. }
  79. },
  80. "analyzer": {
  81. "my_analyzer": {
  82. "type": "custom",
  83. "tokenizer": "nori_user_dict"
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. GET nori_sample/_analyze
  91. {
  92. "analyzer": "my_analyzer",
  93. "text": "세종시" <1>
  94. }
  95. --------------------------------------------------
  96. <1> Sejong city
  97. The above `analyze` request returns the following:
  98. [source,console-result]
  99. --------------------------------------------------
  100. {
  101. "tokens" : [ {
  102. "token" : "세종시",
  103. "start_offset" : 0,
  104. "end_offset" : 3,
  105. "type" : "word",
  106. "position" : 0,
  107. "positionLength" : 2 <1>
  108. }, {
  109. "token" : "세종",
  110. "start_offset" : 0,
  111. "end_offset" : 2,
  112. "type" : "word",
  113. "position" : 0
  114. }, {
  115. "token" : "시",
  116. "start_offset" : 2,
  117. "end_offset" : 3,
  118. "type" : "word",
  119. "position" : 1
  120. }]
  121. }
  122. --------------------------------------------------
  123. <1> This is a compound token that spans two positions (`mixed` mode).
  124. --
  125. `user_dictionary_rules`::
  126. +
  127. --
  128. You can also inline the rules directly in the tokenizer definition using
  129. the `user_dictionary_rules` option:
  130. [source,console]
  131. --------------------------------------------------
  132. PUT nori_sample
  133. {
  134. "settings": {
  135. "index": {
  136. "analysis": {
  137. "tokenizer": {
  138. "nori_user_dict": {
  139. "type": "nori_tokenizer",
  140. "decompound_mode": "mixed",
  141. "user_dictionary_rules": ["c++", "C샤프", "세종", "세종시 세종 시"]
  142. }
  143. },
  144. "analyzer": {
  145. "my_analyzer": {
  146. "type": "custom",
  147. "tokenizer": "nori_user_dict"
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. --------------------------------------------------
  155. --
  156. The `nori_tokenizer` sets a number of additional attributes per token that are used by token filters
  157. to modify the stream.
  158. You can view all these additional attributes with the following request:
  159. [source,console]
  160. --------------------------------------------------
  161. GET _analyze
  162. {
  163. "tokenizer": "nori_tokenizer",
  164. "text": "뿌리가 깊은 나무는", <1>
  165. "attributes" : ["posType", "leftPOS", "rightPOS", "morphemes", "reading"],
  166. "explain": true
  167. }
  168. --------------------------------------------------
  169. <1> A tree with deep roots
  170. Which responds with:
  171. [source,console-result]
  172. --------------------------------------------------
  173. {
  174. "detail": {
  175. "custom_analyzer": true,
  176. "charfilters": [],
  177. "tokenizer": {
  178. "name": "nori_tokenizer",
  179. "tokens": [
  180. {
  181. "token": "뿌리",
  182. "start_offset": 0,
  183. "end_offset": 2,
  184. "type": "word",
  185. "position": 0,
  186. "leftPOS": "NNG(General Noun)",
  187. "morphemes": null,
  188. "posType": "MORPHEME",
  189. "reading": null,
  190. "rightPOS": "NNG(General Noun)"
  191. },
  192. {
  193. "token": "가",
  194. "start_offset": 2,
  195. "end_offset": 3,
  196. "type": "word",
  197. "position": 1,
  198. "leftPOS": "J(Ending Particle)",
  199. "morphemes": null,
  200. "posType": "MORPHEME",
  201. "reading": null,
  202. "rightPOS": "J(Ending Particle)"
  203. },
  204. {
  205. "token": "깊",
  206. "start_offset": 4,
  207. "end_offset": 5,
  208. "type": "word",
  209. "position": 2,
  210. "leftPOS": "VA(Adjective)",
  211. "morphemes": null,
  212. "posType": "MORPHEME",
  213. "reading": null,
  214. "rightPOS": "VA(Adjective)"
  215. },
  216. {
  217. "token": "은",
  218. "start_offset": 5,
  219. "end_offset": 6,
  220. "type": "word",
  221. "position": 3,
  222. "leftPOS": "E(Verbal endings)",
  223. "morphemes": null,
  224. "posType": "MORPHEME",
  225. "reading": null,
  226. "rightPOS": "E(Verbal endings)"
  227. },
  228. {
  229. "token": "나무",
  230. "start_offset": 7,
  231. "end_offset": 9,
  232. "type": "word",
  233. "position": 4,
  234. "leftPOS": "NNG(General Noun)",
  235. "morphemes": null,
  236. "posType": "MORPHEME",
  237. "reading": null,
  238. "rightPOS": "NNG(General Noun)"
  239. },
  240. {
  241. "token": "는",
  242. "start_offset": 9,
  243. "end_offset": 10,
  244. "type": "word",
  245. "position": 5,
  246. "leftPOS": "J(Ending Particle)",
  247. "morphemes": null,
  248. "posType": "MORPHEME",
  249. "reading": null,
  250. "rightPOS": "J(Ending Particle)"
  251. }
  252. ]
  253. },
  254. "tokenfilters": []
  255. }
  256. }
  257. --------------------------------------------------
  258. [[analysis-nori-speech]]
  259. ==== `nori_part_of_speech` token filter
  260. The `nori_part_of_speech` token filter removes tokens that match a set of
  261. part-of-speech tags. The list of supported tags and their meanings can be found here:
  262. {lucene-core-javadoc}/../analyzers-nori/org/apache/lucene/analysis/ko/POS.Tag.html[Part of speech tags]
  263. It accepts the following setting:
  264. `stoptags`::
  265. An array of part-of-speech tags that should be removed.
  266. and defaults to:
  267. [source,js]
  268. --------------------------------------------------
  269. "stoptags": [
  270. "E",
  271. "IC",
  272. "J",
  273. "MAG", "MAJ", "MM",
  274. "SP", "SSC", "SSO", "SC", "SE",
  275. "XPN", "XSA", "XSN", "XSV",
  276. "UNA", "NA", "VSV"
  277. ]
  278. --------------------------------------------------
  279. // NOTCONSOLE
  280. For example:
  281. [source,console]
  282. --------------------------------------------------
  283. PUT nori_sample
  284. {
  285. "settings": {
  286. "index": {
  287. "analysis": {
  288. "analyzer": {
  289. "my_analyzer": {
  290. "tokenizer": "nori_tokenizer",
  291. "filter": [
  292. "my_posfilter"
  293. ]
  294. }
  295. },
  296. "filter": {
  297. "my_posfilter": {
  298. "type": "nori_part_of_speech",
  299. "stoptags": [
  300. "NR" <1>
  301. ]
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. GET nori_sample/_analyze
  309. {
  310. "analyzer": "my_analyzer",
  311. "text": "여섯 용이" <2>
  312. }
  313. --------------------------------------------------
  314. <1> Korean numerals should be removed (`NR`)
  315. <2> Six dragons
  316. Which responds with:
  317. [source,console-result]
  318. --------------------------------------------------
  319. {
  320. "tokens" : [ {
  321. "token" : "용",
  322. "start_offset" : 3,
  323. "end_offset" : 4,
  324. "type" : "word",
  325. "position" : 1
  326. }, {
  327. "token" : "이",
  328. "start_offset" : 4,
  329. "end_offset" : 5,
  330. "type" : "word",
  331. "position" : 2
  332. } ]
  333. }
  334. --------------------------------------------------
  335. [[analysis-nori-readingform]]
  336. ==== `nori_readingform` token filter
  337. The `nori_readingform` token filter rewrites tokens written in Hanja to their Hangul form.
  338. [source,console]
  339. --------------------------------------------------
  340. PUT nori_sample
  341. {
  342. "settings": {
  343. "index": {
  344. "analysis": {
  345. "analyzer": {
  346. "my_analyzer": {
  347. "tokenizer": "nori_tokenizer",
  348. "filter": [ "nori_readingform" ]
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. GET nori_sample/_analyze
  356. {
  357. "analyzer": "my_analyzer",
  358. "text": "鄕歌" <1>
  359. }
  360. --------------------------------------------------
  361. <1> A token written in Hanja: Hyangga
  362. Which responds with:
  363. [source,console-result]
  364. --------------------------------------------------
  365. {
  366. "tokens" : [ {
  367. "token" : "향가", <1>
  368. "start_offset" : 0,
  369. "end_offset" : 2,
  370. "type" : "word",
  371. "position" : 0
  372. }]
  373. }
  374. --------------------------------------------------
  375. <1> The Hanja form is replaced by the Hangul translation.
  376. [[analysis-nori-number]]
  377. ==== `nori_number` token filter
  378. The `nori_number` token filter normalizes Korean numbers
  379. to regular Arabic decimal numbers in half-width characters.
  380. Korean numbers are often written using a combination of Hangul and Arabic numbers with various kinds punctuation.
  381. For example, 3.2천 means 3200.
  382. This filter does this kind of normalization and allows a search for 3200 to match 3.2천 in text,
  383. but can also be used to make range facets based on the normalized numbers and so on.
  384. [NOTE]
  385. ====
  386. Notice that this analyzer uses a token composition scheme and relies on punctuation tokens
  387. being found in the token stream.
  388. Please make sure your `nori_tokenizer` has `discard_punctuation` set to false.
  389. In case punctuation characters, such as U+FF0E(.), is removed from the token stream,
  390. this filter would find input tokens 3 and 2천 and give outputs 3 and 2000 instead of 3200,
  391. which is likely not the intended result.
  392. If you want to remove punctuation characters from your index that are not part of normalized numbers,
  393. add a `stop` token filter with the punctuation you wish to remove after `nori_number` in your analyzer chain.
  394. ====
  395. Below are some examples of normalizations this filter supports.
  396. The input is untokenized text and the result is the single term attribute emitted for the input.
  397. - 영영칠 -> 7
  398. - 일영영영 -> 1000
  399. - 삼천2백2십삼 -> 3223
  400. - 조육백만오천일 -> 1000006005001
  401. - 3.2천 -> 3200
  402. - 1.2만345.67 -> 12345.67
  403. - 4,647.100 -> 4647.1
  404. - 15,7 -> 157 (be aware of this weakness)
  405. For example:
  406. [source,console]
  407. --------------------------------------------------
  408. PUT nori_sample
  409. {
  410. "settings": {
  411. "index": {
  412. "analysis": {
  413. "analyzer": {
  414. "my_analyzer": {
  415. "tokenizer": "tokenizer_discard_puncuation_false",
  416. "filter": [
  417. "part_of_speech_stop_sp", "nori_number"
  418. ]
  419. }
  420. },
  421. "tokenizer": {
  422. "tokenizer_discard_puncuation_false": {
  423. "type": "nori_tokenizer",
  424. "discard_punctuation": "false"
  425. }
  426. },
  427. "filter": {
  428. "part_of_speech_stop_sp": {
  429. "type": "nori_part_of_speech",
  430. "stoptags": ["SP"]
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. GET nori_sample/_analyze
  438. {
  439. "analyzer": "my_analyzer",
  440. "text": "십만이천오백과 3.2천"
  441. }
  442. --------------------------------------------------
  443. Which results in:
  444. [source,console-result]
  445. --------------------------------------------------
  446. {
  447. "tokens" : [{
  448. "token" : "102500",
  449. "start_offset" : 0,
  450. "end_offset" : 6,
  451. "type" : "word",
  452. "position" : 0
  453. }, {
  454. "token" : "과",
  455. "start_offset" : 6,
  456. "end_offset" : 7,
  457. "type" : "word",
  458. "position" : 1
  459. }, {
  460. "token" : "3200",
  461. "start_offset" : 8,
  462. "end_offset" : 12,
  463. "type" : "word",
  464. "position" : 2
  465. }]
  466. }
  467. --------------------------------------------------