-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improves chinese and japanese tokenizers (#899)
- Loading branch information
1 parent
22cf6a2
commit 8fdd4bb
Showing
22 changed files
with
302 additions
and
1,497 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "../src", | ||
"module": "nodenext", | ||
"moduleResolution": "nodenext" | ||
} | ||
} |
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,16 @@ | ||
{ | ||
"extends": "./build.json", | ||
"include": [ | ||
"../src/**/*.ts", | ||
"../src/**/*.cts", | ||
"../src/**/*.tsx", | ||
"../src/**/*.json" | ||
], | ||
"exclude": [ | ||
"../src/**/*.mts", | ||
"../src/package.json" | ||
], | ||
"compilerOptions": { | ||
"outDir": "../.tshy-build/commonjs" | ||
} | ||
} |
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 @@ | ||
{ | ||
"extends": "./build.json", | ||
"include": [ | ||
"../src/**/*.ts", | ||
"../src/**/*.mts", | ||
"../src/**/*.tsx", | ||
"../src/**/*.json" | ||
], | ||
"exclude": [ | ||
"../src/package.json" | ||
], | ||
"compilerOptions": { | ||
"outDir": "../.tshy-build/esm" | ||
} | ||
} |
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 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 @@ | ||
import { createTokenizer as createJapaneseTokenizer } from "./japanese.js"; | ||
|
||
export default { | ||
japanese: createJapaneseTokenizer, | ||
} |
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,94 @@ | ||
import type { DefaultTokenizer, DefaultTokenizerConfig } from "@orama/orama"; | ||
import { normalizeToken } from "@orama/orama/internals"; | ||
|
||
const tokenizerLanguage = "japanese"; | ||
|
||
type TLanguage = typeof tokenizerLanguage; | ||
|
||
type JapaneseTokenizerConfig = DefaultTokenizerConfig & { | ||
language: TLanguage; | ||
}; | ||
|
||
const defaultConfig: JapaneseTokenizerConfig = { | ||
language: tokenizerLanguage, | ||
}; | ||
|
||
const segmenter = new Intl.Segmenter("ja", { granularity: "word" }); | ||
|
||
/* c8 ignore next 10 */ | ||
function trim(text: string[]): string[] { | ||
while (text[text.length - 1] === "") { | ||
text.pop(); | ||
} | ||
while (text[0] === "") { | ||
text.shift(); | ||
} | ||
return text; | ||
} | ||
|
||
function tokenize(text: string): string[] { | ||
const segments = segmenter.segment(text); | ||
|
||
const tokens: string[] = []; | ||
for (const segment of segments) { | ||
if (segment.isWordLike) { | ||
tokens.push(segment.segment); | ||
} | ||
} | ||
|
||
return tokens; | ||
} | ||
|
||
function tokenizeInternal( | ||
this: DefaultTokenizer, | ||
input: string, | ||
language?: TLanguage, | ||
prop?: string, | ||
): string[] { | ||
/* c8 ignore next 3 */ | ||
if (typeof input !== "string") { | ||
return [input]; | ||
} | ||
|
||
let tokens: string[]; | ||
if (prop && this?.tokenizeSkipProperties?.has(prop)) { | ||
// @ts-ignore | ||
tokens = [this?.normalizeToken?.bind(this, prop ?? "")(input)]; | ||
} else { | ||
tokens = tokenize(input); | ||
} | ||
|
||
const trimTokens = trim(tokens); | ||
|
||
if (!this.allowDuplicates) { | ||
return Array.from(new Set(trimTokens)); | ||
} | ||
|
||
return trimTokens; | ||
} | ||
|
||
export function createTokenizer( | ||
config: JapaneseTokenizerConfig = defaultConfig, | ||
): DefaultTokenizer { | ||
const tokenizerConfig = { | ||
tokenize: tokenizeInternal, | ||
language: config.language, | ||
stemmerSkipProperties: new Set( | ||
config.stemmerSkipProperties ? [config.stemmerSkipProperties].flat() : [], | ||
), | ||
tokenizeSkipProperties: new Set( | ||
config.tokenizeSkipProperties | ||
? [config.tokenizeSkipProperties].flat() | ||
: [], | ||
), | ||
stopWords: config.stopWords as string[] | undefined, | ||
allowDuplicates: Boolean(config.allowDuplicates), | ||
normalizeToken, | ||
normalizationCache: new Map(), | ||
}; | ||
|
||
// @ts-ignore | ||
tokenizerConfig.tokenize = tokenizeInternal.bind(tokenizeInternal); | ||
|
||
return tokenizerConfig; | ||
} |
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,94 @@ | ||
import type { DefaultTokenizer, DefaultTokenizerConfig } from "@orama/orama"; | ||
import { normalizeToken } from "@orama/orama/internals"; | ||
|
||
const tokenizerLanguage = "japanese"; | ||
|
||
type TLanguage = typeof tokenizerLanguage; | ||
|
||
type JapaneseTokenizerConfig = DefaultTokenizerConfig & { | ||
language: TLanguage; | ||
}; | ||
|
||
const defaultConfig: JapaneseTokenizerConfig = { | ||
language: tokenizerLanguage, | ||
}; | ||
|
||
const segmenter = new Intl.Segmenter("zh-CN", { granularity: "word" }); | ||
|
||
/* c8 ignore next 10 */ | ||
function trim(text: string[]): string[] { | ||
while (text[text.length - 1] === "") { | ||
text.pop(); | ||
} | ||
while (text[0] === "") { | ||
text.shift(); | ||
} | ||
return text; | ||
} | ||
|
||
function tokenize(text: string): string[] { | ||
const segments = segmenter.segment(text); | ||
|
||
const tokens: string[] = []; | ||
for (const segment of segments) { | ||
if (segment.isWordLike) { | ||
tokens.push(segment.segment); | ||
} | ||
} | ||
|
||
return tokens; | ||
} | ||
|
||
function tokenizeInternal( | ||
this: DefaultTokenizer, | ||
input: string, | ||
language?: TLanguage, | ||
prop?: string, | ||
): string[] { | ||
/* c8 ignore next 3 */ | ||
if (typeof input !== "string") { | ||
return [input]; | ||
} | ||
|
||
let tokens: string[]; | ||
if (prop && this?.tokenizeSkipProperties?.has(prop)) { | ||
// @ts-ignore | ||
tokens = [this?.normalizeToken?.bind(this, prop ?? "")(input)]; | ||
} else { | ||
tokens = tokenize(input); | ||
} | ||
|
||
const trimTokens = trim(tokens); | ||
|
||
if (!this.allowDuplicates) { | ||
return Array.from(new Set(trimTokens)); | ||
} | ||
|
||
return trimTokens; | ||
} | ||
|
||
export function createTokenizer( | ||
config: JapaneseTokenizerConfig = defaultConfig, | ||
): DefaultTokenizer { | ||
const tokenizerConfig = { | ||
tokenize: tokenizeInternal, | ||
language: config.language, | ||
stemmerSkipProperties: new Set( | ||
config.stemmerSkipProperties ? [config.stemmerSkipProperties].flat() : [], | ||
), | ||
tokenizeSkipProperties: new Set( | ||
config.tokenizeSkipProperties | ||
? [config.tokenizeSkipProperties].flat() | ||
: [], | ||
), | ||
stopWords: config.stopWords as string[] | undefined, | ||
allowDuplicates: Boolean(config.allowDuplicates), | ||
normalizeToken, | ||
normalizationCache: new Map(), | ||
}; | ||
|
||
// @ts-ignore | ||
tokenizerConfig.tokenize = tokenizeInternal.bind(tokenizeInternal); | ||
|
||
return tokenizerConfig; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.