analysis-nori.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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,js]
  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. // TESTRESPONSE
  122. <1> This is a compound token that spans two positions (`mixed` mode).
  123. --
  124. `user_dictionary_rules`::
  125. +
  126. --
  127. You can also inline the rules directly in the tokenizer definition using
  128. the `user_dictionary_rules` option:
  129. [source,js]
  130. --------------------------------------------------
  131. PUT nori_sample
  132. {
  133. "settings": {
  134. "index": {
  135. "analysis": {
  136. "tokenizer": {
  137. "nori_user_dict": {
  138. "type": "nori_tokenizer",
  139. "decompound_mode": "mixed",
  140. "user_dictionary_rules": ["c++", "C샤프", "세종", "세종시 세종 시"]
  141. }
  142. },
  143. "analyzer": {
  144. "my_analyzer": {
  145. "type": "custom",
  146. "tokenizer": "nori_user_dict"
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. --------------------------------------------------
  154. // CONSOLE
  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,js]
  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. // CONSOLE
  170. <1> A tree with deep roots
  171. Which responds with:
  172. [source,js]
  173. --------------------------------------------------
  174. {
  175. "detail": {
  176. "custom_analyzer": true,
  177. "charfilters": [],
  178. "tokenizer": {
  179. "name": "nori_tokenizer",
  180. "tokens": [
  181. {
  182. "token": "뿌리",
  183. "start_offset": 0,
  184. "end_offset": 2,
  185. "type": "word",
  186. "position": 0,
  187. "leftPOS": "NNG(General Noun)",
  188. "morphemes": null,
  189. "posType": "MORPHEME",
  190. "reading": null,
  191. "rightPOS": "NNG(General Noun)"
  192. },
  193. {
  194. "token": "가",
  195. "start_offset": 2,
  196. "end_offset": 3,
  197. "type": "word",
  198. "position": 1,
  199. "leftPOS": "J(Ending Particle)",
  200. "morphemes": null,
  201. "posType": "MORPHEME",
  202. "reading": null,
  203. "rightPOS": "J(Ending Particle)"
  204. },
  205. {
  206. "token": "깊",
  207. "start_offset": 4,
  208. "end_offset": 5,
  209. "type": "word",
  210. "position": 2,
  211. "leftPOS": "VA(Adjective)",
  212. "morphemes": null,
  213. "posType": "MORPHEME",
  214. "reading": null,
  215. "rightPOS": "VA(Adjective)"
  216. },
  217. {
  218. "token": "은",
  219. "start_offset": 5,
  220. "end_offset": 6,
  221. "type": "word",
  222. "position": 3,
  223. "leftPOS": "E(Verbal endings)",
  224. "morphemes": null,
  225. "posType": "MORPHEME",
  226. "reading": null,
  227. "rightPOS": "E(Verbal endings)"
  228. },
  229. {
  230. "token": "나무",
  231. "start_offset": 7,
  232. "end_offset": 9,
  233. "type": "word",
  234. "position": 4,
  235. "leftPOS": "NNG(General Noun)",
  236. "morphemes": null,
  237. "posType": "MORPHEME",
  238. "reading": null,
  239. "rightPOS": "NNG(General Noun)"
  240. },
  241. {
  242. "token": "는",
  243. "start_offset": 9,
  244. "end_offset": 10,
  245. "type": "word",
  246. "position": 5,
  247. "leftPOS": "J(Ending Particle)",
  248. "morphemes": null,
  249. "posType": "MORPHEME",
  250. "reading": null,
  251. "rightPOS": "J(Ending Particle)"
  252. }
  253. ]
  254. },
  255. "tokenfilters": []
  256. }
  257. }
  258. --------------------------------------------------
  259. // TESTRESPONSE
  260. [[analysis-nori-speech]]
  261. ==== `nori_part_of_speech` token filter
  262. The `nori_part_of_speech` token filter removes tokens that match a set of
  263. part-of-speech tags. The list of supported tags and their meanings can be found here:
  264. {lucene-core-javadoc}/../analyzers-nori/org/apache/lucene/analysis/ko/POS.Tag.html[Part of speech tags]
  265. It accepts the following setting:
  266. `stoptags`::
  267. An array of part-of-speech tags that should be removed.
  268. and defaults to:
  269. [source,js]
  270. --------------------------------------------------
  271. "stoptags": [
  272. "E",
  273. "IC",
  274. "J",
  275. "MAG", "MAJ", "MM",
  276. "SP", "SSC", "SSO", "SC", "SE",
  277. "XPN", "XSA", "XSN", "XSV",
  278. "UNA", "NA", "VSV"
  279. ]
  280. --------------------------------------------------
  281. // NOTCONSOLE
  282. For example:
  283. [source,js]
  284. --------------------------------------------------
  285. PUT nori_sample
  286. {
  287. "settings": {
  288. "index": {
  289. "analysis": {
  290. "analyzer": {
  291. "my_analyzer": {
  292. "tokenizer": "nori_tokenizer",
  293. "filter": [
  294. "my_posfilter"
  295. ]
  296. }
  297. },
  298. "filter": {
  299. "my_posfilter": {
  300. "type": "nori_part_of_speech",
  301. "stoptags": [
  302. "NR" <1>
  303. ]
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. GET nori_sample/_analyze
  311. {
  312. "analyzer": "my_analyzer",
  313. "text": "여섯 용이" <2>
  314. }
  315. --------------------------------------------------
  316. // CONSOLE
  317. <1> Korean numerals should be removed (`NR`)
  318. <2> Six dragons
  319. Which responds with:
  320. [source,js]
  321. --------------------------------------------------
  322. {
  323. "tokens" : [ {
  324. "token" : "용",
  325. "start_offset" : 3,
  326. "end_offset" : 4,
  327. "type" : "word",
  328. "position" : 1
  329. }, {
  330. "token" : "이",
  331. "start_offset" : 4,
  332. "end_offset" : 5,
  333. "type" : "word",
  334. "position" : 2
  335. } ]
  336. }
  337. --------------------------------------------------
  338. // TESTRESPONSE
  339. [[analysis-nori-readingform]]
  340. ==== `nori_readingform` token filter
  341. The `nori_readingform` token filter rewrites tokens written in Hanja to their Hangul form.
  342. [source,js]
  343. --------------------------------------------------
  344. PUT nori_sample
  345. {
  346. "settings": {
  347. "index":{
  348. "analysis":{
  349. "analyzer" : {
  350. "my_analyzer" : {
  351. "tokenizer" : "nori_tokenizer",
  352. "filter" : ["nori_readingform"]
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. GET nori_sample/_analyze
  360. {
  361. "analyzer": "my_analyzer",
  362. "text": "鄕歌" <1>
  363. }
  364. --------------------------------------------------
  365. // CONSOLE
  366. <1> A token written in Hanja: Hyangga
  367. Which responds with:
  368. [source,js]
  369. --------------------------------------------------
  370. {
  371. "tokens" : [ {
  372. "token" : "향가", <1>
  373. "start_offset" : 0,
  374. "end_offset" : 2,
  375. "type" : "word",
  376. "position" : 0
  377. }]
  378. }
  379. --------------------------------------------------
  380. // TESTRESPONSE
  381. <1> The Hanja form is replaced by the Hangul translation.