Skip to content

Commit

Permalink
Merge pull request #1 from obot-ai/Matcher
Browse files Browse the repository at this point in the history
Matcherオプションを追加し、キーワードベースでのマッチルールを追加
  • Loading branch information
LINYE-MARIANA authored Jun 11, 2024
2 parents bcf4df6 + 574349d commit f7e9ebe
Show file tree
Hide file tree
Showing 15 changed files with 2,910 additions and 1,192 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,23 @@ import { Generator, Fetcher } from '@obot-ai/completion-generator'

/* Generator Constructor
Matchers:
- ForwardMatcher:
- 入力文全文を前方一致でマッチする
- キーワードで判定された部分以外も比較対象テキストに含まれる必要がある
- KeywordMatcher:
- キーワードベースの正規表現を用いてマッチする
- キーワードの全文マッチが優先、マッチがなければ部分マッチを行う
- ConcatMatcher:
- 複数のMatcherのマッチ結果を結合して出力する
- addMatcherByClass, addMatcherで結合対象のMatcherを追加しないとマッチ成功結果が得られない
- KeywordForwardMatcher:
- KeywordMatcher, ForwardMatcherをConcatMatcherで結合したクラス
- DefaultMatcher: ForwardMatcher
Args:
- properties: object
- matcher?: Matcher?:
- 指定すると他のオプション設定は無視される、していなければ他のオプション設定でDefaultMatcherを構成して使う
- keywordSeparator?: string: キーワードの分割文字
- minKeywordLength?: number: キーワードとして認定する最短長さ
- strictMatchLocales?: string[]: 単語ごとにキーワードをマッチする言語(指定していない言語コードは文字ごとにマッチを行う)
Expand All @@ -60,6 +75,7 @@ import { Generator, Fetcher } from '@obot-ai/completion-generator'
- locale: string: 言語コード
- Return: 数字で示した比較結果。0: = , >0: >, <0: <
- filter?: (localeData: LocaleDataItem[], input: string, locale: string) => MatchedResultData[]: 既存のマッチルールを上書きするメソッド
- KeywordMatcherでのみ有効。廃止予定、カスタマイズしたMatcherの指定を推奨
- Args:
- localeData: LocaleDataItem({text: string, keywords: string})[]: 該当言語の候補データ集
- input: string: 入力テキスト
Expand Down
209 changes: 142 additions & 67 deletions dist/completion-generator.d.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,88 @@
export type LocaleDataItem = {
text: string;
keywords: string;
};
export type MatchedResult = {
isMatched: boolean;
data?: MatchedResultData;
};
export type MatchedResultData = {
text: string;
keywords: string;
matchedKeywords?: MatchedKeyword[];
};
export type MatchedKeyword = {
text: string;
startAt: number;
endAt: number;
export declare type CompletionFetcherProperties = {
apiKey: string;
apiKeyHeaderName?: string;
getEndpoint?: GetEndpoint;
handleResponse?: HandleResponse;
};
export type CompletionGeneratorProperties = {
keywordSeparator: string;
minKeywordLength: number;
strictMatchLocales: string[];

export declare type CompletionGeneratorProperties = {
keywordSeparator?: string;
minKeywordLength?: number;
strictMatchLocales?: string[];
maxResults?: number;
comparator?: LocaleDataComparator;
filter?: LocaleDateFilter;
matcher?: Matcher;
};
export type LocaleDataComparator = (itemA: LocaleDataItem, itemB: LocaleDataItem, input: string, locale: string) => number;
export type LocaleDateFilter = (localeData: LocaleDataItem[], input: string, locale: string) => MatchedResultData[];
export interface CompletionGeneratorInter {
/** キーワードの分割文字 */
keywordSeparator: string;
/** キーワードとして認定する最短長さ */
minKeywordLength: number;
/** _strictMatchを使う言語コード設定 */
strictMatchLocales: string[];
/** ソート用メソッド */

export declare type CompletionMatcherProperties = {
keywordSeparator?: string;
minKeywordLength?: number;
strictMatchLocales?: string[];
maxResults?: number;
comparator?: LocaleDataComparator;
/** 絞り込み用メソッド */
filter?: LocaleDateFilter;
data: Map<string, LocaleDataItem[]>;
loadData: (locale: string, localeData: LocaleDataItem[]) => void;
generateCompletions: (input: string, locale: string) => MatchedResultData[];
};

