-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spoken-to-signed): add text normalization suggestion
- Loading branch information
Showing
28 changed files
with
492 additions
and
73 deletions.
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
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
Empty file.
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 |
---|---|---|
|
@@ -15,4 +15,5 @@ database-debug.log | |
firestore-debug.log | ||
|
||
coverage/ | ||
!.env | ||
.env | ||
.runtimeconfig.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Text-Normalization | ||
|
||
Normalizes the given text for sign language translation. | ||
|
||
Takes in a text that might contain misspellings, incorrect capitalization, | ||
missing hyphenation, numbers, units, or other phenomena requiring special | ||
vocalization. Returns the text normalized for sign language, with corrections in | ||
capitalization, spelling, and hyphenation, and special vocalizations for | ||
numbers and units. | ||
|
||
## Parameters: | ||
|
||
| Parameter | Type | Description | | ||
| --------- | -------- | ------------------------- | | ||
| `lang` | `string` | Language code of the text | | ||
| `text` | `string` | Text to be normalized | | ||
|
||
## Example Call: | ||
|
||
```bash | ||
curl "https://sign.mt/api/text-normalization?lang=en&text=test" | ||
``` | ||
|
||
## Response (Success, 200): | ||
|
||
```json | ||
{ | ||
"lang": "en", | ||
"text": "Test" | ||
} | ||
``` | ||
|
||
## Response (Failure, 400): | ||
|
||
``` | ||
{ | ||
"message": "Failure", | ||
"error": "Missing \"text\" query parameter" | ||
} | ||
``` |
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,97 @@ | ||
import {TextNormalizationEndpoint} from './controller'; | ||
import {setupFirebaseTestEnvironment} from '../firebase.extend-spec'; | ||
import {StringParam} from 'firebase-functions/lib/params/types'; | ||
|
||
const MockExpressRequest = require('mock-express-request'); | ||
const MockExpressResponse = require('mock-express-response'); | ||
|
||
describe('TextNormalizationEndpoint', () => { | ||
const testEnvironment = setupFirebaseTestEnvironment(); | ||
|
||
let controller: TextNormalizationEndpoint; | ||
beforeEach(() => { | ||
const key = new StringParam('mock-key'); | ||
controller = new TextNormalizationEndpoint(testEnvironment.database, key); | ||
spyOn(controller, 'normalize').and.returnValue(Promise.resolve('mock normalized text')); | ||
}); | ||
|
||
it('should error missing "lang"', async () => { | ||
const mockReq = new MockExpressRequest({ | ||
url: '/', | ||
params: {}, | ||
query: {text: 'hello'}, | ||
}); | ||
const mockRes = new MockExpressResponse({request: mockReq}); | ||
|
||
await expect(controller.request(mockReq, mockRes)).rejects.toThrow(new Error('Missing "from" query parameter')); | ||
}); | ||
|
||
it('should error missing "text"', async () => { | ||
const mockReq = new MockExpressRequest({ | ||
url: '/', | ||
params: {}, | ||
query: {lang: 'en'}, | ||
}); | ||
const mockRes = new MockExpressResponse({request: mockReq}); | ||
|
||
await expect(controller.request(mockReq, mockRes)).rejects.toThrow(new Error('Missing "text" query parameter')); | ||
}); | ||
|
||
async function normalizeExample() { | ||
const mockReq = new MockExpressRequest({ | ||
url: '/', | ||
params: {}, | ||
query: {lang: 'en', text: 'hello'}, | ||
}); | ||
const mockRes = new MockExpressResponse({request: mockReq}); | ||
await controller.request(mockReq, mockRes); | ||
|
||
return mockRes._getJSON(); | ||
} | ||
|
||
it('should normalize text with correct url parameters', async () => { | ||
const response = await normalizeExample(); | ||
|
||
expect(response).toEqual({ | ||
lang: 'en', | ||
text: 'mock normalized text,', | ||
}); | ||
}); | ||
|
||
it('should cache normalization response', async () => { | ||
const helloMd5 = '5d41402abc4b2a76b9719d911017c592'; | ||
const ref = testEnvironment.database.ref('/normalizations/en/' + helloMd5); | ||
const snapshotBefore = await ref.once('value'); | ||
expect(snapshotBefore.exists()).toBe(false); | ||
|
||
const response = await normalizeExample(); | ||
|
||
const snapshot = await ref.once('value'); | ||
expect(snapshot.exists()).toBe(true); | ||
const cachedValue = snapshot.val(); | ||
expect(cachedValue.input).toEqual('hello'); | ||
expect(cachedValue.counter).toEqual(1); | ||
expect(cachedValue.timestamp).toBeLessThanOrEqual(Date.now()); | ||
expect(cachedValue.timestamp).toBeGreaterThanOrEqual(Date.now() - 5000); | ||
expect(cachedValue.output).toEqual(response.text); | ||
}); | ||
|
||
it('should respond with cached response if exists', async () => { | ||
const helloMd5 = '5d41402abc4b2a76b9719d911017c592'; | ||
const ref = testEnvironment.database.ref('/normalizations/en/' + helloMd5); | ||
await ref.set({ | ||
input: 'hello', | ||
output: 'fake translation', | ||
counter: 1, | ||
timestamp: Date.now(), | ||
}); | ||
|
||
const response = await normalizeExample(); | ||
expect(response.text).toEqual('fake translation'); | ||
|
||
const snapshot = await ref.once('value'); | ||
expect(snapshot.exists()).toBe(true); | ||
const cachedValue = snapshot.val(); | ||
expect(cachedValue.counter).toEqual(2); | ||
}); | ||
}); |
Oops, something went wrong.