/* eslint-env jest */ import { countKeyphrases, detectLanguage, englishForms, hasKeyphrase, keyphrase, pageLanguage, romanianStem, sentences, } from '../keyphrase'; describe('romanianStem', () => { it('gives every form of a noun or an adjective one stem', () => { const families: Record = { hanorac: [ 'hanorac', 'hanoracul', 'hanoracului', 'hanorace', 'hanoracele', 'hanoracelor', ], marim: ['marime', 'marimea', 'marimii', 'marimi', 'marimile'], tiga: ['tigaie', 'tigaia', 'tigaii', 'tigai', 'tigaile', 'tigailor'], font: ['fonta', 'fontei', 'fonte', 'fontele'], potrivit: ['potrivit', 'potrivita', 'potrivite', 'potriviti'], lucr: ['lucru', 'lucrul', 'lucruri', 'lucrurile', 'lucrurilor'], negr: ['neagra', 'negru', 'negri', 'negre'], flor: ['floare', 'flori', 'florile'], roch: ['rochie', 'rochia', 'rochii', 'rochiile'], }; Object.entries(families).forEach(([stem, forms]) => forms.forEach(form => expect([form, romanianStem(form)]).toEqual([form, stem]) ) ); }); it('leaves short words and other words apart', () => { expect(romanianStem('din')).toBe('din'); expect(romanianStem('cat')).toBe('cat'); expect(romanianStem('categorii')).not.toBe(romanianStem('cat')); expect(romanianStem('pantof')).not.toBe(romanianStem('hanorac')); }); }); describe('englishForms', () => { it('knows plurals and the possessive', () => { expect(englishForms('hoodies')).toContain('hoodie'); expect(englishForms('cities')).toContain('city'); expect(englishForms('boxes')).toContain('box'); expect(englishForms("men's")).toContain('men'); expect(englishForms('dress')).toEqual(['dress']); expect(englishForms('bus')).toEqual(['bus']); expect(englishForms('categories')).not.toContain('cat'); }); }); describe('the language a page is read by', () => { it('tells Romanian and English from the text, and says nothing on too little', () => { expect( detectLanguage( 'Cum alegi hanoracul potrivit: ghid de mărimi. Alege mărimea mai mare dacă porți ceva pe dedesubt.' ) ).toBe('ro'); expect( detectLanguage( 'How to choose the right hoodie size for you. The chart is on this page and it is easy to read.' ) ).toBe('en'); expect(detectLanguage('Zipper hoodie')).toBe(''); }); it('prefers the language a multilingual plugin gives, then the text, then the site', () => { expect( pageLanguage('ro', 'The hoodie of the year is here and it is warm.', 'en') ).toBe('ro'); expect(pageLanguage('fr', 'Cum alegi hanoracul și mărimea.', 'ro')).toBe( '' ); expect(pageLanguage('', 'Hanorac', 'ro_RO')).toBe('ro'); expect(pageLanguage(undefined, 'Hanorac', 'de_DE')).toBe(''); }); }); describe('sentences', () => { it('ends a sentence at a block, at . ! ? ; and at a title separator', () => { expect( sentences( '

Hoodie

Size matters. Pick well!

' ) ).toEqual(['Hoodie', 'Size matters.', 'Pick well!', 'One', 'Two']); expect(sentences('Zipper Hoodie: 5 Reasons | Shop')).toEqual([ 'Zipper Hoodie: 5 Reasons', 'Shop', ]); expect(sentences('Costs 2.5 euro')).toEqual(['Costs 2.5 euro']); }); }); describe('hasKeyphrase', () => { const en = (keyword: string): ReturnType => keyphrase(keyword, 'en'); const ro = (keyword: string): ReturnType => keyphrase(keyword, 'ro'); it('finds the words of a keyword in any order within one sentence', () => { expect( hasKeyphrase( 'The best cast iron skillet 26 cm for induction.', en('26 cm cast iron skillet') ) ).toBe(true); expect( hasKeyphrase('Our hoodies come in every size.', en('hoodie size')) ).toBe(true); expect( hasKeyphrase("Men's hiking boots on sale.", en("men's hiking boots")) ).toBe(true); }); it('finds Romanian words in their inflected forms, the small words optional', () => { expect( hasKeyphrase( 'Cum alegi hanoracul potrivit: ghid de mărimi', ro('hanorac') ) ).toBe(true); expect( hasKeyphrase( 'Alegerea mărimii potrivite durează un minut.', ro('mărimea potrivită') ) ).toBe(true); expect( hasKeyphrase( 'Tigaia din fontă se încinge uniform.', ro('tigaie din fontă') ) ).toBe(true); expect( hasKeyphrase('Fonta tigăii ține căldura.', ro('tigaie din fontă')) ).toBe(true); }); it('wants every word in the same sentence, and the right words', () => { expect( hasKeyphrase( 'The skillet is cast iron. It measures 26 cm.', en('26 cm cast iron skillet') ) ).toBe(false); expect( hasKeyphrase('

Hoodie

Size matters.

', en('hoodie size')) ).toBe(false); expect(hasKeyphrase('Categories and education', en('cat'))).toBe(false); expect( hasKeyphrase('Tigaia din oțel se încinge repede.', ro('tigaie din fontă')) ).toBe(false); expect(hasKeyphrase('Anything at all', keyphrase('', 'en'))).toBe(false); }); it('matches word for word in a language without rules', () => { const phrase = keyphrase('robe rouge', ''); expect(hasKeyphrase('Une rouge robe', phrase)).toBe(true); expect(hasKeyphrase('Des robes rouges', phrase)).toBe(false); }); }); describe('countKeyphrases', () => { it('counts every mention in a sentence, a word serving one mention only', () => { const text = '

The hoodie size chart and the hoodie size guide. A hoodie.

'; expect(countKeyphrases(text, [keyphrase('hoodie size', 'en')])).toBe(2); expect( countKeyphrases(text, [ keyphrase('hoodie', 'en'), keyphrase('hoodie size', 'en'), ]) ).toBe(3); expect( countKeyphrases('

Mărimea potrivită și mărimile hanoracelor.

', [ keyphrase('mărime', 'ro'), ]) ).toBe(2); }); });