analysis-nori.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. `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. As a demonstration of how the user dictionary can be used, save the following
  52. dictionary to `$ES_HOME/config/userdict_ko.txt`:
  53. [source,txt]
  54. -----------------------
  55. c++ <1>
  56. C샤프
  57. 세종
  58. 세종시 세종 시 <2>
  59. -----------------------
  60. <1> A simple noun
  61. <2> A compound noun (`세종시`) followed by its decomposition: `세종` and `시`.
  62. Then create an analyzer as follows:
  63. [source,js]
  64. --------------------------------------------------
  65. PUT nori_sample
  66. {
  67. "settings": {
  68. "index": {
  69. "analysis": {
  70. "tokenizer": {
  71. "nori_user_dict": {
  72. "type": "nori_tokenizer",
  73. "decompound_mode": "mixed",
  74. "user_dictionary": "userdict_ko.txt"
  75. }
  76. },
  77. "analyzer": {
  78. "my_analyzer": {
  79. "type": "custom",
  80. "tokenizer": "nori_user_dict"
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. GET nori_sample/_analyze
  88. {
  89. "analyzer": "my_analyzer",
  90. "text": "세종시" <1>
  91. }
  92. --------------------------------------------------
  93. // CONSOLE
  94. <1> Sejong city
  95. The above `analyze` request returns the following:
  96. [source,console-result]
  97. --------------------------------------------------
  98. {
  99. "tokens" : [ {
  100. "token" : "세종시",
  101. "start_offset" : 0,
  102. "end_offset" : 3,
  103. "type" : "word",
  104. "position" : 0,
  105. "positionLength" : 2 <1>
  106. }, {
  107. "token" : "세종",
  108. "start_offset" : 0,
  109. "end_offset" : 2,
  110. "type" : "word",
  111. "position" : 0
  112. }, {
  113. "token" : "시",
  114. "start_offset" : 2,
  115. "end_offset" : 3,
  116. "type" : "word",
  117. "position" : 1
  118. }]
  119. }
  120. --------------------------------------------------
  121. <1> This is a compound token that spans two positions (`mixed` mode).
  122. --
  123. `user_dictionary_rules`::
  124. +
  125. --
  126. You can also inline the rules directly in the tokenizer definition using
  127. the `user_dictionary_rules` option:
  128. [source,js]
  129. --------------------------------------------------
  130. PUT nori_sample
  131. {
  132. "settings": {
  133. "index": {
  134. "analysis": {
  135. "tokenizer": {
  136. "nori_user_dict": {
  137. "type": "nori_tokenizer",
  138. "decompound_mode": "mixed",
  139. "user_dictionary_rules": ["c++", "C샤프", "세종", "세종시 세종 시"]
  140. }
  141. },
  142. "analyzer": {
  143. "my_analyzer": {
  144. "type": "custom",
  145. "tokenizer": "nori_user_dict"
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. --------------------------------------------------
  153. // CONSOLE
  154. --
  155. The `nori_tokenizer` sets a number of additional attributes per token that are used by token filters
  156. to modify the stream.
  157. You can view all these additional attributes with the following request:
  158. [source,js]
  159. --------------------------------------------------
  160. GET _analyze
  161. {
  162. "tokenizer": "nori_tokenizer",
  163. "text": "뿌리가 깊은 나무는", <1>
  164. "attributes" : ["posType", "leftPOS", "rightPOS", "morphemes", "reading"],
  165. "explain": true
  166. }
  167. --------------------------------------------------
  168. // CONSOLE
  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,js]
  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. // CONSOLE
  315. <1> Korean numerals should be removed (`NR`)
  316. <2> Six dragons
  317. Which responds with:
  318. [source,console-result]
  319. --------------------------------------------------
  320. {
  321. "tokens" : [ {
  322. "token" : "용",
  323. "start_offset" : 3,
  324. "end_offset" : 4,
  325. "type" : "word",
  326. "position" : 1
  327. }, {
  328. "token" : "이",
  329. "start_offset" : 4,
  330. "end_offset" : 5,
  331. "type" : "word",
  332. "position" : 2
  333. } ]
  334. }
  335. --------------------------------------------------
  336. [[analysis-nori-readingform]]
  337. ==== `nori_readingform` token filter
  338. The `nori_readingform` token filter rewrites tokens written in Hanja to their Hangul form.
  339. [source,js]
  340. --------------------------------------------------
  341. PUT nori_sample
  342. {
  343. "settings": {
  344. "index":{
  345. "analysis":{
  346. "analyzer" : {
  347. "my_analyzer" : {
  348. "tokenizer" : "nori_tokenizer",
  349. "filter" : ["nori_readingform"]
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. GET nori_sample/_analyze
  357. {
  358. "analyzer": "my_analyzer",
  359. "text": "鄕歌" <1>
  360. }
  361. --------------------------------------------------
  362. // CONSOLE
  363. <1> A token written in Hanja: Hyangga
  364. Which responds with:
  365. [source,console-result]
  366. --------------------------------------------------
  367. {
  368. "tokens" : [ {
  369. "token" : "향가", <1>
  370. "start_offset" : 0,
  371. "end_offset" : 2,
  372. "type" : "word",
  373. "position" : 0
  374. }]
  375. }
  376. --------------------------------------------------
  377. <1> The Hanja form is replaced by the Hangul translation.