Skip to content
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

旧漢字・異体字リストをオブジェクトで管理する #236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions src/lib/dict.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
// JIS 第2水準 => 第1水準 及び 旧字体 => 新字体
const JIS_OLD_KANJI = '亞,圍,壹,榮,驛,應,櫻,假,會,懷,覺,樂,陷,歡,氣,戲,據,挾,區,徑,溪,輕,藝,儉,圈,權,嚴,恆,國,齋,雜,蠶,殘,兒,實,釋,從,縱,敍,燒,條,剩,壤,釀,眞,盡,醉,髓,聲,竊,淺,錢,禪,爭,插,騷,屬,對,滯,擇,單,斷,癡,鑄,敕,鐵,傳,黨,鬪,屆,腦,廢,發,蠻,拂,邊,瓣,寶,沒,滿,藥,餘,樣,亂,兩,禮,靈,爐,灣,惡,醫,飮,營,圓,歐,奧,價,繪,擴,學,罐,勸,觀,歸,犧,擧,狹,驅,莖,經,繼,缺,劍,檢,顯,廣,鑛,碎,劑,參,慘,絲,辭,舍,壽,澁,肅,將,證,乘,疊,孃,觸,寢,圖,穗,樞,齊,攝,戰,潛,雙,莊,裝,藏,續,體,臺,澤,膽,彈,蟲,廳,鎭,點,燈,盜,獨,貳,霸,賣,髮,祕,佛,變,辯,豐,飜,默,與,譽,謠,覽,獵,勵,齡,勞,壓,爲,隱,衞,鹽,毆,穩,畫,壞,殼,嶽,卷,關,顏,僞,舊,峽,曉,勳,惠,螢,鷄,縣,險,獻,驗,效,號,濟,册,棧,贊,齒,濕,寫,收,獸,處,稱,奬,淨,繩,讓,囑,愼,粹,隨,數,靜,專,踐,纖,壯,搜,總,臟,墮,帶,瀧,擔,團,遲,晝,聽,遞,轉,當,稻,讀,惱,拜,麥,拔,濱,竝,辨,舖,襃,萬,譯,豫,搖,來,龍,壘,隸,戀,樓,鰺,鶯,蠣,攪,竈,灌,諫,頸,礦,蘂,靱,賤,壺,礪,檮,濤,邇,蠅,檜,儘,藪,籠,彌,麩'.split(
/,/,
)
const JIS_NEW_KANJI = '亜,囲,壱,栄,駅,応,桜,仮,会,懐,覚,楽,陥,歓,気,戯,拠,挟,区,径,渓,軽,芸,倹,圏,権,厳,恒,国,斎,雑,蚕,残,児,実,釈,従,縦,叙,焼,条,剰,壌,醸,真,尽,酔,髄,声,窃,浅,銭,禅,争,挿,騒,属,対,滞,択,単,断,痴,鋳,勅,鉄,伝,党,闘,届,脳,廃,発,蛮,払,辺,弁,宝,没,満,薬,余,様,乱,両,礼,霊,炉,湾,悪,医,飲,営,円,欧,奥,価,絵,拡,学,缶,勧,観,帰,犠,挙,狭,駆,茎,経,継,欠,剣,検,顕,広,鉱,砕,剤,参,惨,糸,辞,舎,寿,渋,粛,将,証,乗,畳,嬢,触,寝,図,穂,枢,斉,摂,戦,潜,双,荘,装,蔵,続,体,台,沢,胆,弾,虫,庁,鎮,点,灯,盗,独,弐,覇,売,髪,秘,仏,変,弁,豊,翻,黙,与,誉,謡,覧,猟,励,齢,労,圧,為,隠,衛,塩,殴,穏,画,壊,殻,岳,巻,関,顔,偽,旧,峡,暁,勲,恵,蛍,鶏,県,険,献,験,効,号,済,冊,桟,賛,歯,湿,写,収,獣,処,称,奨,浄,縄,譲,嘱,慎,粋,随,数,静,専,践,繊,壮,捜,総,臓,堕,帯,滝,担,団,遅,昼,聴,逓,転,当,稲,読,悩,拝,麦,抜,浜,並,弁,舗,褒,万,訳,予,揺,来,竜,塁,隷,恋,楼,鯵,鴬,蛎,撹,竃,潅,諌,頚,砿,蕊,靭,賎,壷,砺,梼,涛,迩,蝿,桧,侭,薮,篭,弥,麸'.split(
/,/,
)

