analysis-nori.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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-reading,`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. `user_dictionary`::
  38. +
  39. --
  40. The Nori tokenizer uses the https://bitbucket.org/eunjeon/mecab-ko-dic[mecab-ko-dic dictionary] by default.
  41. A `user_dictionary` with custom nouns (`NNG`) may be appended to the default dictionary.
  42. The dictionary should have the following format:
  43. [source,txt]
  44. -----------------------
  45. <token> [<token 1> ... <token n>]
  46. -----------------------
  47. The first token is mandatory and represents the custom noun that should be added in
  48. the dictionary. For compound nouns the custom segmentation can be provided
  49. after the first token (`[<token 1> ... <token n>]`). The segmentation of the
  50. custom compound nouns is controlled by the `decompound_mode` setting.
  51. --
  52. As a demonstration of how the user dictionary can be used, save the following
  53. dictionary to `$ES_HOME/config/userdict_ko.txt`:
  54. [source,txt]
  55. -----------------------
  56. c++ <1>
  57. C샤프
  58. 세종
  59. 세종시 세종 시 <2>
  60. -----------------------
  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,js]
  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. "user_dictionary": "userdict_ko.txt"
  77. }
  78. },
  79. "analyzer": {
  80. "my_analyzer": {
  81. "type": "custom",
  82. "tokenizer": "nori_user_dict"
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. GET nori_sample/_analyze
  90. {
  91. "analyzer": "my_analyzer",
  92. "text": "세종시" <1>
  93. }
  94. --------------------------------------------------
  95. // CONSOLE
  96. <1> Sejong city
  97. The above `analyze` request returns the following:
  98. [source,js]
  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. // TESTRESPONSE
  124. <1> This is a compound token that spans two positions (`mixed` mode).
  125. The `nori_tokenizer` sets a number of additional attributes per token that are used by token filters
  126. to modify the stream.
  127. You can view all these additional attributes with the following request:
  128. [source,js]
  129. --------------------------------------------------
  130. GET _analyze
  131. {
  132. "tokenizer": "nori_tokenizer",
  133. "text": "뿌리가 깊은 나무는", <1>
  134. "attributes" : ["posType", "leftPOS", "rightPOS", "morphemes", "reading"],
  135. "explain": true
  136. }
  137. --------------------------------------------------
  138. // CONSOLE
  139. <1> A tree with deep roots
  140. Which responds with:
  141. [source,js]
  142. --------------------------------------------------
  143. {
  144. "detail": {
  145. "custom_analyzer": true,
  146. "charfilters": [],
  147. "tokenizer": {
  148. "name": "nori_tokenizer",
  149. "tokens": [
  150. {
  151. "token": "뿌리",
  152. "start_offset": 0,
  153. "end_offset": 2,
  154. "type": "word",
  155. "position": 0,
  156. "leftPOS": "NNG(General Noun)",
  157. "morphemes": null,
  158. "posType": "MORPHEME",
  159. "reading": null,
  160. "rightPOS": "NNG(General Noun)"
  161. },
  162. {
  163. "token": "가",
  164. "start_offset": 2,
  165. "end_offset": 3,
  166. "type": "word",
  167. "position": 1,
  168. "leftPOS": "J(Ending Particle)",
  169. "morphemes": null,
  170. "posType": "MORPHEME",
  171. "reading": null,
  172. "rightPOS": "J(Ending Particle)"
  173. },
  174. {
  175. "token": "깊",
  176. "start_offset": 4,
  177. "end_offset": 5,
  178. "type": "word",
  179. "position": 2,
  180. "leftPOS": "VA(Adjective)",
  181. "morphemes": null,
  182. "posType": "MORPHEME",
  183. "reading": null,
  184. "rightPOS": "VA(Adjective)"
  185. },
  186. {
  187. "token": "은",
  188. "start_offset": 5,
  189. "end_offset": 6,
  190. "type": "word",
  191. "position": 3,
  192. "leftPOS": "E(Verbal endings)",
  193. "morphemes": null,
  194. "posType": "MORPHEME",
  195. "reading": null,
  196. "rightPOS": "E(Verbal endings)"
  197. },
  198. {
  199. "token": "나무",
  200. "start_offset": 7,
  201. "end_offset": 9,
  202. "type": "word",
  203. "position": 4,
  204. "leftPOS": "NNG(General Noun)",
  205. "morphemes": null,
  206. "posType": "MORPHEME",
  207. "reading": null,
  208. "rightPOS": "NNG(General Noun)"
  209. },
  210. {
  211. "token": "는",
  212. "start_offset": 9,
  213. "end_offset": 10,
  214. "type": "word",
  215. "position": 5,
  216. "leftPOS": "J(Ending Particle)",
  217. "morphemes": null,
  218. "posType": "MORPHEME",
  219. "reading": null,
  220. "rightPOS": "J(Ending Particle)"
  221. }
  222. ]
  223. },
  224. "tokenfilters": []
  225. }
  226. }
  227. --------------------------------------------------
  228. // TESTRESPONSE
  229. [[analysis-nori-speech]]
  230. ==== `nori_part_of_speech` token filter
  231. The `nori_part_of_speech` token filter removes tokens that match a set of
  232. part-of-speech tags. The list of supported tags and their meanings can be found here:
  233. {lucene_version_path}/org/apache/lucene/analysis/ko/POS.Tag.html[Part of speech tags]
  234. It accepts the following setting:
  235. `stoptags`::
  236. An array of part-of-speech tags that should be removed.
  237. and defaults to:
  238. ```
  239. "stoptags": [
  240. "E",
  241. "IC",
  242. "J",
  243. "MAG", "MAJ", "MM",
  244. "SP", "SSC", "SSO", "SC", "SE",
  245. "XPN", "XSA", "XSN", "XSV",
  246. "UNA", "NA", "VSV"
  247. ]
  248. ```
  249. For example:
  250. [source,js]
  251. --------------------------------------------------
  252. PUT nori_sample
  253. {
  254. "settings": {
  255. "index": {
  256. "analysis": {
  257. "analyzer": {
  258. "my_analyzer": {
  259. "tokenizer": "nori_tokenizer",
  260. "filter": [
  261. "my_posfilter"
  262. ]
  263. }
  264. },
  265. "filter": {
  266. "my_posfilter": {
  267. "type": "nori_part_of_speech",
  268. "stoptags": [
  269. "NR" <1>
  270. ]
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. GET nori_sample/_analyze
  278. {
  279. "analyzer": "my_analyzer",
  280. "text": "여섯 용이" <2>
  281. }
  282. --------------------------------------------------
  283. // CONSOLE
  284. <1> Korean numerals should be removed (`NR`)
  285. <2> Six dragons
  286. Which responds with:
  287. [source,js]
  288. --------------------------------------------------
  289. {
  290. "tokens" : [ {
  291. "token" : "용",
  292. "start_offset" : 3,
  293. "end_offset" : 4,
  294. "type" : "word",
  295. "position" : 1
  296. }, {
  297. "token" : "이",
  298. "start_offset" : 4,
  299. "end_offset" : 5,
  300. "type" : "word",
  301. "position" : 2
  302. } ]
  303. }
  304. --------------------------------------------------
  305. // TESTRESPONSE
  306. [[analysis-nori-readingform]]
  307. ==== `nori_readingform` token filter
  308. The `nori_readingform` token filter rewrites tokens written in Hanja to their Hangul form.
  309. [source,js]
  310. --------------------------------------------------
  311. PUT nori_sample
  312. {
  313. "settings": {
  314. "index":{
  315. "analysis":{
  316. "analyzer" : {
  317. "my_analyzer" : {
  318. "tokenizer" : "nori_tokenizer",
  319. "filter" : ["nori_readingform"]
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. GET nori_sample/_analyze
  327. {
  328. "analyzer": "my_analyzer",
  329. "text": "鄕歌" <1>
  330. }
  331. --------------------------------------------------
  332. // CONSOLE
  333. <1> Hyangga
  334. Which responds with:
  335. [source,js]
  336. --------------------------------------------------
  337. {
  338. "tokens" : [ {
  339. "token" : "향가", <2>
  340. "start_offset" : 0,
  341. "end_offset" : 2,
  342. "type" : "word",
  343. "position" : 0
  344. }]
  345. }
  346. --------------------------------------------------
  347. // TESTRESPONSE
  348. <1> A token written in Hanja.
  349. <2> The Hanja form is replaced by the Hangul translation.