Skip to content

Commit

Permalink
Fix incorrect match on multiple digits of addr (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
keichan34 authored Feb 21, 2024
1 parent 0cdc0e1 commit 411cd01
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test:addresses": "jest test/addresses.test.ts",
"test:advanced": "jest test/advanced --runInBand",
"test:util": "jest test/util.test.ts",
"test:node": "curl -sL https://github.com/geolonia/japanese-addresses/archive/refs/heads/master.tar.gz | tar xvfz - -C ./test > nul 2>&1 && jest test/fs.test.ts",
"test:node": "curl -sL https://github.com/geolonia/japanese-addresses/archive/refs/heads/master.tar.gz | tar xvfz - -C ./test > /dev/null 2>&1 && jest test/fs.test.ts",
"test:node-win": "curl -sL https://github.com/geolonia/japanese-addresses/archive/refs/heads/master.tar.gz -o master.tar.gz && 7z x master.tar.gz -y -o./ && 7z x ./master.tar -y -o./test && del master.tar.gz master.tar && jest test/fs.test.ts",
"test:generate-test-data": "npx ts-node -O '{\"module\":\"commonjs\"}' test/build-test-data.ts > test/addresses.csv",
"lint": "eslint \"src/**/*.ts\" \"test/**/*.test.ts\" --fix",
Expand Down
9 changes: 6 additions & 3 deletions src/lib/cacheRegexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ const cachedTowns: { [key: string]: TownList } = {}
const cachedGaikuListItem: { [key: string]: GaikuListItem[] } = {}
const cachedResidentials: { [key: string]: ResidentialList } = {}
const cachedAddrs: { [key: string]: AddrList } = {} // TODO: use LRU
let cachedSameNamedPrefectureCityRegexPatterns: [string, string][] | undefined =
undefined
let cachedSameNamedPrefectureCityRegexPatterns:
| [string, string][]
| undefined = undefined

export const getPrefectures = async () => {
if (typeof cachedPrefectures !== 'undefined') {
Expand Down Expand Up @@ -208,7 +209,9 @@ export const getAddrs = async (pref: string, city: string, town: string) => {
addrs = []
}

addrs.sort((res1, res2) => res1.addr.length - res2.addr.length)
// 文字数が多い順に並び替えします。
// 長い順に並び替えしないと、短い住所がマッチしてしまう。
addrs.sort((res1, res2) => res2.addr.length - res1.addr.length)
return (cachedAddrs[cacheKey] = addrs)
}

Expand Down
77 changes: 77 additions & 0 deletions test/advanced/interface_v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { normalize, config } from '../../src/main-node'
import { NormalizeResult } from '../../src/normalize'

config.interfaceVersion = 2
config.transformRequest = async (url, query) => {
Expand Down Expand Up @@ -48,6 +49,11 @@ config.transformRequest = async (url, query) => {
{ addr: '1-1', lat: '30.1', lng: '135.1' },
{ addr: '1-2', lat: '30.2', lng: '135.2' },
{ addr: '2-3', lat: null, lng: null },
{ addr: '3-1', lat: '30.25', lng: '135.25' },
{ addr: '3-19', lat: '30.3', lng: '135.3' },
{ addr: '13-1', lat: '30.4', lng: '135.4' },
{ addr: '13-14', lat: '30.45', lng: '135.45' },
{ addr: '13-19', lat: '30.5', lng: '135.5' },
]
} else {
return []
Expand Down Expand Up @@ -107,6 +113,77 @@ test('リクエスト変形テスト - レベル8 で緯度経度が null の時
})
})

const expectedCommon = { pref: 'A県', city: 'X市', town: 'あああ', level: 8 }
const cases: { input: string; expected: NormalizeResult }[] = [
{
input: 'A県 X市 あああ1-1 こんばんはビル',
expected: {
...expectedCommon,
addr: '1-1',
other: 'こんばんはビル',
lng: 135.1,
lat: 30.1,
},
},
{
input: 'A県 X市 あああ3-1 こんばんはビル',
expected: {
...expectedCommon,
addr: '3-1',
other: 'こんばんはビル',
lng: 135.25,
lat: 30.25,
},
},
{
input: 'A県 X市 あああ3-19 こんばんはビル',
expected: {
...expectedCommon,
addr: '3-19',
other: 'こんばんはビル',
lng: 135.3,
lat: 30.3,
},
},
{
input: 'A県 X市 あああ13-1 こんばんはビル',
expected: {
...expectedCommon,
addr: '13-1',
other: 'こんばんはビル',
lng: 135.4,
lat: 30.4,
},
},
{
input: 'A県 X市 あああ13-14 こんばんはビル',
expected: {
...expectedCommon,
addr: '13-14',
other: 'こんばんはビル',
lng: 135.45,
lat: 30.45,
},
},
{
input: 'A県 X市 あああ13-19 こんばんはビル',
expected: {
...expectedCommon,
addr: '13-19',
other: 'こんばんはビル',
lng: 135.5,
lat: 30.5,
},
},
]

for (const { input, expected } of cases) {
test(`リクエスト変形テスト - ${input}`, async () => {
const res = await normalize(input, { level: 8 })
expect(res).toStrictEqual(expected)
})
}

test('リクエスト変形テスト - 全く正規化できないケース', async () => {
const res = await normalize('こんにちはこんにちは', { level: 8 })
expect(res).toStrictEqual({
Expand Down

0 comments on commit 411cd01

Please sign in to comment.