Skip to content
Open
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
9 changes: 8 additions & 1 deletion lib/communeHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const { initFields, initFormat } = require('./helpers');

const abbreviations = {
'st': 'saint',
'ste': 'sainte',
'cgne': 'campagne',
};

@teleboas teleboas Feb 3, 2017

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A rapprocher de la liste utilisée pour les noms de rues dans la BAN ?
https://github.com/etalab/ban-data/blob/master/data/abbrev.txt



const initCommuneFields = initFields({
default: ['nom', 'code', 'codeDepartement', 'codeRegion', 'codesPostaux', 'population'],
base: ['nom', 'code'],
Expand All @@ -10,4 +17,4 @@ const initCommuneFormat = initFormat({
defaultGeometry: 'centre',
});

module.exports = { initCommuneFields, initCommuneFormat };
module.exports = { initCommuneFields, initCommuneFormat, abbreviations };
2 changes: 2 additions & 0 deletions lib/communes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const SearchableCollection = require('./searchableCollection');
const { abbreviations } = require('./communeHelpers.js');

const schema = {
nom: {
type: 'text',
queryWith: 'nom',
ref: 'code',
replacePatterns: abbreviations,
boost: {
population: (commune, score) => {
if (commune.population) {
Expand Down
7 changes: 7 additions & 0 deletions lib/searchableCollection/indexes/text.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const normalizeString = require('../normalizeString');
const replaceAbbreviations = require('../replaceAbbreviations');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le nom me parait trop spécifique

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

À modifier

const lunr = require('lunr');
const { clone, sortBy } = require('lodash');

Expand All @@ -7,6 +8,7 @@ class TextIndex {
if (!key) throw new Error('key is required');
this._key = key;
this._boost = options.boost || {};
this._replacePatterns = options.replacePatterns;
const refKey = this._refKey = options.ref || 'id';
this._refIndex = new Map();
this._index = lunr(function () {
Expand All @@ -31,6 +33,11 @@ class TextIndex {

find(terms, options = {}) {
let boosted = false;

if (this._replacePatterns) {
terms = replaceAbbreviations(terms, this._replacePatterns);
}

const results = this._index.search(terms)
.map(result => {
const item = clone(this._refIndex.get(result.ref));
Expand Down
12 changes: 12 additions & 0 deletions lib/searchableCollection/replaceAbbreviations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function replaceAbbreviations(terms, patterns) {
if (!terms.includes(' ') && !terms.includes('-')) return terms;

return terms
.toLocaleLowerCase()
.replace(/-/g, ' ')
.split(' ')
.map(token => token in patterns ? patterns[token] : token)
.join(' ');
}

module.exports = replaceAbbreviations;
64 changes: 64 additions & 0 deletions test/replaceAbbreviations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-env mocha */
const expect = require('expect.js');
const replaceAbbreviations = require('../lib/searchableCollection/replaceAbbreviations');

describe('replaceAbbreviations()', function () {
const abbreviations = {
'st': 'saint',
'ste': 'sainte',
'cgne': 'campagne',
};

beforeEach(done => {
done();
});

describe('Words separated by spaces', function () {
it('should replace Pattern', function () {
const str = 'st louis';
const out = 'saint louis';

expect(replaceAbbreviations(str, abbreviations)).to.equal(out);
});

it('should replace Pattern', function () {
const str = 'marcilly la cgne';
const out = 'marcilly la campagne';

expect(replaceAbbreviations(str, abbreviations)).to.equal(out);
});
});

describe('Words separated by dashes', function () {
it('should replace Pattern', function () {
const str = 'st-louis';
const out = 'saint louis';

expect(replaceAbbreviations(str, abbreviations)).to.equal(out);
});

it('should replace Pattern', function () {
const str = 'marcilly-la-cgne';
const out = 'marcilly la campagne';

expect(replaceAbbreviations(str, abbreviations)).to.equal(out);
});
});

describe('search contained only one word', () => {
it('should not replace Pattern', () => {
const str = 'st';

expect(replaceAbbreviations(str, abbreviations)).to.equal(str);
});
});

describe('Pattern is contained in a word', () => {
it('should not replace Pattern', () => {
const str = 'le stinx';

expect(replaceAbbreviations(str, abbreviations)).to.equal(str);
});
});

});