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

react-ui: Add support to display Chinese #713

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,21 @@ export const doConversation = (conversationRequest: ConversationRequest) => {
const match = msg.data.match(/b'([^']*)'/);
if (match && match[1] != "</s>") {
const extractedText = match[1];
result += extractedText;

// Check for the presence of \x hexadecimal
if (extractedText.includes("\\x")) {
// Decode Chinese (or other non-ASCII characters)
const decodedText = decodeEscapedBytes(extractedText);
result += decodedText;
} else {
result += extractedText;
}
} else if (!match) {
// Return data without pattern
result += msg?.data;
}
// Store back result if it is not null
if (result) {
store.dispatch(setOnGoingResult(result));
}
} catch (e) {
Expand Down Expand Up @@ -195,3 +209,13 @@ export const doConversation = (conversationRequest: ConversationRequest) => {
console.log(err);
}
};

// decode \x hexadecimal encoding
function decodeEscapedBytes(str: string): string {
// Convert the byte portion separated by \x into a byte array and decode it into a UTF-8 string
const byteArray: number[] = str
.split("\\x")
.slice(1)
.map((byte: string) => parseInt(byte, 16));
return new TextDecoder("utf-8").decode(new Uint8Array(byteArray));
}
Loading