UNPKG

bilingual-summarizer

Version:

A powerful text summarization package for Arabic and English content with sentiment analysis and topic extraction

146 lines (145 loc) 4.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.analyzeSentiment = analyzeSentiment; exports.getSentimentLabel = getSentimentLabel; const sentiment_1 = __importDefault(require("sentiment")); const languageDetection_1 = require("../utils/languageDetection"); // Try to import sentiment-multilang or create a mock let multiSentiment; try { multiSentiment = require('sentiment-multilang'); } catch (error) { multiSentiment = { analyze: (text, options) => ({ score: 0, comparative: 0 }) }; } /** * Analyzes the sentiment of the given text based on its language * @param text The text to analyze * @returns An object with the sentiment score and comparative value */ function analyzeSentiment(text) { try { // Check if text is Arabic if ((0, languageDetection_1.isArabic)(text)) { return analyzeArabicSentiment(text); } else { return analyzeEnglishSentiment(text); } } catch (error) { console.error('Sentiment analysis error:', error); // Return neutral sentiment on error return { score: 0, comparative: 0 }; } } /** * Analyzes sentiment for English text * @param text The English text to analyze * @returns An object with the sentiment score and comparative value */ function analyzeEnglishSentiment(text) { try { // Use the sentiment library directly as a function // Ensure we handle the result properly even if undefined const result = (0, sentiment_1.default)(text); if (!result || typeof result !== 'object') { return { score: 0, comparative: 0 }; } return { score: result.score || 0, comparative: result.comparative || 0 }; } catch (error) { console.error('English sentiment analysis error:', error); return { score: 0, comparative: 0 }; } } /** * Analyzes sentiment for Arabic text * @param text The Arabic text to analyze * @returns An object with the sentiment score and comparative value */ function analyzeArabicSentiment(text) { try { // First try with sentiment-multilang which supports Arabic if (multiSentiment && typeof multiSentiment.analyze === 'function') { const result = multiSentiment.analyze(text, { language: 'ar' }); if (!result || typeof result !== 'object') { return { score: 0, comparative: 0 }; } return { score: result.score || 0, comparative: result.comparative || result.score / (text.split(/\s+/).length || 1) || 0 }; } // Fallback to basic sentiment analysis const basicResult = (0, sentiment_1.default)(text); if (!basicResult || typeof basicResult !== 'object') { return { score: 0, comparative: 0 }; } return { score: basicResult.score || 0, comparative: basicResult.comparative || 0 }; } catch (error) { console.error('Arabic sentiment analysis error:', error); // Use basic sentiment as another fallback try { const basicResult = (0, sentiment_1.default)(text); if (!basicResult || typeof basicResult !== 'object') { return { score: 0, comparative: 0 }; } return { score: basicResult.score || 0, comparative: basicResult.comparative || 0 }; } catch (innerError) { return { score: 0, comparative: 0 }; } } } /** * Maps a sentiment score to a human-readable label * @param score The sentiment score * @returns A sentiment label (positive, negative, or neutral) */ function getSentimentLabel(score) { if (score > 0) return 'positive'; if (score < 0) return 'negative'; return 'neutral'; }