Skip to content

Commit 20321ef

Browse files
sarangan12xirzec
andauthored
Convenience Method to create a synonymmap object (Azure#16054)
* Convenience Method to create a synonymmap object * Update sdk/search/search-documents/src/synonymMapHelper.ts Co-authored-by: Jeff Fisher <[email protected]> * Fix for PR Comments Co-authored-by: Jeff Fisher <[email protected]>
1 parent 8236d61 commit 20321ef

File tree

8 files changed

+93
-1
lines changed

8 files changed

+93
-1
lines changed

sdk/search/search-documents/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"LICENSE"
4242
],
4343
"browser": {
44-
"./dist-esm/src/base64.js": "./dist-esm/src/base64.browser.js"
44+
"./dist-esm/src/base64.js": "./dist-esm/src/base64.browser.js",
45+
"./dist-esm/src/synonymMapHelper.js": "./dist-esm/src/synonymMapHelper.browser.js"
4546
},
4647
"//metadata": {
4748
"constantPaths": [

sdk/search/search-documents/review/search-documents.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ export interface CreateOrUpdateSynonymMapOptions extends OperationOptions {
288288
// @public
289289
export type CreateSkillsetOptions = OperationOptions;
290290

291+
// @public
292+
export function createSynonymMapFromFile(name: string, filePath: string): Promise<SynonymMap>;
293+
291294
// @public
292295
export type CreateSynonymMapOptions = OperationOptions;
293296

sdk/search/search-documents/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,4 @@ export {
301301
SearchIndexerKnowledgeStoreTableProjectionSelector
302302
} from "./generated/service/models";
303303
export { AzureKeyCredential } from "@azure/core-auth";
304+
export { createSynonymMapFromFile } from "./synonymMapHelper";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { SynonymMap } from "./serviceModels";
5+
6+
/**
7+
* Helper method to create a SynonymMap object. This is a NodeJS only method.
8+
* Will throw an error for browser.
9+
*
10+
* @param _name - Name of the SynonymMap.
11+
* @param _filePath - Path of the file that contains the Synonyms (seperated by new lines)
12+
* @returns SynonymMap object
13+
*/
14+
export async function createSynonymMapFromFile(
15+
_name: string,
16+
_filePath: string
17+
): Promise<SynonymMap> {
18+
throw new Error("Not implemented for browser.");
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { SynonymMap } from "./serviceModels";
5+
import { promisify } from "util";
6+
import * as fs from "fs";
7+
const readFileAsync = promisify(fs.readFile);
8+
9+
/**
10+
* Helper method to create a SynonymMap object. This is a NodeJS only method.
11+
*
12+
* @param name - Name of the SynonymMap.
13+
* @param filePath - Path of the file that contains the Synonyms (seperated by new lines)
14+
* @returns SynonymMap object
15+
*/
16+
export async function createSynonymMapFromFile(
17+
name: string,
18+
filePath: string
19+
): Promise<SynonymMap> {
20+
const synonyms: string[] = (await readFileAsync(filePath, "utf-8"))
21+
.replace(/\r/g, "")
22+
.split("\n")
23+
.map((line) => line.trim())
24+
.filter(Boolean);
25+
26+
return {
27+
name,
28+
synonyms
29+
};
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { assert } from "chai";
5+
import { createSynonymMapFromFile } from "../../../src/synonymMapHelper.browser";
6+
7+
describe("synonymmap", () => {
8+
it("create synonymmap from file(browser)", async function() {
9+
let errorThrown = false;
10+
try {
11+
await createSynonymMapFromFile("my-synonym-map-1", "./test/internal/synonymMap.txt");
12+
} catch (ex) {
13+
errorThrown = true;
14+
}
15+
assert.isTrue(errorThrown, "Expected createSynonymMapFromFile to fail with an exception");
16+
});
17+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { assert } from "chai";
5+
import { createSynonymMapFromFile } from "../../../src";
6+
import { SynonymMap } from "../../../src/serviceModels";
7+
8+
describe("synonymmap", () => {
9+
it("create synonymmap from file(node)", async function() {
10+
const synonymMap: SynonymMap = await createSynonymMapFromFile(
11+
"my-synonym-map-1",
12+
"./test/internal/synonymMap.txt"
13+
);
14+
assert.equal(synonymMap.name, "my-synonym-map-1");
15+
assert.equal(synonymMap.synonyms.length, 2);
16+
assert.equal(synonymMap.synonyms[0], "United States, United States of America => USA");
17+
assert.equal(synonymMap.synonyms[1], "Washington, Wash. => WA");
18+
});
19+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
United States, United States of America => USA
2+
Washington, Wash. => WA

0 commit comments

Comments
 (0)