-
Notifications
You must be signed in to change notification settings - Fork 369
POC for showPointLabels with TeX #3768
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
Draft
ivyolamit
wants to merge
2
commits into
showlabels/02-renderer-and-flag
Choose a base branch
from
showlabels/02-renderer-and-flag-poc-for-tex
base: showlabels/02-renderer-and-flag
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| --- |
This file contains hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
| import {generateSpokenMathDetails} from "./spoken-math"; | ||
|
|
||
| describe("generateSpokenMathDetails", () => { | ||
| it("converts TeX to spoken language (root, fraction)", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails( | ||
| "$\\sqrt{\\frac{1}{2}}$", | ||
| ); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("StartRoot one half EndRoot"); | ||
| }); | ||
|
|
||
| it("converts TeX to spoken language (exponent)", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("$x^{2}$"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("x Superscript 2"); | ||
| }); | ||
|
|
||
| it("converts TeX to spoken language (negative)", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("$-2$"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("negative 2"); | ||
| }); | ||
|
|
||
| it("converts TeX to spoken language (subtraction)", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("$2-1$"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("2 minus 1"); | ||
| }); | ||
|
|
||
| it("converts TeX to spoken language (normal words)", async () => { | ||
| // Arrange, Act | ||
| const convertedString = | ||
| await generateSpokenMathDetails("$\\text{square b}$"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("square b"); | ||
| }); | ||
|
|
||
| it("converts TeX to spoken language (random letters)", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("$cat$"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("c a t"); | ||
| }); | ||
|
|
||
| it("keeps non-math text as is", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails( | ||
| "Circle with radius $\\frac{1}{2}$ units", | ||
| ); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("Circle with radius one half units"); | ||
| }); | ||
|
|
||
| it("reads dollar signs as dollars inside tex", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails( | ||
| "This sandwich costs ${$}12.34$", | ||
| ); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("This sandwich costs dollar sign 12.34"); | ||
| }); | ||
|
|
||
| it("reads dollar signs as dollars outside tex", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails( | ||
| "This sandwich costs \\$12.34", | ||
| ); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("This sandwich costs $12.34"); | ||
| }); | ||
|
|
||
| it("reads curly braces", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("Hello}{"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("Hello}{"); | ||
| }); | ||
|
|
||
| it("reads backslashes", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("\\"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("\\"); | ||
| }); | ||
|
|
||
| it("reads lone dollar signs as regular dollar signs", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("$50"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("$50"); | ||
| }); | ||
|
|
||
| it("reads lone escaped dollar signs in text as regular dollar signs", async () => { | ||
| // Arrange, Act | ||
| const convertedString = await generateSpokenMathDetails("\\$50"); | ||
|
|
||
| // Assert | ||
| expect(convertedString).toBe("$50"); | ||
| }); | ||
| }); |
This file contains hidden or 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,48 @@ | ||
| import {SpeechRuleEngine} from "@khanacademy/mathjax-renderer"; | ||
|
|
||
| import {mathOnlyParser} from "../widgets/interactive-graphs/utils"; | ||
|
|
||
| /** | ||
| * Given a string that may contain math within TeX represented by $...$, | ||
| * returns the spoken math equivalent using the SpeechRuleEngine. | ||
| * | ||
| * Example: "Circle with radius $\frac{1}{2}$" ==> "Circle with radius one half" | ||
| * | ||
| * NOTE(LEMS): This is duplicated from perseus-editor's | ||
| * `interactive-graph-editor/locked-figures/util.ts` so it can run in the | ||
| * renderer (perseus can't import from perseus-editor). A follow-up will | ||
| * migrate the editor to import this copy and delete its own. | ||
| */ | ||
| export async function generateSpokenMathDetails( | ||
| mathString: string, | ||
| ): Promise<string> { | ||
| const engine = await SpeechRuleEngine.setup("en"); | ||
| let convertedSpeech = ""; | ||
|
|
||
| // All the information we need is in the first section, | ||
| // whether it's typed as "blockmath" or "paragraph" | ||
| const parsedContent = mathOnlyParser(mathString); | ||
|
|
||
| // If it's a paragraph, we need to iterate through the sections | ||
| // to look for individual math blocks. | ||
| for (const piece of parsedContent) { | ||
| switch (piece.type) { | ||
| case "math": | ||
| convertedSpeech += engine.texToSpeech(piece.content); | ||
| break; | ||
| case "specialCharacter": | ||
| // We don't want the backslash from special character | ||
| // to show up in the generated aria label. | ||
| convertedSpeech += | ||
| piece.content.length > 1 | ||
| ? piece.content.slice(1) | ||
| : piece.content; | ||
| break; | ||
| default: | ||
| convertedSpeech += piece.content; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return convertedSpeech; | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are hardcoding the
"en"locale here, which is not right. It was okay to hardcode"en"when this function was only used in the editor, because the generated labels would be saved in the content and go through the normal translation process. However, if we are generating math labels dynamically on the learner's computer, we need to use the appropriate locale.This is also going to make a web request for a JSON file that defines the spoken English for various math symbols. We have an architectural rule in Perseus that Perseus doesn't make web requests.
I think what what we need to do is generate spoken labels in the editor like we did for locked labels. We'll need to save the spoken labels and visible labels (which may include TeX) separately in the content JSON.
Alternatively, we could decide that we won't support visible labels with TeX for this first pass.