export declare class ConcatMatcher extends Matcher {
matchers: Matcher[];
constructor(properties: CompletionMatcherProperties);
addMatcherByClass(matcherClass: typeof Matcher): void;
addMatcher(matcher: Matcher): void;
loadData(locale: string, localeData: LocaleDataItem[]): void;
match(input: string, locale: string): MatchedResultData[];
}
export declare class Generator implements CompletionGeneratorInter {
keywordSeparator: string;
minKeywordLength: number;
strictMatchLocales: string[];
comparator?: LocaleDataComparator;
filter?: LocaleDateFilter;
data: Map<string, LocaleDataItem[]>;

export declare const DefaultMatcher: typeof ForwardMatcher;

export declare class Fetcher {
apiKey: string;
apiKeyHeaderName: string;
getEndpoint: GetEndpoint;
handleResponse: HandleResponse;
constructor(properties?: CompletionFetcherProperties);
/**
* 指定エンドポイントから候補データを取得
* @param locale 言語コード
* @returns
*/
fetch(locale: string): Promise<LocaleDataItem[]>;
_fetch(endpoint: string): Promise<any>;
isFetchAvailable(): boolean;
}

/**
* 入力文を前方一致でマッチするMatcher
*/
export declare class ForwardMatcher extends Matcher {
match(input: string, locale: string): MatchedResultData[];
_match(localeData: LocaleDataItem[], input: string, locale: string): MatchedResultData[];
_charMatch(dataItem: LocaleDataItem, input: string): {
isMatched: boolean;
data?: undefined;
} | {
isMatched: boolean;
data: {
text: string;
keywords: string;
matchedKeywords: MatchedKeyword[];
};
};
_wordMatch(dataItem: LocaleDataItem, input: string): {
isMatched: boolean;
data?: undefined;
} | {
isMatched: boolean;
data: {
text: string;
keywords: string;
matchedKeywords: MatchedKeyword[];
};
};
}

declare class Generator_2 {
matcher: Matcher;
constructor(properties?: CompletionGeneratorProperties);
/**
* 候補データをインスタンスにセットする
Expand All @@ -62,34 +98,73 @@ export declare class Generator implements CompletionGeneratorInter {
* @returns 補完データ
*/
generateCompletions(input: string, locale: string): MatchedResultData[];
_getMatchedCompletions(localeData: LocaleDataItem[], input: string, locale: string): MatchedResultData[];
_match(dataItem: LocaleDataItem, input: string): MatchedResult;
_strictMatch(dataItem: LocaleDataItem, input: string): MatchedResult;
get keywordSeparator(): string;
get minKeywordLength(): number;
get strictMatchLocales(): string[];
get data(): Map<string, LocaleDataItem[]>;
}
export type CompletionFetcherProperties = {
apiKey: string;
apiKeyHeaderName?: string;
getEndpoint?: GetEndpoint;
handleResponse?: HandleResponse;
};
export type GetEndpoint = (locale: string) => string;
export type HandleResponse = (data: any) => LocaleDataItem[];
export interface CompletionFetcherInter {
apiKey: string;
fetch(locale: string): Promise<LocaleDataItem[]>;
export { Generator_2 as Generator }

export declare type GetEndpoint = (locale: string) => string;

export declare type HandleResponse = (data: any) => LocaleDataItem[];

export declare class KeywordForwardMatcher extends ConcatMatcher {
constructor(properties: CompletionMatcherProperties);
}
export declare class Fetcher implements CompletionFetcherInter {
apiKey: string;
apiKeyHeaderName: string;
getEndpoint: GetEndpoint;
handleResponse: HandleResponse;
constructor(properties?: CompletionFetcherProperties);

export declare class KeywordMatcher extends Matcher {
exactRegExpMap: Map<string, RegExp>;
partialRegExpMap: Map<string, RegExp>;
loadData(locale: string, localeData: LocaleDataItem[]): void;
match(input: string, locale: string): MatchedResultData[];
_match(localeData: LocaleDataItem[], input: string, locale: string): MatchedResultData[];
}

export declare type LocaleDataComparator = (itemA: LocaleDataItem, itemB: LocaleDataItem, input: string, locale: string) => number;

export declare type LocaleDataItem = {
idx?: number;
text: string;
keywords: string;
};

export declare type LocaleDateFilter = (localeData: LocaleDataItem[], input: string, locale: string) => MatchedResultData[];

export declare type MatchedKeyword = {
text: string;
startAt: number;
endAt: number;
};

export declare type MatchedResult = {
isMatched: boolean;
data?: MatchedResultData;
};

export declare type MatchedResultData = {
idx?: number;
text: string;
keywords: string;
matchedKeywords?: MatchedKeyword[];
};

export declare class Matcher {
keywordSeparator: string;
minKeywordLength: number;
strictMatchLocales: string[];
comparator?: LocaleDataComparator;
filter?: LocaleDateFilter;
data: Map<string, LocaleDataItem[]>;
maxResults?: number;
constructor(properties?: CompletionMatcherProperties);
/**
* 指定エンドポイントから候補データを取得
* 候補データをインスタンスにセットする
* @param locale 言語コード
* @returns
* @param localeData 言語データ
*/
fetch(locale: string): Promise<LocaleDataItem[]>;
_fetch(endpoint: string): Promise<any>;
isFetchAvailable(): boolean;
loadData(locale: string, localeData: LocaleDataItem[]): void;
match(input: string, locale: string): MatchedResultData[];
}

export { }
2 changes: 1 addition & 1 deletion dist/completion-generator.es5.js

Large diffs are not rendered by default.

Loading

0 comments on commit f7e9ebe

Please sign in to comment.