Skip to content

Commit

Permalink
Fix for url translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kh4l1d64 committed Feb 26, 2024
1 parent c988eaa commit 354c309
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/modules/aiTools/ai-tools.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class AiToolsService {
text: string
) {
try {
const urlRegex = /(https?:\/\/[^\s]+)|(www\.[^\s]+)/g;
const urls = text.match(urlRegex) || [];

const placeHolder = "9kBjf325" //placeholder which stays the same across languages after translation
const textWithoutUrls = text.replace(urlRegex, placeHolder)
let config = {
"language": {
"sourceLanguage": source,
Expand All @@ -89,7 +94,7 @@ export class AiToolsService {
}
let bhashiniConfig: any = await this.getBhashiniConfig('translation',config)

let textArray = text.split("\n")
let textArray = textWithoutUrls.split("\n")
for(let i=0;i<textArray.length;i++){
let response: any = await this.computeBhashini(
bhashiniConfig?.pipelineInferenceAPIEndPoint?.inferenceApiKey?.value,
Expand All @@ -112,8 +117,9 @@ export class AiToolsService {
}
textArray[i]=response?.pipelineResponse[0]?.output[0]?.target
}
const translatedText = textArray.join('\n').replace(new RegExp(placeHolder, 'g'), () => urls.shift() || '');
return {
text: textArray.join('\n'),
text: translatedText,
error: null
}
} catch(error){
Expand Down

0 comments on commit 354c309

Please sign in to comment.