Skip to content

Commit

Permalink
Get short synonyms list inside updateMatches() #202
Browse files Browse the repository at this point in the history
  • Loading branch information
metincansiper committed Mar 15, 2018
1 parent 002e20b commit 9d4897a
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/client/components/element-info/entity-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ class EntityInfo extends React.Component {
.then( matches => {
if( this._unmounted ){ return; }

let getShortSynonyms = (synonyms, cmpStr = name) => {
if (synonyms.length <= MAX_SYNONYMS_SHOWN) {
return synonyms;
}

// use a memoized distance function to avoid re-calculating the same distances inside nsmallest function
let distance = _.memoize( s => {
return distanceMetric(s, cmpStr);
} );

let cmp = (s1, s2) => {
return distance(s1) - distance(s2);
};

// fix the first constant number of synonyms
let fixed = synonyms.slice(0, MAX_FIXED_SYNONYMS);
// complete the short list by the best matches among the remaining synonyms
let remainingBestMatch = Heap.nsmallest(synonyms.slice(MAX_FIXED_SYNONYMS), MAX_SYNONYMS_SHOWN - MAX_FIXED_SYNONYMS, cmp);

return [...fixed, ...remainingBestMatch];
};

matches.forEach( (match) => {
if (match.synonyms) {
match.shortSynonyms = getShortSynonyms(match.synonyms);
}
} );

if( clearOldMatches ){
s.matches = matches;
} else {
Expand Down Expand Up @@ -569,37 +597,15 @@ class EntityInfo extends React.Component {
h('span.entity-info-title', 'Charge'),
h('span', m.charge)
]),
h('div.entity-info-section', !m.synonyms ? [] : [
h('div.entity-info-section', !m.shortSynonyms ? [] : [
h('span.entity-info-title', 'Synonyms'),
...shortenSynonyms(m.synonyms, this.data.name).map( name => h('span.entity-info-alt-name', [
...m.shortSynonyms.map( name => h('span.entity-info-alt-name', [
h(Highlighter, { text: name, terms: searchTerms })
]))
])
];
};

let shortenSynonyms = (synonyms, cmpStr) => {
if (synonyms.length <= MAX_SYNONYMS_SHOWN) {
return synonyms;
}

// use a memoized distance function to avoid re-calculating the same distances inside nsmallest function
let distance = _.memoize( s => {
return distanceMetric(s, cmpStr);
} );

let cmp = (s1, s2) => {
return distance(s1) - distance(s2);
};

// fix the first constant number of synonyms
let fixed = synonyms.slice(0, MAX_FIXED_SYNONYMS);
// complete the short list by the best matches among the remaining synonyms
let remainingBestMatch = Heap.nsmallest(synonyms.slice(MAX_FIXED_SYNONYMS), MAX_SYNONYMS_SHOWN - MAX_FIXED_SYNONYMS, cmp);

return [...fixed, ...remainingBestMatch];
};

let targetFromAssoc = (m) => {
let complete = stage === STAGES.COMPLETED;
let highlight = !complete;
Expand Down

0 comments on commit 9d4897a

Please sign in to comment.