-
Notifications
You must be signed in to change notification settings - Fork 17
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
Gestion des mots contractés #72
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||
replacePatern: abbreviations, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replacePatterns |
||
boost: { | ||
population: (commune, score) => { | ||
if (commune.population) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const normalizeString = require('../normalizeString'); | ||
const replaceAbbreviations = require('../replaceAbbreviations'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Le nom me parait trop spécifique There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. À modifier |
||
const lunr = require('lunr'); | ||
const { clone, sortBy } = require('lodash'); | ||
|
||
|
@@ -7,6 +8,7 @@ class TextIndex { | |
if (!key) throw new Error('key is required'); | ||
this._key = key; | ||
this._boost = options.boost || {}; | ||
this._replacePatern = options.replacePatern || {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _replacePatterns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pas utile de mettre une valeur par défaut coûteuse à tester |
||
const refKey = this._refKey = options.ref || 'id'; | ||
this._refIndex = new Map(); | ||
this._index = lunr(function () { | ||
|
@@ -31,6 +33,9 @@ class TextIndex { | |
|
||
find(terms, options = {}) { | ||
let boosted = false; | ||
|
||
terms = replaceAbbreviations(terms, this._replacePatern); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const results = this._index.search(terms) | ||
.map(result => { | ||
const item = clone(this._refIndex.get(result.ref)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const removeDiacritics = require('./removeDiacritics'); | ||
|
||
function replaceAbbreviations(search, abbreviations) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. terms, patterns |
||
const terms = removeDiacritics(search) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pourquoi utilise |
||
.toLowerCase() | ||
.replace(/"/g, '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Un commentaire est bienvenu There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Je pense que tu fais trop de chose dans cette fonction replace. Mieux faut avoir plusieurs fonctions, les appeler dans le bon ordre et leur donner le nom adapté. |
||
.replace(/-/g, ' ') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idem |
||
.split(' '); | ||
|
||
if (terms.length <= 1) return search; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. search est trop abstrait There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Au lieu de faire toutes ces transformations pour rien, je ferai le test de la présence d'un espace dès le début. On fait de l'auto-complétion, chaque miliseconde compte. |
||
|
||
return terms | ||
.map(token => token in abbreviations ? abbreviations[token] : token) | ||
.join(' '); | ||
} | ||
|
||
module.exports = replaceAbbreviations; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-env mocha */ | ||
const expect = require('expect.js'); | ||
const replaceAbbreviations = require('../lib/searchableCollection/replaceAbbreviations'); | ||
|
||
describe.only('replaceAbbreviations()', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only |
||
const abbreviations = { | ||
'st': 'saint', | ||
'ste': 'sainte', | ||
'cgne': 'campagne', | ||
}; | ||
|
||
beforeEach(done => { | ||
done(); | ||
}); | ||
|
||
describe('Words separated by spaces', function () { | ||
it('should replace patern', function () { | ||
const str = '"st louis"'; | ||
const out = 'saint louis'; | ||
|
||
expect(replaceAbbreviations(str, abbreviations)).to.equal(out); | ||
}); | ||
|
||
it('should replace patern', 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 patern', function () { | ||
const str = '"st-louis"'; | ||
const out = 'saint louis'; | ||
|
||
expect(replaceAbbreviations(str, abbreviations)).to.equal(out); | ||
}); | ||
|
||
it('should replace patern', 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 patern', () => { | ||
const str = '"st"'; | ||
|
||
expect(replaceAbbreviations(str, abbreviations)).to.equal(str); | ||
}); | ||
}); | ||
|
||
describe('Patern is contained in a word', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pattern |
||
it('should not replace patern', () => { | ||
const str = '"le stinx"'; | ||
const out = 'le stinx'; | ||
|
||
expect(replaceAbbreviations(str, abbreviations)).to.equal(out); | ||
}); | ||
}); | ||
|
||
describe('Accents management', () => { | ||
it('sould not replace the accent with a space', () => { | ||
const str = '"St-Auban-sur-l\'Ouvèze"'; | ||
const out = 'saint auban sur l\'ouveze'; | ||
|
||
expect(replaceAbbreviations(str, abbreviations)).to.equal(out); | ||
}); | ||
}); | ||
|
||
}); |
There was a problem hiding this comment.
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