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

Initial commit: Bing Spell Check Node #762

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions extensions/bing-spell-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Bing Spell Check

The Bing Spell Check extension allows you to automatically fix spelling mistakes in the user input, increasing the NLU classification accuracy.

---
## Connection ##

To use the extension, you will need a Bing API access.

----
## Node: Bing Spell Check

This Flow Node sends the user input to the Bing Spell Check API, and automatically applies all suggestions that have a confidence score higher than a given threshold.

**Fields:**

### Bing Connection

To configure the connection, provide the Bing API key.

### Threshold

Threshold defines the minimal confidence score to apply suggested fix.

### Storage Options

Storage Options allow you to store the result in Input or Context.
Binary file added extensions/bing-spell-check/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions extensions/bing-spell-check/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions extensions/bing-spell-check/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "bing-spell-check",
"version": "1.0.0",
"description": "The Bing Spell Check extension sends the user input to the Bing Spell Check API and returns a fixed version of it.",
"main": "build/module.js",
"scripts": {
"tsc": "tsc -p .",
"zip": "tar cfz bing-spell-check.tar.gz build/* package.json package-lock.json README.md icon.png",
"build": "npm run tsc && npm run zip"
},
"keywords": [
"Bing",
"typo",
"spellcheck",
"proofread"
],
"author": "Alex Rassokhin",
"license": "ISC",
"dependencies": {
"@cognigy/extension-tools": "^0.11.0",
"axios": "^0.21.4"
},
"devDependencies": {
"@types/node": "^14.14.28",
"typescript": "^4.9.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IConnectionSchema } from "@cognigy/extension-tools";

export const bingApiConnection: IConnectionSchema = {
type: "bing",
label: "Holds the API key for Bing.",
fields: [
{ fieldName: "key" }
]
};
12 changes: 12 additions & 0 deletions extensions/bing-spell-check/src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createExtension } from "@cognigy/extension-tools";
import { bingSpellCheck } from "./nodes/bingSpellCheck";
import { bingApiConnection } from "./connections/bingApiConnection";

export default createExtension({
nodes: [
bingSpellCheck
],
connections: [
bingApiConnection
]
});
153 changes: 153 additions & 0 deletions extensions/bing-spell-check/src/nodes/bingSpellCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { createNodeDescriptor, INodeFunctionBaseParams } from "@cognigy/extension-tools";
import axios from 'axios';

export interface IRunBingSpellCheck extends INodeFunctionBaseParams {
config: {
connection: {
key: string;
}
threshold: number;
storeLocation: string;
inputKey: string;
contextKey: string;
};
}

export const bingSpellCheck = createNodeDescriptor({
type: "bingSpellCheck",
defaultLabel: "Bing Spell Check",
preview: {
key: "cognigytext",
type: "text"
},
fields: [
{
key: "connection",
type: "connection",
label: "Bing Connection",
params: {
connectionType: "bing"
}
},
{
key: "threshold",
type: "slider",
label: "Threshold",
defaultValue: 0.5,
params: {
min: 0,
max: 1,
step: 0.1
}
},
{
key: "storeLocation",
type: "select",
label: "Where to store the result",
defaultValue: "input",
params: {
options: [
{
label: "Input",
value: "input"
},
{
label: "Context",
value: "context"
}
],
required: true
},
},
{
key: "inputKey",
type: "cognigyText",
label: "Input Key to store Result",
defaultValue: "proofreadInput",
condition: {
key: "storeLocation",
value: "input"
}
},
{
key: "contextKey",
type: "cognigyText",
label: "Context Key to store Result",
defaultValue: "proofreadInput",
condition: {
key: "storeLocation",
value: "context"
}
}
],
sections: [
{
key: "storage",
label: "Storage Options",
defaultCollapsed: true,
fields: [
"storeLocation",
"inputKey",
"contextKey"
]
}
],
form: [
{ type: "field", key: "connection" },
{ type: "field", key: "threshold" },
{ type: "section", key: "storage" },
],
function: async ({ cognigy, config }: IRunBingSpellCheck) => {
const { input, api } = cognigy;
const { connection, threshold, storeLocation, inputKey, contextKey } = config;
const { key } = connection;

try {
const response = await axios({
method: 'get',
url: `https://api.bing.microsoft.com/v7.0/spellcheck?mode=proof&mkt=${input.language}&text=q=${encodeURIComponent(input.text)}`,
headers: {
'Ocp-Apim-Subscription-Key': key,
'Accept': 'application/json'
}
});

let spellCheckResult = response.data;
let proofreadInput = input.text;

// Sort flaggedTokens by offset in descending order to avoid messing up the offsets
// while replacing text from the beginning to the end.
// spellCheckResult.flaggedTokens.sort((a, b) => b.offset - a.offset);

spellCheckResult.flaggedTokens.forEach(flaggedToken => {
const { offset, token, suggestions } = flaggedToken;
// Replace only if score is above threshold and if it's longer than "A" (because Bing seems to have a bug with single-character fixes)
if (suggestions[0].score > threshold && (suggestions[0].suggestion.length > 1 || suggestions[0].suggestion === "A")) {
const suggestion = suggestions[0].suggestion;
const startIndex = proofreadInput.indexOf(token, offset - token.length + 1);
if (startIndex !== -1) { // Only replace if the misspelled word was found
const endIndex = startIndex + token.length;
proofreadInput = proofreadInput.substring(0, startIndex) + suggestion + proofreadInput.substring(endIndex);
}
}
});

if (storeLocation === "context") {
api.addToContext(contextKey, proofreadInput, "simple");
} else {
// @ts-ignore
api.addToInput(inputKey, proofreadInput);
}

// @ts-ignore
// api.addToInput("spellCheckResult", response.data);
} catch (error) {
if (storeLocation === "context") {
api.addToContext(contextKey, error, "simple");
} else {
// @ts-ignore
api.addToInput(inputKey, error);
}
}
}
});
14 changes: 14 additions & 0 deletions extensions/bing-spell-check/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compileOnSave": true,
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"module": "commonjs",
"rootDir": "src",
"outDir": "build",
"sourceMap": true,
"moduleResolution": "node"//,
//"watch": false
}
}
32 changes: 32 additions & 0 deletions extensions/bing-spell-check/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"rules": {
"prefer-for-of": true,
"only-arrow-functions": [true, "allow-declarations", "allow-named-functions"],
"no-var-keyword": true,
"no-var-requires": false,
"comment-format": [true, "check-space"],
"no-duplicate-variable": true,
"no-eval": false,
"no-internal-module": true,
"no-trailing-whitespace": true,
"one-line": [true, "check-open-brace", "check-whitespace", "check-catch", "check-finally", "check-else"],
"semicolon": [true, "always"],
"triple-equals": [true, "allow-null-check"],
"typedef-whitespace": [true, {
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}],
"typedef": [true, "call-signature", "parameter", "member-variable-declaration"],
"variable-name": [true, "ban-keywords"],
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}