const JIS_KANJI_REGEX_PATTERNS = JIS_OLD_KANJI.map((old, i) => {
const pattern = `${old}|${JIS_NEW_KANJI[i]}`
return [pattern, old, JIS_NEW_KANJI[i]]
})

export const jisKanji = (str: string) => {
let _str = str

for (let i = 0; i < JIS_KANJI_REGEX_PATTERNS.length; i++) {
const [pattern, oldKanji, newKanji] = JIS_KANJI_REGEX_PATTERNS[i]
_str = _str.replace(new RegExp(pattern, 'g'), `(${oldKanji}|${newKanji})`)
}

return _str
}
import { convert } from './dictionaries/convert'

export const toRegexPattern = (string: string) => {
let _str = string
Expand Down Expand Up @@ -55,7 +33,7 @@ export const toRegexPattern = (string: string) => {
.replace(/莵|菟/g, '(莵|菟)')
.replace(/市|巿/g, '(市|巿)')

_str = jisKanji(_str)
_str = convert(_str)

return _str
}
16 changes: 16 additions & 0 deletions src/lib/dictionaries/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { dictionary } from './dictionary'

type PatternMap = { [key: string]: string }
const patternMap = dictionary.reduce((acc: PatternMap, dictionary) => {
const pattern = `(${dictionary.src}|${dictionary.dst})`
// { 亞: '(亞|亜)', 亜: '(亞|亜)', 圍: '(圍|囲)', 囲: '(圍|囲)', ...}
return { ...acc, [dictionary.src]: pattern, [dictionary.dst]: pattern }
}, {})

const regexp = new RegExp(
Array.from(new Set(Object.values(patternMap))).join('|'),
'g',
)

export const convert = (regexText: string) =>
regexText.replace(regexp, (match) => patternMap[match])
9 changes: 9 additions & 0 deletions src/lib/dictionaries/dictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { jisDai2Dictionary } from './jisDai2'

export type Dictionary = { src: string; dst: string }

export const dictionary = [
jisDai2Dictionary,
// Add more dictionary here
// exmapleDictionary,
].flat()
294 changes: 294 additions & 0 deletions src/lib/dictionaries/jisDai2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
import { Dictionary } from './dictionary'

// JIS 第2水準 => 第1水準 及び 旧字体 => 新字体
export const jisDai2Dictionary: Dictionary[] = [
{ src: '亞', dst: '亜' },
{ src: '圍', dst: '囲' },
{ src: '壹', dst: '壱' },
{ src: '榮', dst: '栄' },
{ src: '驛', dst: '駅' },
{ src: '應', dst: '応' },
{ src: '櫻', dst: '桜' },
{ src: '假', dst: '仮' },
{ src: '會', dst: '会' },
{ src: '懷', dst: '懐' },
{ src: '覺', dst: '覚' },
{ src: '樂', dst: '楽' },
{ src: '陷', dst: '陥' },
{ src: '歡', dst: '歓' },
{ src: '氣', dst: '気' },
{ src: '戲', dst: '戯' },
{ src: '據', dst: '拠' },
{ src: '挾', dst: '挟' },
{ src: '區', dst: '区' },
{ src: '徑', dst: '径' },
{ src: '溪', dst: '渓' },
{ src: '輕', dst: '軽' },
{ src: '藝', dst: '芸' },
{ src: '儉', dst: '倹' },
{ src: '圈', dst: '圏' },
{ src: '權', dst: '権' },
{ src: '嚴', dst: '厳' },
{ src: '恆', dst: '恒' },
{ src: '國', dst: '国' },
{ src: '齋', dst: '斎' },
{ src: '雜', dst: '雑' },
{ src: '蠶', dst: '蚕' },
{ src: '殘', dst: '残' },
{ src: '兒', dst: '児' },
{ src: '實', dst: '実' },
{ src: '釋', dst: '釈' },
{ src: '從', dst: '従' },
{ src: '縱', dst: '縦' },
{ src: '敍', dst: '叙' },
{ src: '燒', dst: '焼' },
{ src: '條', dst: '条' },
{ src: '剩', dst: '剰' },
{ src: '壤', dst: '壌' },
{ src: '釀', dst: '醸' },
{ src: '眞', dst: '真' },
{ src: '盡', dst: '尽' },
{ src: '醉', dst: '酔' },
{ src: '髓', dst: '髄' },
{ src: '聲', dst: '声' },
{ src: '竊', dst: '窃' },
{ src: '淺', dst: '浅' },
{ src: '錢', dst: '銭' },
{ src: '禪', dst: '禅' },
{ src: '爭', dst: '争' },
{ src: '插', dst: '挿' },
{ src: '騷', dst: '騒' },
{ src: '屬', dst: '属' },
{ src: '對', dst: '対' },
{ src: '滯', dst: '滞' },
{ src: '擇', dst: '択' },
{ src: '單', dst: '単' },
{ src: '斷', dst: '断' },
{ src: '癡', dst: '痴' },
{ src: '鑄', dst: '鋳' },
{ src: '敕', dst: '勅' },
{ src: '鐵', dst: '鉄' },
{ src: '傳', dst: '伝' },
{ src: '黨', dst: '党' },
{ src: '鬪', dst: '闘' },
{ src: '屆', dst: '届' },
{ src: '腦', dst: '脳' },
{ src: '廢', dst: '廃' },
{ src: '發', dst: '発' },
{ src: '蠻', dst: '蛮' },
{ src: '拂', dst: '払' },
{ src: '邊', dst: '辺' },
{ src: '瓣', dst: '弁' },
{ src: '寶', dst: '宝' },
{ src: '沒', dst: '没' },
{ src: '滿', dst: '満' },
{ src: '藥', dst: '薬' },
{ src: '餘', dst: '余' },
{ src: '樣', dst: '様' },
{ src: '亂', dst: '乱' },
{ src: '兩', dst: '両' },
{ src: '禮', dst: '礼' },
{ src: '靈', dst: '霊' },
{ src: '爐', dst: '炉' },
{ src: '灣', dst: '湾' },
{ src: '惡', dst: '悪' },
{ src: '醫', dst: '医' },
{ src: '飮', dst: '飲' },
{ src: '營', dst: '営' },
{ src: '圓', dst: '円' },
{ src: '歐', dst: '欧' },
{ src: '奧', dst: '奥' },
{ src: '價', dst: '価' },
{ src: '繪', dst: '絵' },
{ src: '擴', dst: '拡' },
{ src: '學', dst: '学' },
{ src: '罐', dst: '缶' },
{ src: '勸', dst: '勧' },
{ src: '觀', dst: '観' },
{ src: '歸', dst: '帰' },
{ src: '犧', dst: '犠' },
{ src: '擧', dst: '挙' },
{ src: '狹', dst: '狭' },
{ src: '驅', dst: '駆' },
{ src: '莖', dst: '茎' },
{ src: '經', dst: '経' },
{ src: '繼', dst: '継' },
{ src: '缺', dst: '欠' },
{ src: '劍', dst: '剣' },
{ src: '檢', dst: '検' },
{ src: '顯', dst: '顕' },
{ src: '廣', dst: '広' },
{ src: '鑛', dst: '鉱' },
{ src: '碎', dst: '砕' },
{ src: '劑', dst: '剤' },
{ src: '參', dst: '参' },
{ src: '慘', dst: '惨' },
{ src: '絲', dst: '糸' },
{ src: '辭', dst: '辞' },
{ src: '舍', dst: '舎' },
{ src: '壽', dst: '寿' },
{ src: '澁', dst: '渋' },
{ src: '肅', dst: '粛' },
{ src: '將', dst: '将' },
{ src: '證', dst: '証' },
{ src: '乘', dst: '乗' },
{ src: '疊', dst: '畳' },
{ src: '孃', dst: '嬢' },
{ src: '觸', dst: '触' },
{ src: '寢', dst: '寝' },
{ src: '圖', dst: '図' },
{ src: '穗', dst: '穂' },
{ src: '樞', dst: '枢' },
{ src: '齊', dst: '斉' },
{ src: '攝', dst: '摂' },
{ src: '戰', dst: '戦' },
{ src: '潛', dst: '潜' },
{ src: '雙', dst: '双' },
{ src: '莊', dst: '荘' },
{ src: '裝', dst: '装' },
{ src: '藏', dst: '蔵' },
{ src: '續', dst: '続' },
{ src: '體', dst: '体' },
{ src: '臺', dst: '台' },
{ src: '澤', dst: '沢' },
{ src: '膽', dst: '胆' },
{ src: '彈', dst: '弾' },
{ src: '蟲', dst: '虫' },
{ src: '廳', dst: '庁' },
{ src: '鎭', dst: '鎮' },
{ src: '點', dst: '点' },
{ src: '燈', dst: '灯' },
{ src: '盜', dst: '盗' },
{ src: '獨', dst: '独' },
{ src: '貳', dst: '弐' },
{ src: '霸', dst: '覇' },
{ src: '賣', dst: '売' },
{ src: '髮', dst: '髪' },
{ src: '祕', dst: '秘' },
{ src: '佛', dst: '仏' },
{ src: '變', dst: '変' },
{ src: '辯', dst: '弁' },
{ src: '豐', dst: '豊' },
{ src: '飜', dst: '翻' },
{ src: '默', dst: '黙' },
{ src: '與', dst: '与' },
{ src: '譽', dst: '誉' },
{ src: '謠', dst: '謡' },
{ src: '覽', dst: '覧' },
{ src: '獵', dst: '猟' },
{ src: '勵', dst: '励' },
{ src: '齡', dst: '齢' },
{ src: '勞', dst: '労' },
{ src: '壓', dst: '圧' },
{ src: '爲', dst: '為' },
{ src: '隱', dst: '隠' },
{ src: '衞', dst: '衛' },
{ src: '鹽', dst: '塩' },
{ src: '毆', dst: '殴' },
{ src: '穩', dst: '穏' },
{ src: '畫', dst: '画' },
{ src: '壞', dst: '壊' },
{ src: '殼', dst: '殻' },
{ src: '嶽', dst: '岳' },
{ src: '卷', dst: '巻' },
{ src: '關', dst: '関' },
{ src: '顏', dst: '顔' },
{ src: '僞', dst: '偽' },
{ src: '舊', dst: '旧' },
{ src: '峽', dst: '峡' },
{ src: '曉', dst: '暁' },
{ src: '勳', dst: '勲' },
{ src: '惠', dst: '恵' },
{ src: '螢', dst: '蛍' },
{ src: '鷄', dst: '鶏' },
{ src: '縣', dst: '県' },
{ src: '險', dst: '険' },
{ src: '獻', dst: '献' },
{ src: '驗', dst: '験' },
{ src: '效', dst: '効' },
{ src: '號', dst: '号' },
{ src: '濟', dst: '済' },
{ src: '册', dst: '冊' },
{ src: '棧', dst: '桟' },
{ src: '贊', dst: '賛' },
{ src: '齒', dst: '歯' },
{ src: '濕', dst: '湿' },
{ src: '寫', dst: '写' },
{ src: '收', dst: '収' },
{ src: '獸', dst: '獣' },
{ src: '處', dst: '処' },
{ src: '稱', dst: '称' },
{ src: '奬', dst: '奨' },
{ src: '淨', dst: '浄' },
{ src: '繩', dst: '縄' },
{ src: '讓', dst: '譲' },
{ src: '囑', dst: '嘱' },
{ src: '愼', dst: '慎' },
{ src: '粹', dst: '粋' },
{ src: '隨', dst: '随' },
{ src: '數', dst: '数' },
{ src: '靜', dst: '静' },
{ src: '專', dst: '専' },
{ src: '踐', dst: '践' },
{ src: '纖', dst: '繊' },
{ src: '壯', dst: '壮' },
{ src: '搜', dst: '捜' },
{ src: '總', dst: '総' },
{ src: '臟', dst: '臓' },
{ src: '墮', dst: '堕' },
{ src: '帶', dst: '帯' },
{ src: '瀧', dst: '滝' },
{ src: '擔', dst: '担' },
{ src: '團', dst: '団' },
{ src: '遲', dst: '遅' },
{ src: '晝', dst: '昼' },
{ src: '聽', dst: '聴' },
{ src: '遞', dst: '逓' },
{ src: '轉', dst: '転' },
{ src: '當', dst: '当' },
{ src: '稻', dst: '稲' },
{ src: '讀', dst: '読' },
{ src: '惱', dst: '悩' },
{ src: '拜', dst: '拝' },
{ src: '麥', dst: '麦' },
{ src: '拔', dst: '抜' },
{ src: '濱', dst: '浜' },
{ src: '竝', dst: '並' },
{ src: '辨', dst: '弁' },
{ src: '舖', dst: '舗' },
{ src: '襃', dst: '褒' },
{ src: '萬', dst: '万' },
{ src: '譯', dst: '訳' },
{ src: '豫', dst: '予' },
{ src: '搖', dst: '揺' },
{ src: '來', dst: '来' },
{ src: '龍', dst: '竜' },
{ src: '壘', dst: '塁' },
{ src: '隸', dst: '隷' },
{ src: '戀', dst: '恋' },
{ src: '樓', dst: '楼' },
{ src: '鰺', dst: '鯵' },
{ src: '鶯', dst: '鴬' },
{ src: '蠣', dst: '蛎' },
{ src: '攪', dst: '撹' },
{ src: '竈', dst: '竃' },
{ src: '灌', dst: '潅' },
{ src: '諫', dst: '諌' },
{ src: '頸', dst: '頚' },
{ src: '礦', dst: '砿' },
{ src: '蘂', dst: '蕊' },
{ src: '靱', dst: '靭' },
{ src: '賤', dst: '賎' },
{ src: '壺', dst: '壷' },
{ src: '礪', dst: '砺' },
{ src: '檮', dst: '梼' },
{ src: '濤', dst: '涛' },
{ src: '邇', dst: '迩' },
{ src: '蠅', dst: '蝿' },
{ src: '檜', dst: '桧' },
{ src: '儘', dst: '侭' },
{ src: '藪', dst: '薮' },
{ src: '籠', dst: '篭' },
{ src: '彌', dst: '弥' },
{ src: '麩', dst: '麸' },
]
14 changes: 14 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,20 @@ for (const [runtime, normalize] of cases) {
expect(res.addr).toEqual('ビル名')
})

test('旧漢字対応 (亞 -> 亜)', async () => {
const address = '宮城県大崎市古川大崎東亜'
const res = await normalize(address)
expect(res.addr).toEqual('東亜')
expect(res.level).toEqual(3)
})

test('旧漢字対応 (澤 -> 沢)', async () => {
const address = '東京都西多摩郡奥多摩町海澤'
const res = await normalize(address)
expect(res.town).toEqual('海澤')
expect(res.level).toEqual(3)
})

test('旧漢字対応 (麩 -> 麸)', async () => {
const address = '愛知県津島市池麩町'
const res = await normalize(address)
Expand Down