-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
78 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { createCorpus, Document } from './corpus' | ||
|
||
function doc(docID: string | number, text: string): Document { | ||
return { docID: typeof docID === 'string' ? docID : docID.toString(), text } | ||
} | ||
|
||
describe('Corpus', () => { | ||
test('#length', () => { | ||
expect(createCorpus([doc(1, 'a'), doc(2, 'b')]).length).toEqual | ||
}) | ||
}) |
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,32 @@ | ||
import { CorpusSearchResult } from './search' | ||
|
||
/** | ||
* A documentation corpus. | ||
*/ | ||
export interface Corpus { | ||
search(query: string): Promise<CorpusSearchResult[]> | ||
length: number | ||
} | ||
|
||
export interface Document { | ||
docID: string | ||
text: string | ||
} | ||
|
||
export function createCorpus(docs: Document[]): Corpus { | ||
return { | ||
search: query => { | ||
const terms = query.split(/\s+/) | ||
const results: CorpusSearchResult[] = [] | ||
for (const doc of docs) { | ||
if (terms.some(term => doc.text.includes(term))) { | ||
results.push({ docID: doc.docID, score: 1, excerpt: doc.text }) | ||
} | ||
} | ||
return Promise.resolve(results) | ||
}, | ||
get length(): number { | ||
return docs.length | ||
}, | ||
} | ||
} |
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,15 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { createCorpus, Document } from './corpus' | ||
import { CorpusSearchResult } from './search' | ||
|
||
function doc(docID: string | number, text: string): Document { | ||
return { docID: typeof docID === 'string' ? docID : docID.toString(), text } | ||
} | ||
|
||
describe('Corpus#search', () => { | ||
test('finds matches', async () => { | ||
expect(await createCorpus([doc(1, 'a'), doc(2, 'b')]).search('b')).toEqual<CorpusSearchResult[]>([ | ||
{ docID: '2', score: 1, excerpt: 'b' }, | ||
]) | ||
}) | ||
}) |
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,5 @@ | ||
export interface CorpusSearchResult { | ||
docID: string | ||
score: number | ||
excerpt: string | ||
} |
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