-
-
Notifications
You must be signed in to change notification settings - Fork 716
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
Added a module to split Japanese words #3158
Open
miku0
wants to merge
2
commits into
osm-search:master
Choose a base branch
from
miku0:soft_phrase-final
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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,67 @@ | ||
# from nominatim.tokenizer.sanitizers.tag_japanese import convert_kanji_sequence_to_number | ||
|
||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
# This file is part of Nominatim. (https://nominatim.org) | ||
# | ||
# Copyright (C) 2023 by the Nominatim developer community. | ||
# For a full list of authors see the git log. | ||
""" | ||
This file divides Japanese addresses into three categories: | ||
prefecture, municipality, and other. | ||
The division is not strict but simple using these keywords. | ||
Based on this division, icu_tokenizer.py inserts | ||
a SOFT_PHRASE break between the divided words | ||
and penalizes the words with this SOFT_PHRASE | ||
to lower the search priority. | ||
""" | ||
import re | ||
from typing import List | ||
from nominatim.api.search import query as qmod | ||
|
||
def transliterate(text: str) -> str: | ||
""" | ||
This function performs a division on the given text using a regular expression. | ||
""" | ||
pattern_full = r''' | ||
(...??[都道府県]) # [group1] prefecture | ||
(.+?[市区町村]) # [group2] municipalities (city/wards/towns/villages) | ||
(.+) # [group3] other words | ||
''' | ||
pattern_1 = r''' | ||
(...??[都道府県]) # [group1] prefecture | ||
(.+) # [group3] other words | ||
''' | ||
pattern_2 = r''' | ||
(.+?[市区町村]) # [group2] municipalities (city/wards/towns/villages) | ||
(.+) # [group3] other words | ||
''' | ||
result_full = re.match(pattern_full, text, re.VERBOSE) | ||
result_1 = re.match(pattern_1, text, re.VERBOSE) | ||
result_2 = re.match(pattern_2, text, re.VERBOSE) | ||
if result_full is not None: | ||
joined_group = ''.join([ | ||
result_full.group(1), | ||
', ', | ||
result_full.group(2), | ||
', ', | ||
result_full.group(3) | ||
]) | ||
return joined_group | ||
if result_1 is not None: | ||
joined_group = ''.join([result_1.group(1),', ',result_1.group(2)]) | ||
return joined_group | ||
if result_2 is not None: | ||
joined_group = ''.join([result_2.group(1),', ',result_2.group(2)]) | ||
return joined_group | ||
return text | ||
|
||
def split_key_japanese_phrases( | ||
phrases: List[qmod.Phrase] | ||
) -> List[qmod.Phrase]: | ||
"""Split a Japanese address using japanese_tokenizer. | ||
""" | ||
splited_address = list(filter(lambda p: p.text, | ||
(qmod.Phrase(p.ptype, transliterate(p.text)) | ||
for p in phrases))) | ||
return splited_address |
This file contains 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 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
86 changes: 86 additions & 0 deletions
86
test/python/api/search/test_icu_japanese_query_analyzer.py
This file contains 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,86 @@ | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
# This file is part of Nominatim. (https://nominatim.org) | ||
# | ||
# Copyright (C) 2023 by the Nominatim developer community. | ||
# For a full list of authors see the git log. | ||
""" | ||
Tests for query analyzer for ICU tokenizer. | ||
""" | ||
from pathlib import Path | ||
|
||
import pytest | ||
import pytest_asyncio | ||
|
||
from nominatim.api import NominatimAPIAsync | ||
from nominatim.api.search.query import Phrase, PhraseType, BreakType | ||
import nominatim.api.search.icu_tokenizer as tok | ||
|
||
async def add_word(conn, word_id, word_token, wtype, word, info = None): | ||
t = conn.t.meta.tables['word'] | ||
await conn.execute(t.insert(), {'word_id': word_id, | ||
'word_token': word_token, | ||
'type': wtype, | ||
'word': word, | ||
'info': info}) | ||
|
||
|
||
def make_phrase(query): | ||
return [Phrase(PhraseType.NONE, s) for s in query.split(',')] | ||
@pytest_asyncio.fixture | ||
async def conn(table_factory): | ||
""" Create an asynchronous SQLAlchemy engine for the test DB. | ||
""" | ||
table_factory('nominatim_properties', | ||
definition='property TEXT, value TEXT', | ||
content=(('tokenizer_import_normalisation', ':: lower();'), | ||
('tokenizer_import_transliteration', "'1' > '/1/'; 'ä' > 'ä '"))) | ||
table_factory('word', | ||
definition='word_id INT, word_token TEXT, type TEXT, word TEXT, info JSONB') | ||
|
||
api = NominatimAPIAsync(Path('/invalid'), {}) | ||
async with api.begin() as conn: | ||
yield conn | ||
await api.close() | ||
@pytest.mark.asyncio | ||
async def test_soft_phrase(conn): | ||
ana = await tok.create_query_analyzer(conn) | ||
|
||
await add_word(conn, 100, 'da', 'w', None) | ||
await add_word(conn, 101, 'ban', 'w', None) | ||
await add_word(conn, 102, 'fu', 'w', None) | ||
await add_word(conn, 103, 'shi', 'w', None) | ||
|
||
await add_word(conn, 1, 'da ban fu', 'W', '大阪府') | ||
await add_word(conn, 2, 'da ban shi', 'W', '大阪市') | ||
await add_word(conn, 3, 'da ban', 'W', '大阪') | ||
query = await ana.analyze_query(make_phrase('大阪府大阪市大阪')) | ||
assert query.nodes[0].btype == BreakType.START | ||
assert query.nodes[1].btype == BreakType.SOFT_PHRASE | ||
assert query.nodes[2].btype == BreakType.SOFT_PHRASE | ||
assert query.nodes[3].btype == BreakType.END | ||
|
||
query2 = await ana.analyze_query(make_phrase('大阪府大阪')) | ||
assert query2.nodes[1].btype == BreakType.SOFT_PHRASE | ||
|
||
query3 = await ana.analyze_query(make_phrase('大阪市大阪')) | ||
assert query3.nodes[1].btype == BreakType.SOFT_PHRASE | ||
|
||
@pytest.mark.asyncio | ||
async def test_penalty_soft_phrase(conn): | ||
ana = await tok.create_query_analyzer(conn) | ||
|
||
await add_word(conn, 104, 'da', 'w', 'da') | ||
await add_word(conn, 105, 'ban', 'w', 'ban') | ||
await add_word(conn, 107, 'shi', 'w', 'shi') | ||
|
||
await add_word(conn, 2, 'da ban shi', 'W', '大阪市') | ||
await add_word(conn, 3, 'da ban', 'W', '大阪') | ||
await add_word(conn, 4, 'da ban shi da ban', 'W', '大阪市大阪') | ||
|
||
query = await ana.analyze_query(make_phrase('da ban shi da ban')) | ||
|
||
torder = [(tl.tokens[0].penalty, tl.tokens[0].lookup_word) for tl in query.nodes[0].starting] | ||
torder.sort() | ||
|
||
assert torder[-1][-1] == '大阪市大阪' |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add documentation for this new type, just like it is done in the lines above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for your help and comments.
I added the documentation.