-
-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made a module to split Japanese words
- Loading branch information
Showing
5 changed files
with
212 additions
and
8 deletions.
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] == '大阪市大阪' |