Skip to content

Commit

Permalink
fix(transformation): change steps to combine lyrics and translation
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftaufiq committed Apr 13, 2023
1 parent 8c8345c commit 7eaf5d0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
Expand Up @@ -46,10 +46,7 @@ describe('googleTranslationAndRomanization', () => {
] as SourceTranslation[];

jest.unstable_mockModule('@vitalets/google-translate-api', () => ({
translate: jest.fn().mockReturnValue({
text: '',
raw: { sentences },
}),
translate: jest.fn().mockReturnValue({ raw: { sentences } }),
}));

const result = await firstValueFrom(
Expand All @@ -72,7 +69,16 @@ describe('googleTranslationAndRomanization', () => {
const lyrics = createRandomLinesResponse({ count: 1 });

jest.unstable_mockModule('@vitalets/google-translate-api', () => ({
translate: jest.fn().mockReturnValue({ text, raw: { sentences: [] } }),
translate: jest.fn().mockReturnValue({
raw: {
sentences: [
{
trans: text,
orig: lyrics.at(0)?.words,
},
],
},
}),
}));

const result = await firstValueFrom(
Expand Down
@@ -1,4 +1,7 @@
import { SrcTranslit as SourceTranslation } from '@vitalets/google-translate-api/dist/cjs/types.js';
import {
Sentence,
SrcTranslit as SourceTranslation,
} from '@vitalets/google-translate-api/dist/cjs/types.js';
import { from } from 'rxjs';
import { InitializationPipeFunction } from '../../interfaces/initialization-pipe-function.interface.js';

Expand All @@ -14,6 +17,13 @@ const isSourceTranslation = (value: unknown): value is SourceTranslation =>
typeof value === 'object' &&
'src_translit' in value;

const isSentence = (value: unknown): value is Sentence =>
value !== undefined &&
value !== null &&
typeof value === 'object' &&
'trans' in value &&
'orig' in value;

export const googleTranslationAndRomanization = ({
to,
romanize,
Expand All @@ -30,18 +40,40 @@ export const googleTranslationAndRomanization = ({

const lyrics = lines
.map((line) => line.words.split('\n').at(0))
.join('|');
.join(' \r ');

const translate = (await googleTranslateApi).translate;
const { text, raw } = await translate(lyrics, {
const { raw } = await translate(lyrics, {
to,
});

const translatedLyrics = text.split('|');
const lastSentence = raw.sentences.at(-1);
const romanizedLyrics = isSourceTranslation(lastSentence)
? lastSentence.src_translit.split('|')
: [];
const { translatedLyrics, romanizedLyrics } = raw.sentences.reduce(
(acc, val) => {
if (isSourceTranslation(val)) {
return {
translatedLyrics: acc.translatedLyrics,
romanizedLyrics: val.src_translit.split('\r'),
};
}

if (isSentence(val)) {
return {
romanizedLyrics: acc.romanizedLyrics,
translatedLyrics: [
...acc.translatedLyrics,
val.trans.replace(/\n/g, ''),
],
};
}

return acc;
},
{
translatedLyrics: [] as string[],
romanizedLyrics: [] as string[],
},
);

const translatedLines = lines.map((line, index) => {
const translatedLyric = translatedLyrics.at(index);
const romanizedLyric = romanizedLyrics.at(index);
Expand All @@ -54,14 +86,15 @@ export const googleTranslationAndRomanization = ({
romanizedLyric !== undefined &&
romanizedLyric.replace(/\s/g, '') !==
line.words.replace(/\s/g, '')
? romanizedLyric
? romanizedLyric.trim()
: [],
)
.concat(
showTranslation === true &&
translatedLyric !== undefined &&
translatedLyric !== line.words
? translatedLyric
translatedLyric.replace(/\s/g, '') !==
line.words.replace(/\s/g, '')
? translatedLyric.trim()
: [],
)
.join('\n'),
Expand Down

0 comments on commit 7eaf5d0

Please sign in to comment.