Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience Method to create a synonymmap object #16054

Merged
merged 3 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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