Skip to content

Commit

Permalink
Convenience Method to create a synonymmap object (Azure#16054)
Browse files Browse the repository at this point in the history
* Convenience Method to create a synonymmap object

* Update sdk/search/search-documents/src/synonymMapHelper.ts

Co-authored-by: Jeff Fisher <xirzec@xirzec.com>

* Fix for PR Comments

Co-authored-by: Jeff Fisher <xirzec@xirzec.com>
  • Loading branch information
sarangan12 and xirzec authored Jun 29, 2021
1 parent 8236d61 commit 20321ef
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sdk/search/search-documents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"LICENSE"
],
"browser": {
"./dist-esm/src/base64.js": "./dist-esm/src/base64.browser.js"
"./dist-esm/src/base64.js": "./dist-esm/src/base64.browser.js",
"./dist-esm/src/synonymMapHelper.js": "./dist-esm/src/synonymMapHelper.browser.js"
},
"//metadata": {
"constantPaths": [
Expand Down
3 changes: 3 additions & 0 deletions sdk/search/search-documents/review/search-documents.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ export interface CreateOrUpdateSynonymMapOptions extends OperationOptions {
// @public
export type CreateSkillsetOptions = OperationOptions;

// @public
export function createSynonymMapFromFile(name: string, filePath: string): Promise<SynonymMap>;

// @public
export type CreateSynonymMapOptions = OperationOptions;

Expand Down
1 change: 1 addition & 0 deletions sdk/search/search-documents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,4 @@ export {
SearchIndexerKnowledgeStoreTableProjectionSelector
} from "./generated/service/models";
export { AzureKeyCredential } from "@azure/core-auth";
export { createSynonymMapFromFile } from "./synonymMapHelper";
19 changes: 19 additions & 0 deletions sdk/search/search-documents/src/synonymMapHelper.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { SynonymMap } from "./serviceModels";

/**
* Helper method to create a SynonymMap object. This is a NodeJS only method.
* Will throw an error for browser.
*
* @param _name - Name of the SynonymMap.
* @param _filePath - Path of the file that contains the Synonyms (seperated by new lines)
* @returns SynonymMap object
*/
export async function createSynonymMapFromFile(
_name: string,
_filePath: string
): Promise<SynonymMap> {
throw new Error("Not implemented for browser.");
}
30 changes: 30 additions & 0 deletions sdk/search/search-documents/src/synonymMapHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { SynonymMap } from "./serviceModels";
import { promisify } from "util";
import * as fs from "fs";
const readFileAsync = promisify(fs.readFile);

/**
* Helper method to create a SynonymMap object. This is a NodeJS only method.
*
* @param name - Name of the SynonymMap.
* @param filePath - Path of the file that contains the Synonyms (seperated by new lines)
* @returns SynonymMap object
*/
export async function createSynonymMapFromFile(
name: string,
filePath: string
): Promise<SynonymMap> {
const synonyms: string[] = (await readFileAsync(filePath, "utf-8"))
.replace(/\r/g, "")
.split("\n")
.map((line) => line.trim())
.filter(Boolean);

return {
name,
synonyms
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { assert } from "chai";
import { createSynonymMapFromFile } from "../../../src/synonymMapHelper.browser";

describe("synonymmap", () => {
it("create synonymmap from file(browser)", async function() {
let errorThrown = false;
try {
await createSynonymMapFromFile("my-synonym-map-1", "./test/internal/synonymMap.txt");
} catch (ex) {
errorThrown = true;
}
assert.isTrue(errorThrown, "Expected createSynonymMapFromFile to fail with an exception");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { assert } from "chai";
import { createSynonymMapFromFile } from "../../../src";
import { SynonymMap } from "../../../src/serviceModels";

describe("synonymmap", () => {
it("create synonymmap from file(node)", async function() {
const synonymMap: SynonymMap = await createSynonymMapFromFile(
"my-synonym-map-1",
"./test/internal/synonymMap.txt"
);
assert.equal(synonymMap.name, "my-synonym-map-1");
assert.equal(synonymMap.synonyms.length, 2);
assert.equal(synonymMap.synonyms[0], "United States, United States of America => USA");
assert.equal(synonymMap.synonyms[1], "Washington, Wash. => WA");
});
});
2 changes: 2 additions & 0 deletions sdk/search/search-documents/test/internal/synonymMap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
United States, United States of America => USA
Washington, Wash. => WA

0 comments on commit 20321ef

Please sign in to comment.