Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function DetectLangFromStream(feedStream) {
let posts = await ReadFeedStream(feedStream);
let bestGuessLanguage = 'eng';
if (!posts || !posts.length) {
return bestGuessLanguage;
}
let meta = posts[0].meta;
// language = meta.language
// guess the language using franc
let languageSums = {};
for (let post of posts.slice(0, 10)) {
let languageVector = franc.all(`${post.title} ${post.description}`);
for (let [language, score] of languageVector) {
if (!(language in languageSums)) {
languageSums[language] = 0;
}
languageSums[language] += score;
}
}
// see which language has the highest score
let languages = Object.entries(languageSums).sort((a, b) => b[1] - a[1]);
if (languages) {
bestGuessLanguage = languages[0][0];
}
return bestGuessLanguage;
}
ParsePodcast(podcastUrl, (err, podcastContents) => {
if (err) {
// sometimes rss feeds throw a 503 when being added and parsed immediately - in that case, just resolve to english
return resolve('eng');
}
// language in the feed metadata is only sometimes there - calculating via franc should be good enough for now
let languageSums = {};
for (let episode of podcastContents.episodes) {
let languageVector = franc.all(`${episode.title} ${episode.description}`);
for (let languageGuess of languageVector) {
if (languageGuess[0] in languageSums) {
languageSums[languageGuess[0]] += languageGuess[1];
} else {
languageSums[languageGuess[0]] = languageGuess[1];
}
}
}
// get largest languageSum
let max = 0;
let bestGuessLanguage = 'eng';
for (var language in languageSums) {
if (languageSums.hasOwnProperty(language)) {
if (languageSums[language] > max) {
max = languageSums[language];
bestGuessLanguage = language;