-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert.mjs
196 lines (189 loc) · 7.93 KB
/
convert.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import csv from 'csv'
import fs from 'node:fs'
import path from 'node:path'
// const wanakana = require('wanakana')
// const fullwidth = require('fullwidth').default
const INPUT_FILENAME = 'KEN_ALL-utf8.CSV'
const STANDARD_OUTPUT_FILENAME = 'postal.json'
// const EXTENDED_OUTPUT_FILENAME = 'postal-extended.json'
// const KANA_OUTPUT_FILENAME = 'kana.json'
// const KANA_EXTENDED_OUTPUT_FILENAME = 'kana-extended.json'
// function toHalfWidth (str) {
// return str.replace(/[A-Za-z0-9]/g, function(s) {return String.fromCharCode(s.charCodeAt(0) - 0xFEE0)}).replace('-', '-')
// }
// function toFullWidth (str) {
// return str.replace(/[A-Za-z0-9]/g, function(s) {return String.fromCharCode(s.charCodeAt(0) + 0xFEE0)})
// }
// function capitalize(str){
// if (!str.length) return str
// return str[0].toUpperCase() + str.slice(1)
// }
// function toRomaji(str){
// return capitalize(toHalfWidth(wanakana.toRomaji(fullwidth(str))))
// }
const index = {
postalCode: 2,
prefectureKana: 3,
regionKana: 4,
subregionKana: 5,
prefecture: 6,
region: 7,
subregion: 8
}
// const kana = {}
// const kanaExtended = {}
const writeFile = (fileName, data) => fs.writeFile(path.resolve(__dirname, fileName), JSON.stringify(data, null, 2), err => {
if (err) throw err
})
fs.readFile(path.resolve(__dirname, INPUT_FILENAME), 'utf8', (err, rawString) => {
if (err) throw err
csv.parse(rawString, (err, csvData) => {
if (err) throw err
const postalData = {}
// const extendedPostalData = {}
for (const row of csvData) {
const postalCode = row[index.postalCode]
if (!/^[0-9]{7}$/.test(postalCode))
throw new Error(`Postal code ${postalCode} has an invalid format`)
// const prefectureKana = row[index.prefectureKana]
// const regionKana = row[index.regionKana]
// const subregionKana = row[index.subregionKana].toLowerCase() === 'ikanikeisaiganaibaai' ? '' : row[index.subregionKana].replace('notsuginibanchigakurubaai', '')
const prefecture = row[index.prefecture]
const region = row[index.region]
const subregion = row[index.subregion] === '以下に掲載がない場合' ? '' : row[index.subregion].replace('の次に番地がくる場合', '')
const combinedRegion = region + subregion
// kana[prefecture] = toRomaji(prefectureKana)
// kana[combinedRegion] = toRomaji(regionKana) + ' ' + toRomaji(subregionKana)
// kanaExtended[prefecture] = toRomaji(prefectureKana)
// kanaExtended[region] = toRomaji(regionKana)
// kanaExtended[subregion] = toRomaji(subregionKana)
postalData[postalCode] = {
...postalData[postalCode] || {},
[prefecture]: [
...postalData[postalCode] && postalData[postalCode][prefecture] || [],
combinedRegion
]
}
// extendedPostalData[postalCode] = {
// ...(extendedPostalData[postalCode] || {}),
// [prefecture]: {
// ...(extendedPostalData[postalCode] && extendedPostalData[postalCode][prefecture] || {}),
// [region]: [
// ...(extendedPostalData[postalCode] && extendedPostalData[postalCode][prefecture] && extendedPostalData[postalCode][prefecture][region] || []),
// ...(subregion ? [subregion] : [])
// ]
// }
// }
}
// Do some statistics
let maxRegionCount = 0
let maxRegionPostal
let maxPrefectureCount = 0
let maxPrefecturePostal
for (const [postalCode, prefectures] of Object.entries(postalData)) {
const prefectureCount = Object.keys(prefectures).length
if (prefectureCount > maxPrefectureCount) {
maxPrefectureCount = prefectureCount
maxPrefecturePostal = postalCode
}
if (prefectureCount > 1) {
console.log(`Postal code ${postalCode} is associated with ${prefectureCount} prefectures.`)
for (const prefecture of Object.keys(prefectures))
console.log(prefecture)
}
const regionCount = Object.keys(prefectures).reduce(
(acc, cur) => acc + prefectures[cur].length, 0
)
if (regionCount > maxRegionCount) {
maxRegionCount = regionCount
maxRegionPostal = postalCode
}
const countMatches = (haystack, needle) => (haystack.match(new RegExp(needle, 'g')) || []).length
const endWithANumber = str => {
for (const needle of [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
]) if (str.endsWith(needle)) return true
return false
}
const logOnlyConversionWithSuffix = false //toggle this variable to debug the suffix smart conversion
for (const regions of Object.values(prefectures)) {
const newRegions = []
const smartSubRegions = str => {
if (str.includes('「')) {
newRegions.push(str)
return
}
const fullSubRegions = []
const prefix = str.substring(0, str.indexOf('('))
const subRegions = str.substring(str.indexOf('(') + 1, str.length - 1).split('、')
if (!subRegions.length) throwError()
const lastSubRegion = subRegions[subRegions.length - 1]
let suffix = ''
const suffixList = ['番地', '丁目']
for (const sf of suffixList) if (lastSubRegion.endsWith(sf)) {
suffix = sf
break
}
for (const subRegion of subRegions) {
fullSubRegions.push(`${prefix}(${subRegion}${subRegion.includes(suffix) || !endWithANumber(subRegion) ? '' : suffix})`)
}
if (!logOnlyConversionWithSuffix || suffix) {
console.log(`Successfully did the smart conversion of postal code ${postalCode}. From ${str} to`)
for (const fullSubRegion of fullSubRegions)
console.log(fullSubRegion)
}
newRegions.push(...fullSubRegions)
}
const throwError = () => {
console.log(`Regions of ${postalCode}`)
for (const region of regions) console.log(region)
throw new Error(`Suspected region found with postal code ${postalCode}, but not able to support conversion`)
}
let acc = ''
for (const region of regions) {
if (region.includes('(') && region.includes(')')) {
if (acc) throwError()
if (region.includes('、')) {
if (countMatches(region, ')') !== 1 || countMatches(region, '(') !== 1) throwError()
if (!region.length || region[region.length - 1] !== ')') throwError()
smartSubRegions(region)
}
else newRegions.push(region)
continue
}
if (region.includes('(')) {
if (acc || countMatches(region, '(') !== 1) throwError()
acc = region
continue
}
if (region.includes(')')) {
if (countMatches(region, ')') !== 1) throwError()
if (!region.length || region[region.length - 1] !== ')') throwError()
acc += region
//separate here
smartSubRegions(acc)
acc = ''
continue
}
if (acc) {
acc += region
continue
}
if (acc.includes('、')) throwError()
newRegions.push(region)
}
if (acc) throwError()
regions.splice(0, regions.length, ...newRegions)
}
}
console.log(`Postal code ${maxRegionPostal} is associated with ${maxRegionCount} regions`)
console.log(`Postal code ${maxPrefecturePostal} is associated with ${maxPrefectureCount} prefectures`)
const postalCodeCount = Object.keys(postalData).length
console.log(`${csvData.length} records have been proceed to obtain ${postalCodeCount} postal codes`)
writeFile(STANDARD_OUTPUT_FILENAME, postalData)
//remove support to other data
// writeFile(EXTENDED_OUTPUT_FILENAME, extendedPostalData)
// writeFile(KANA_OUTPUT_FILENAME, kana)
// writeFile(KANA_EXTENDED_OUTPUT_FILENAME, kanaExtended)
})
})