-
Notifications
You must be signed in to change notification settings - Fork 25.3k
add nori_number token filter in analysis-nori #53583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6221c5
add nori_number token filter
danmuzi d6dbe51
add discard_punctuation option in nori_tokenizer
danmuzi 9bd0ebe
add description about using discard_punctuation in nori_number
danmuzi 6ed33a1
add note in asciidoc and test cases for discard_punctuation
danmuzi 1a4367e
fix wrong indentation in nori_number test
danmuzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,10 @@ It can be set to: | |
가곡역 => 가곡역, 가곡, 역 | ||
-- | ||
|
||
`discard_punctuation`:: | ||
|
||
Whether punctuation should be discarded from the output. Defaults to `true`. | ||
|
||
`user_dictionary`:: | ||
+ | ||
-- | ||
|
@@ -99,6 +103,7 @@ PUT nori_sample | |
"nori_user_dict": { | ||
"type": "nori_tokenizer", | ||
"decompound_mode": "mixed", | ||
"discard_punctuation": "false", | ||
"user_dictionary": "userdict_ko.txt" | ||
} | ||
}, | ||
|
@@ -434,3 +439,107 @@ Which responds with: | |
-------------------------------------------------- | ||
|
||
<1> The Hanja form is replaced by the Hangul translation. | ||
|
||
|
||
[[analysis-nori-number]] | ||
==== `nori_number` token filter | ||
|
||
The `nori_number` token filter normalizes Korean numbers | ||
to regular Arabic decimal numbers in half-width characters. | ||
|
||
Korean numbers are often written using a combination of Hangul and Arabic numbers with various kinds punctuation. | ||
For example, 3.2천 means 3200. | ||
This filter does this kind of normalization and allows a search for 3200 to match 3.2천 in text, | ||
but can also be used to make range facets based on the normalized numbers and so on. | ||
|
||
[NOTE] | ||
==== | ||
Notice that this analyzer uses a token composition scheme and relies on punctuation tokens | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
being found in the token stream. | ||
Please make sure your `nori_tokenizer` has `discard_punctuation` set to false. | ||
In case punctuation characters, such as U+FF0E(.), is removed from the token stream, | ||
this filter would find input tokens 3 and 2천 and give outputs 3 and 2000 instead of 3200, | ||
which is likely not the intended result. | ||
|
||
If you want to remove punctuation characters from your index that are not part of normalized numbers, | ||
add a `stop` token filter with the punctuation you wish to remove after `nori_number` in your analyzer chain. | ||
==== | ||
Below are some examples of normalizations this filter supports. | ||
The input is untokenized text and the result is the single term attribute emitted for the input. | ||
|
||
- 영영칠 -> 7 | ||
- 일영영영 -> 1000 | ||
- 삼천2백2십삼 -> 3223 | ||
- 조육백만오천일 -> 1000006005001 | ||
- 3.2천 -> 3200 | ||
- 1.2만345.67 -> 12345.67 | ||
- 4,647.100 -> 4647.1 | ||
- 15,7 -> 157 (be aware of this weakness) | ||
|
||
For example: | ||
|
||
[source,console] | ||
-------------------------------------------------- | ||
PUT nori_sample | ||
{ | ||
"settings": { | ||
"index": { | ||
"analysis": { | ||
"analyzer": { | ||
"my_analyzer": { | ||
"tokenizer": "tokenizer_discard_puncuation_false", | ||
"filter": [ | ||
"part_of_speech_stop_sp", "nori_number" | ||
] | ||
} | ||
}, | ||
"tokenizer": { | ||
"tokenizer_discard_puncuation_false": { | ||
"type": "nori_tokenizer", | ||
"discard_punctuation": "false" | ||
} | ||
}, | ||
"filter": { | ||
"part_of_speech_stop_sp": { | ||
"type": "nori_part_of_speech", | ||
"stoptags": ["SP"] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
GET nori_sample/_analyze | ||
{ | ||
"analyzer": "my_analyzer", | ||
"text": "십만이천오백과 3.2천" | ||
} | ||
-------------------------------------------------- | ||
|
||
Which results in: | ||
|
||
[source,console-result] | ||
-------------------------------------------------- | ||
{ | ||
"tokens" : [{ | ||
"token" : "102500", | ||
"start_offset" : 0, | ||
"end_offset" : 6, | ||
"type" : "word", | ||
"position" : 0 | ||
}, { | ||
"token" : "과", | ||
"start_offset" : 6, | ||
"end_offset" : 7, | ||
"type" : "word", | ||
"position" : 1 | ||
}, { | ||
"token" : "3200", | ||
"start_offset" : 8, | ||
"end_offset" : 12, | ||
"type" : "word", | ||
"position" : 2 | ||
}] | ||
} | ||
-------------------------------------------------- |
38 changes: 38 additions & 0 deletions
38
...analysis-nori/src/main/java/org/elasticsearch/index/analysis/NoriNumberFilterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.analysis; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.ko.KoreanNumberFilter; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.index.IndexSettings; | ||
|
||
public class NoriNumberFilterFactory extends AbstractTokenFilterFactory { | ||
|
||
public NoriNumberFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) { | ||
super(indexSettings, name, settings); | ||
} | ||
|
||
@Override | ||
public TokenStream create(TokenStream tokenStream) { | ||
return new KoreanNumberFilter(tokenStream); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.