-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
209 lines (164 loc) · 4.94 KB
/
index.js
File metadata and controls
209 lines (164 loc) · 4.94 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
const {join} = require('path')
const {readFileSync} = require('fs')
const parse = require('csv-parse/lib/sync')
const {deburr} = require('lodash')
const rules = parse(readFileSync(join(__dirname, 'normadresse.csv')), {
columns: true
}).reduce((rules, rule) => {
const step = parseFloat(rule.etape, 10)
rules[step] = rules[step] || []
rules[step].push({
long: rule.long,
short: rule.court.replace(/\\g<(\d+)>/g, '$$$1')
})
return rules
}, {})
function selectShortWords(input, output, maxLength) {
const long = input.split(' ')
const short = output.split(' ')
for (let i = short.length - 1; i >= 0; i--) {
if (short[i] === '@') {
long[i - 1] = short[i - 1]
delete long[i]
delete short[i]
}
}
let next
for (let i = 1; i < short.length; ++i) {
next = [
...short.slice(0, i).filter(w => w),
...long.slice(i).filter(w => w)
].join(' ')
if (next.length <= maxLength) {
break
}
}
return next
}
function normalize(input, maxLength) {
input = deburr(input)
.toUpperCase()
.replace(/[^A-Z0-9s]/g, ' ')
.replace(/ {2}/g, ' ')
let output = input
if (output.length <= maxLength) {
return output
}
// 1 - abréviation du type de voie
for (const rule of rules[1]) {
output = output.replace(new RegExp(rule.long), rule.short)
}
output = selectShortWords(input, output, maxLength)
if (output.length <= maxLength) {
return output
}
input = output
// 2 - abréviation des titres militaires, religieux et civils
for (let step = 0; step < 2; step++) {
for (const rule of rules[2]) {
output = output.replace(new RegExp(` ${rule.long} `), ` ${rule.short} `)
}
}
output = selectShortWords(input, output, maxLength)
if (output.length <= maxLength) {
return output
}
input = output
// 4 - abréviations générales
for (let step = 0; step < 3; step++) {
for (const rule of rules[4]) {
output = output.replace(new RegExp(`(^| )${rule.long} `), ` ${rule.short.toLowerCase()} `).trim()
}
}
output = selectShortWords(input, output, maxLength)
if (output.length <= maxLength) {
return output
}
input = output
// 5 - abréviation type de voies
for (let step = 0; step < 2; step++) {
for (const rule of rules[5]) {
output = output.replace(new RegExp(` ${rule.long.trim()} `), ` ${rule.short.trim().toLowerCase()} `)
}
for (const rule of rules[1]) {
output = output.replace(new RegExp(` ${rule.long.trim()} `), ` ${rule.short.trim().toLowerCase()} `)
}
}
output = selectShortWords(input, output, maxLength)
if (output.length <= maxLength) {
return output
}
input = output
// 3 - abréviations prénoms sauf pour ST prénoms
let words = output.split(' ')
for (let i = 1; i < words.length - 1; i++) {
const word = words[i]
if (words[i - 1].slice(0, 5) !== 'SAINT') {
for (const rule of rules[3]) {
if (new RegExp(`${rule.long}`).test(word)) {
words[i] = rule.short.toLowerCase()
}
}
}
output = words.join(' ')
}
output = selectShortWords(input, output, maxLength)
if (output.length <= maxLength) {
return output
}
input = output
// 6 - abréviation saint/sainte et prolonge(e)/inférieur(e)
for (let step = 0; step < 2; step++) {
for (const rule of rules[6]) {
output = output.replace(new RegExp(rule.long), rule.short.toLowerCase())
}
}
output = selectShortWords(input, output, maxLength)
if (output.length <= maxLength) {
return output
}
input = output
// 5bis - type de voie en début...
for (const rule of rules[5]) {
output = output.replace(new RegExp(`^${rule.long.trim()} `), `${rule.short.trim().toLowerCase()} `)
}
output = selectShortWords(input, output, maxLength)
if (output.length <= maxLength) {
return output
}
input = output
// 9 - remplacement des particules des noms propres pour ne pas les supprimer
for (const rule of rules[9]) {
output = output.replace(new RegExp(rule.long), rule.short)
}
// 10 - élimination des articles
for (let step = 0; step < 6; step++) {
output = output.replace(/ (LE|LA|LES|AU|AUX|DE|DU|DES|D|ET|A|L|SUR|EN) /, ' ')
if (output.length <= maxLength) {
return output
}
}
// 11 - abréviations résiduelle
words = output.split(' ')
for (let i = 1; i < words.length - 1; ++i) {
const word = words[i]
if (word === word.toUpperCase() && word.length > 1 && word.charCodeAt(0) >= 'A'.charCodeAt(0)) {
words[i] = word[0]
output = words.join(' ')
if (output.length <= maxLength) {
return output
}
}
}
// 12 - élimination des articles
for (let step = 0; step < 4; step++) {
output = output.replace(/ (le|la|les|au|aux|de|du|des|d|et|a|l|sur) /, ' ')
if (output.length <= maxLength) {
return output
}
}
return output
}
module.exports = function (input, maxLength = 32) {
return normalize(input, maxLength).toUpperCase()
}