Skip to content

Commit b649546

Browse files
authored
Merge pull request #7 from samchon/feat/dictionary
New function `JsonTranslator.dictionary()`
2 parents 6c76ae7 + 8c38502 commit b649546

File tree

10 files changed

+423
-61
lines changed

10 files changed

+423
-61
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ node_modules/
55
package-lock.json
66
pnpm-lock.yaml
77
env.ts
8-
.env
8+
.env
9+
*.log

README.md

Lines changed: 110 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ English (source) | Korean | Japanese | Arabic
6868

6969

7070
## API
71+
### `JsonTranslator.translate()`
7172
```typescript
7273
export class JsonTranslator {
7374
/**
@@ -89,24 +90,6 @@ export class JsonTranslator {
8990
* @returns The translated JSON data.
9091
*/
9192
public translate<T>(props: JsonTranslator.IProps<T>): Promise<T>;
92-
93-
/**
94-
* Detect the language of JSON data.
95-
*
96-
* Pick the longest text from the JSON input data and detect the language
97-
* through the Google Translate API, with the similar properties like the
98-
* {@link JsonTranslator.translate} method.
99-
*
100-
* Therefore, if you want to filter some specific values to participate in
101-
* the language detection, fill the {@link JsonTranslator.IProps.filter}
102-
* function.
103-
*
104-
* @param input Properties for language detection.
105-
* @returns The detected language or `undefined` if the language is unknown.
106-
*/
107-
public detect<T>(
108-
props: Omit<JsonTranslator.IProps<T>, "source" | "target">,
109-
): Promise<string | undefined>;
11093
}
11194
export namespace JsonTranslator {
11295
/**
@@ -194,4 +177,113 @@ export namespace JsonTranslator {
194177
value: string;
195178
}
196179
}
180+
```
181+
182+
### `JsonTranslator.detect()`
183+
```typescript
184+
export class JsonTranslator {
185+
/**
186+
* Detect the language of JSON data.
187+
*
188+
* Pick the longest text from the JSON input data and detect the language
189+
* through the Google Translate API, with the similar properties like the
190+
* {@link JsonTranslator.translate} method.
191+
*
192+
* Therefore, if you want to filter out some specific values to participate in
193+
* the language detection, fill the {@link JsonTranslator.IDetectProps.filter}
194+
* function.
195+
*
196+
* @param input Properties for language detection.
197+
* @returns The detected language or `undefined` if the language is unknown.
198+
*/
199+
public async detect<T>(
200+
props: JsonTranslator.IDetectProps<T>,
201+
): Promise<string | undefined> {
202+
const texts: string[] = JsonTranslateComposer.composeTexts(props);
203+
return this._Detect_language(texts);
204+
}
205+
}
206+
export namespace JsonTranslator {
207+
/**
208+
* Properties for the language detection.
209+
*
210+
* Keyworded properties used in the {@link JsonTranslator.detect} method.
211+
*/
212+
export interface IDetectProps<T> {
213+
/**
214+
* The JSON input data to detect language.
215+
*/
216+
input: T;
217+
218+
/**
219+
* Filter function specifying which data to translate.
220+
*
221+
* @param explore Information about the data to explore.
222+
* @returns `true` if the data should be translated; otherwise, `false`.
223+
*/
224+
filter?: ((explore: IExplore) => boolean) | null | undefined;
225+
226+
/**
227+
* Reserved dictionary of pre-translated values.
228+
*
229+
* The dictionary is a key-value pair object containing the pre-translated
230+
* values. The key means the original value, and the value means the
231+
* pre-translated value.
232+
*
233+
* If this dictionary has been configured and a JSON input value matches to
234+
* the dictionary's key, the dictionary's value would be used instead of
235+
* calling the Google Translate API.
236+
*/
237+
dictionary?: Record<string, string> | null | undefined;
238+
}
239+
}
240+
```
241+
242+
### `JsonTranslator.dictionary()`
243+
```typescript
244+
export class JsonTranslator {
245+
/**
246+
* Compose dictionary from translated.
247+
*
248+
* Compose dictionary between original JSON input data and its translated
249+
* output data. The dictionary is a key-value pair object containing the
250+
* original value and its translated value.
251+
*
252+
* If you've composed {@link JsonTranslator.IProps.filter} function in
253+
* the {@link JsonTranslator.translate} method, don't forget to re-apply
254+
* the filter function to the {@link JsonTranslator.IDictionaryProps.filter}
255+
* property.
256+
*
257+
* @param props Properties for the dictionary composition.
258+
* @returns Composed dictionary
259+
*/
260+
public dictionary<T>(
261+
props: JsonTranslator.IDictionaryProps<T>,
262+
): Record<string, string> {
263+
return JsonTranslateComposer.composeDictionary(props);
264+
}
265+
}
266+
export namespace JsonTranslator {
267+
/**
268+
* Properties for the dictionary composition.
269+
*
270+
* Keyworded properties used in nthe {@link JsonTranslator.dictionary} method.
271+
*/
272+
export interface IDictionaryProps<T> {
273+
/**
274+
* Input JSON, the original data.
275+
*/
276+
input: T;
277+
278+
/**
279+
* Output JSON, the translated data.
280+
*/
281+
output: T;
282+
283+
/**
284+
* Filter function specifying which data be translated.
285+
*/
286+
filter?: ((explore: IExplore) => boolean) | null | undefined;
287+
}
288+
}
197289
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@samchon/json-translator",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "JSON Translator via Google Translation API with optimization strategies",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/JsonTranslator.ts

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Translate } from "@google-cloud/translate/build/src/v2";
22

3-
import { JsonTranslateExecutor } from "./internal/JsonTranslateExecutor";
3+
import { JsonTranslateComposer } from "./internal/JsonTranslateComposer";
44

55
/**
66
* JSON Translator.
@@ -41,8 +41,8 @@ export class JsonTranslator {
4141
* @returns The translated JSON data.
4242
*/
4343
public async translate<T>(props: JsonTranslator.IProps<T>): Promise<T> {
44-
const collection: JsonTranslateExecutor.ICollection =
45-
JsonTranslateExecutor.prepare(props);
44+
const collection: JsonTranslateComposer.ICollection =
45+
JsonTranslateComposer.composeCollection(props);
4646
const translated: string[] = [];
4747
const from: string | undefined =
4848
props.source === null
@@ -100,20 +100,41 @@ export class JsonTranslator {
100100
* through the Google Translate API, with the similar properties like the
101101
* {@link JsonTranslator.translate} method.
102102
*
103-
* Therefore, if you want to filter some specific values to participate in
104-
* the language detection, fill the {@link JsonTranslator.IProps.filter}
103+
* Therefore, if you want to filter out some specific values to participate in
104+
* the language detection, fill the {@link JsonTranslator.IDetectProps.filter}
105105
* function.
106106
*
107107
* @param input Properties for language detection.
108108
* @returns The detected language or `undefined` if the language is unknown.
109109
*/
110110
public async detect<T>(
111-
props: Omit<JsonTranslator.IProps<T>, "source" | "target">,
111+
props: JsonTranslator.IDetectProps<T>,
112112
): Promise<string | undefined> {
113-
const texts: string[] = JsonTranslateExecutor.getTexts(props);
113+
const texts: string[] = JsonTranslateComposer.composeTexts(props);
114114
return this._Detect_language(texts);
115115
}
116116

117+
/**
118+
* Compose dictionary from translated.
119+
*
120+
* Compose dictionary between original JSON input data and its translated
121+
* output data. The dictionary is a key-value pair object containing the
122+
* original value and its translated value.
123+
*
124+
* If you've composed {@link JsonTranslator.IProps.filter} function in
125+
* the {@link JsonTranslator.translate} method, don't forget to re-apply
126+
* the filter function to the {@link JsonTranslator.IDictionaryProps.filter}
127+
* property.
128+
*
129+
* @param props Properties for the dictionary composition.
130+
* @returns Composed dictionary
131+
*/
132+
public dictionary<T>(
133+
props: JsonTranslator.IDictionaryProps<T>,
134+
): Record<string, string> {
135+
return JsonTranslateComposer.composeDictionary(props);
136+
}
137+
117138
/**
118139
* @internal
119140
*/
@@ -130,6 +151,8 @@ export class JsonTranslator {
130151
export namespace JsonTranslator {
131152
/**
132153
* Properties for the translation.
154+
*
155+
* Keyworded properties used in the {@link JsonTranslator.translate} method.
133156
*/
134157
export interface IProps<T> {
135158
/**
@@ -177,6 +200,61 @@ export namespace JsonTranslator {
177200
dictionary?: Record<string, string> | null | undefined;
178201
}
179202

203+
/**
204+
* Properties for the language detection.
205+
*
206+
* Keyworded properties used in the {@link JsonTranslator.detect} method.
207+
*/
208+
export interface IDetectProps<T> {
209+
/**
210+
* The JSON input data to detect language.
211+
*/
212+
input: T;
213+
214+
/**
215+
* Filter function specifying which data to translate.
216+
*
217+
* @param explore Information about the data to explore.
218+
* @returns `true` if the data should be translated; otherwise, `false`.
219+
*/
220+
filter?: ((explore: IExplore) => boolean) | null | undefined;
221+
222+
/**
223+
* Reserved dictionary of pre-translated values.
224+
*
225+
* The dictionary is a key-value pair object containing the pre-translated
226+
* values. The key means the original value, and the value means the
227+
* pre-translated value.
228+
*
229+
* If this dictionary has been configured and a JSON input value matches to
230+
* the dictionary's key, the dictionary's value would be used instead of
231+
* calling the Google Translate API.
232+
*/
233+
dictionary?: Record<string, string> | null | undefined;
234+
}
235+
236+
/**
237+
* Properties for the dictionary composition.
238+
*
239+
* Keyworded properties used in nthe {@link JsonTranslator.dictionary} method.
240+
*/
241+
export interface IDictionaryProps<T> {
242+
/**
243+
* Input JSON, the original data.
244+
*/
245+
input: T;
246+
247+
/**
248+
* Output JSON, the translated data.
249+
*/
250+
output: T;
251+
252+
/**
253+
* Filter function specifying which data be translated.
254+
*/
255+
filter?: ((explore: IExplore) => boolean) | null | undefined;
256+
}
257+
180258
/**
181259
* Exploration information used in the {@link IProps.filter} function.
182260
*/
@@ -214,8 +292,15 @@ export namespace JsonTranslator {
214292
}
215293
}
216294

295+
/**
296+
* @internal
297+
*/
217298
interface IPiece {
218299
text: string;
219300
length: number;
220301
}
302+
303+
/**
304+
* @internal
305+
*/
221306
const SEPARATOR = " //|-0-|\\ ";

0 commit comments

Comments
 (0)