forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <xirzec@xirzec.com> * Fix for PR Comments Co-authored-by: Jeff Fisher <xirzec@xirzec.com>
- Loading branch information
1 parent
8236d61
commit 20321ef
Showing
8 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
sdk/search/search-documents/src/synonymMapHelper.browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
17 changes: 17 additions & 0 deletions
17
sdk/search/search-documents/test/internal/browser/synonymMap.browser.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
sdk/search/search-documents/test/internal/node/synonymMap.node.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
United States, United States of America => USA | ||
Washington, Wash. => WA |