Skip to content

Commit

Permalink
added 13 communes in autocomplete that are impossible to fetch (excep…
Browse files Browse the repository at this point in the history
…t by their zipcode)
  • Loading branch information
fcamblor committed Apr 20, 2021
1 parent 43db6de commit 4bce49d
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 20 deletions.
1 change: 1 addition & 0 deletions public/autocomplete-cache/eu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"eu","communes":[{"c":"76255","z":"76260","n":"Eu","d":"76","g":"1.4285,50.0418"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/mont.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"mont","communes":[{"c":"64396","z":"64300","n":"Mont","d":"64","g":"-0.6543,43.4284"},{"c":"65317","z":"65240","n":"Mont","d":"65","g":"0.4386,42.8197"},{"c":"71301","z":"71140","n":"Mont","d":"71","g":"3.8209,46.6327"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/our.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"our","communes":[{"c":"39400","z":"39700","n":"Our","d":"39","g":"5.6468,47.1218"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/ri.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"ri","communes":[{"c":"61349","z":"61210","n":"Ri","d":"61","g":"-0.1393,48.7865"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/sai.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"sai","communes":[{"c":"61358","z":"61200","n":"Sai","d":"61","g":"0.0231,48.7405"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/ur.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"ur","communes":[{"c":"66218","z":"66760","n":"Ur","d":"66","g":"1.9331,42.4652"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/urt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"urt","communes":[{"c":"64546","z":"64240","n":"Urt","d":"64","g":"-1.2767,43.4737"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"us","communes":[{"c":"95625","z":"95450","n":"Us","d":"95","g":"1.9616,49.1054"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/ville.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"ville","communes":[{"c":"60676","z":"60400","n":"Ville","d":"60","g":"2.9337,49.5601"},{"c":"67507","z":"67220","n":"Villé","d":"67","g":"7.3024,48.34"}]}
1 change: 1 addition & 0 deletions public/autocomplete-cache/y.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"y","communes":[{"c":"80829","z":"80190","n":"Y","d":"80","g":"2.9828,49.8059"}]}
2 changes: 1 addition & 1 deletion public/autocompletes.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/components/vmd-commune-selector.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class VmdCommuneSelectorComponent extends LitElement {
// If it changed, then we need to raise a new autocomplete-triggered event, so that
// communesDisponibles is updated with a new autocomplete key
const filterMatchedAnAutocomplete = this.filter.split('').some((_, filterSizeAttempt) => {
let filterAttempt = this.filter.substring(0, filterSizeAttempt+1);
let filterAttempt = this.filter.substring(0, this.filter.length - filterSizeAttempt);
const searchableFilterAttempt = Strings.toFullTextSearchableString(filterAttempt);
if(this.autocompleteTriggers!.has(searchableFilterAttempt)) {
if(filterAttempt === this.filterMatchingAutocomplete) {
Expand Down
76 changes: 58 additions & 18 deletions tools/communes-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,74 @@ function search(communes, query) {
);
}

function toCompactedCommune(c) {
return {
c: c.code,
z: c.codePostal,
n: c.nom,
d: c.codeDepartement,
g: (c && c.centre && c.centre.coordinates)?c.centre.coordinates.join(","):undefined
};
}

function communeComparatorFor(query) {
return (c1, c2) =>
Math.min(leven(c1.fullTextSearchableNom, query), leven(c1.codePostal, query))
- Math.min(leven(c2.fullTextSearchableNom, query), leven(c2.codePostal, query));
}

function generateFilesForQuery(query, communes, unreferencedCommuneKeys) {
try {
const matchingCommunes = search(communes, query);
const matchingCommunesByKey = new Map(matchingCommunes.map(c => [keyOf(c), c]));

if(matchingCommunes.length === 0) {
return [];
} else if(matchingCommunes.length < MAX_NUMBER_OF_COMMUNES_PER_FILE || query.length === MAX_AUTOCOMPLETE_TRIGGER_LENGTH) {
matchingCommunes.forEach(matchingCommune => unreferencedCommuneKeys.delete(keyOf(matchingCommune)));
matchingCommunes.sort((c1, c2) => {
return Math.min(leven(c1.fullTextSearchableNom, query), leven(c1.codePostal, query))
- Math.min(leven(c2.fullTextSearchableNom, query), leven(c2.codePostal, query))
});
matchingCommunesByKey.forEach((commune, matchingCommuneKey) => unreferencedCommuneKeys.delete(matchingCommuneKey));

matchingCommunes.sort(communeComparatorFor(query));

// Converting commune info in a most compacted way : keeping only useful fields, 1-char keys, latng compaction
const compactedCommunes = matchingCommunes.map(c => ({
c: c.code,
z: c.codePostal,
n: c.nom,
d: c.codeDepartement,
g: (c && c.centre && c.centre.coordinates)?c.centre.coordinates.join(","):undefined
}))
fs.writeFileSync(`../public/autocomplete-cache/${query}.json`, JSON.stringify({query, communes: compactedCommunes}), 'utf8');
const compactedCommunes = matchingCommunes.map(toCompactedCommune)

fs.writeFileSync(`../public/autocomplete-cache/${query}.json`, JSON.stringify({query, communes: compactedCommunes }), 'utf8');
console.info(`Autocomplete cache for query [${query}] completed !`)
return [query];

return [{ query, matchingCommunesByKey }];
} else {
return INDEXED_CHARS.reduce((queries, q) => {
Array.prototype.push.apply(queries, generateFilesForQuery(query+q, communes, unreferencedCommuneKeys));
return queries;
const subQueries = INDEXED_CHARS.reduce((subQueries, q) => {
Array.prototype.push.apply(subQueries, generateFilesForQuery(query+q, matchingCommunes, unreferencedCommuneKeys));
return subQueries;
}, [])

let filteredMatchingCommunesByKey = new Map(matchingCommunesByKey);
subQueries.forEach(r => {
r.matchingCommunesByKey.forEach((commune, key) => filteredMatchingCommunesByKey.delete(key));
});

// Here, the idea is to add communes with name shorter than the autocomplete keys
// For example, we have the communes named "Y" and "Sai" while minimum autocomplete for these
// kind of communes are respectively longer than 1 and 3
// That's why we're adding here specific keys for these communes
// Note that we don't have a lot of communes in that case, only 10 commune names, representing 13 different
// communes
filteredMatchingCommunesByKey = new Map([...filteredMatchingCommunesByKey].filter(([k, v]) => v.fullTextSearchableNom.length === query.length))
if(filteredMatchingCommunesByKey.size) {
const communesMatchantExactement = [...filteredMatchingCommunesByKey.values()];
[...filteredMatchingCommunesByKey.keys()].forEach(k => unreferencedCommuneKeys.delete(k));

communesMatchantExactement.sort(communeComparatorFor(query));

// Converting commune info in a most compacted way : keeping only useful fields, 1-char keys, latng compaction
const compactedCommunesNonGereesParLesSousNoeuds = communesMatchantExactement.map(toCompactedCommune)
fs.writeFileSync(`../public/autocomplete-cache/${query}.json`, JSON.stringify({query, communes: compactedCommunesNonGereesParLesSousNoeuds /*, subsequentAutoCompletes: true */ }), 'utf8');
console.info(`Intermediate autocomplete cache for query [${query}] completed with ${compactedCommunesNonGereesParLesSousNoeuds.length} communes !`)

subQueries.splice(0, 0, {query, matchingCommunesByKey: filteredMatchingCommunesByKey });
}

return subQueries;
}
} catch(e) {
console.error(e);
Expand All @@ -86,7 +126,7 @@ Promise.all([
const unreferencedCommuneKeys = new Set(communeByKey.keys());

const generatedIndexes = INDEXED_CHARS.reduce((queries, q) => {
Array.prototype.push.apply(queries, generateFilesForQuery(q, communes, unreferencedCommuneKeys));
Array.prototype.push.apply(queries, generateFilesForQuery(q, communes, unreferencedCommuneKeys).map(r => r.query));
return queries;
}, []);

Expand Down

0 comments on commit 4bce49d

Please sign in to comment.