-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-engineer index to match both partial and multiple words (#30)
* Initial work in progress * Re-engineer index to match both partial and multiple words * Remove @liquicode/lib-tokenize dependency
- Loading branch information
Showing
14 changed files
with
356 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Emit } from '@tanishiking/aho-corasick'; | ||
|
||
import { SearchResult } from '../search/index'; | ||
import { Token } from '../tokenizers'; | ||
|
||
/** | ||
* Takes a given search result (which has the start/end position and a "stemmed" keyword) | ||
* that was matched, and maps them to a new start/end position for the original keyword | ||
* which was stem was created from | ||
* @param searchResult | ||
* @param tokens | ||
* @returns | ||
*/ | ||
export const mapStemToOriginalText = (searchResult: Emit, tokens: Token[]): SearchResult => { | ||
const matchingTokens = tokens.filter( | ||
(token) => token.stemStart >= searchResult.start && token.stemEnd <= searchResult.end + 1 | ||
); | ||
|
||
return { | ||
start: matchingTokens[0].originalStart, | ||
end: matchingTokens[matchingTokens.length - 1].originalEnd, | ||
indexKeyword: matchingTokens | ||
.map((token) => token.stem) | ||
.join('') | ||
.toLowerCase(), | ||
originalKeyword: matchingTokens.map((token) => token.originalText).join(''), | ||
}; | ||
}; |
2 changes: 1 addition & 1 deletion
2
src/search/search.utils.spec.ts → src/search/redactText.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Indexer } from '../indexing/indexer'; | ||
import Search from './index'; | ||
|
||
const getKeywordsMockFn = jest.fn(); | ||
|
||
jest.mock('../indexing/indexer', () => { | ||
return { | ||
Indexer: jest.fn().mockImplementation(() => { | ||
return { | ||
getKeywords: getKeywordsMockFn, | ||
getDocumentsByKeyword: () => [{}], | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('Search class', () => { | ||
it('Highlights single keywords that can be stemmed', () => { | ||
getKeywordsMockFn.mockReturnValue(['search', 'note']); | ||
const text = 'This is a note that I will be use for searching'; | ||
|
||
const indexer = new Indexer(null); | ||
const search = new Search(indexer); | ||
const results = search.find(text); | ||
|
||
expect(results).toEqual([ | ||
{ | ||
start: 10, | ||
end: 14, | ||
indexKeyword: 'note', | ||
originalKeyword: 'note', | ||
}, | ||
{ | ||
start: 38, | ||
end: 47, | ||
indexKeyword: 'search', | ||
originalKeyword: 'searching', | ||
}, | ||
]); | ||
}); | ||
|
||
it('Longer keyword matches are always prioritised for highlight', () => { | ||
getKeywordsMockFn.mockReturnValue(['github', 'github fork']); | ||
const text = 'I use GitHub Forks as part of my development flow'; | ||
|
||
const indexer = new Indexer(null); | ||
const search = new Search(indexer); | ||
const results = search.find(text); | ||
|
||
expect(results).toEqual([ | ||
{ | ||
start: 6, | ||
end: 18, | ||
indexKeyword: 'github fork', | ||
originalKeyword: 'GitHub Forks', | ||
}, | ||
]); | ||
}); | ||
|
||
it('Three word keyword is highlighted', () => { | ||
getKeywordsMockFn.mockReturnValue(['shared', 'client', 'record', 'share client record']); | ||
const text = 'Designing a shared client record is a great idea but challenging'; | ||
|
||
const indexer = new Indexer(null); | ||
const search = new Search(indexer); | ||
const results = search.find(text); | ||
|
||
expect(results).toEqual([ | ||
{ | ||
start: 12, | ||
end: 32, | ||
indexKeyword: 'share client record', | ||
originalKeyword: 'shared client record', | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { PorterStemmer } from 'natural'; | ||
|
||
import { WordPunctStemTokenizer } from '../tokenizers'; | ||
|
||
/** | ||
* Stem a given phrase. If the phrase is made up of multiple words, | ||
* the last word in the phrase is the only one that will be stemmed | ||
* @param text input text | ||
* @returns stemmed text | ||
*/ | ||
export const stemLastWord = (text: string): string => { | ||
return PorterStemmer.stem(text); | ||
}; | ||
|
||
export const stemPhrase = (text: string): string => { | ||
const tokenizer = new WordPunctStemTokenizer(); | ||
return tokenizer | ||
.tokenize(text) | ||
.map((t) => t.stem) | ||
.join(''); | ||
}; |
Oops, something went wrong.