@@ -6,27 +6,43 @@ import Button from '@mui/material/Button'
66import Chip from '@mui/material/Chip'
77import isString from 'lodash/isString'
88import map from 'lodash/map'
9- import orderBy from 'lodash/orderBy'
109
1110import Retired from '../common/Retired'
1211import Score from './Score'
1312import MapButton from './MapButton'
1413import ConceptSummaryProperties from '../concepts/ConceptSummaryProperties'
1514
15+
1616const getBestSynonym = synonyms => {
17- return orderBy ( synonyms , match => match . length )
17+ return synonyms
1818 . map ( text => {
1919 const matches = [ ...text . matchAll ( / < e m > ( .* ?) < \/ e m > / g) ] ;
20- const longestMatch = matches . reduce ( ( a , b ) => ( b [ 1 ] . length > a . length ? b [ 1 ] : a ) , "" ) ;
21- const startsWithMatch = text . indexOf ( `<em>${ longestMatch } </em>` ) === 0 ;
22- return { text, longestMatch, length : longestMatch . length , startsWithMatch } ;
20+ const longestMatch = matches . reduce (
21+ ( a , b ) => ( b [ 1 ] . length > a . length ? b [ 1 ] : a ) ,
22+ ""
23+ ) ;
24+
25+ const emTag = `<em>${ longestMatch } </em>` ;
26+ const startsWithMatch = text . indexOf ( emTag ) === 0 ;
27+
28+ // prefer exact "<em>...</em>"
29+ const isExactEmOnly = text . trim ( ) === emTag ;
30+
31+ return {
32+ text,
33+ longestMatch,
34+ length : longestMatch . length ,
35+ startsWithMatch,
36+ isExactEmOnly
37+ } ;
2338 } )
2439 . sort ( ( a , b ) => {
25- if ( b . length !== a . length ) return b . length - a . length ; // longest match first
40+ if ( b . length !== a . length ) return b . length - a . length ; // longest match first
41+ if ( b . isExactEmOnly !== a . isExactEmOnly ) return b . isExactEmOnly ? 1 : - 1 ; // ✅ exact "<em>...</em>" wins
2642 if ( b . startsWithMatch !== a . startsWithMatch ) return b . startsWithMatch ? 1 : - 1 ; // prefer start
2743 return 0 ;
28- } ) [ 0 ] . text ; // return best match's text
29- }
44+ } ) [ 0 ] ? .text ;
45+ } ;
3046
3147
3248const Item = ( { concept, setShowHighlights, onMap, isSelectedForMap, noScore, repoVersion, synonymPrefix, isAIRecommended, bridge, bridgeChild, mapping, showAlgo, candidatesScore, algoScoreFirst, placeholderMap} ) => {
0 commit comments