1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2025 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 This file divides Japanese addresses into three categories:
 
   9 prefecture, municipality, and other.
 
  10 The division is not strict but simple using these keywords.
 
  12 from typing import List
 
  15 from .config import QueryConfig
 
  16 from .base import QueryProcessingFunc
 
  17 from ..search.query import Phrase
 
  21                 (...??[都都道府県縣])            # [group1] prefecture
 
  22                 (.+?[市区區町村])              # [group2] municipalities (city/wards/towns/villages)
 
  23                 (.+)                         # [group3] other words
 
  26                 (...??[都都道府県縣])            # [group1] prefecture
 
  27                 (.+)                         # [group3] other words
 
  30                 (.+?[市区區町村])              # [group2] municipalities (city/wards/towns/villages)
 
  31                 (.+)                         # [group3] other words
 
  36 class _JapanesePreprocessing:
 
  38     def __init__(self, config: QueryConfig) -> None:
 
  41     def split_phrase(self, phrase: Phrase) -> Phrase:
 
  43         This function performs a division on the given text using a regular expression.
 
  45         for pattern in MATCH_PATTERNS:
 
  46             result = re.match(pattern, phrase.text, re.VERBOSE)
 
  47             if result is not None:
 
  48                 return Phrase(phrase.ptype, ':'.join(result.groups()))
 
  52     def __call__(self, phrases: List[Phrase]) -> List[Phrase]:
 
  53         """Split a Japanese address using japanese_tokenizer.
 
  55         return [self.split_phrase(p) for p in phrases]
 
  58 def create(config: QueryConfig) -> QueryProcessingFunc:
 
  59     """ Create a function of japanese preprocessing.
 
  61     return _JapanesePreprocessing(config)