analysis static method

SentimentResult analysis(
  1. String text, {
  2. Map<String, num>? customLang,
  3. bool emoji = false,
  4. String languageCode = 'en',
})

Analysis function

syntax analysis(String text,{Map customLang, bool emoji = false, String languageCode})

return Map<String, dynamic>

example:

 final sentiment = Sentiment();
 print(sentiment.analysis('i hate you piece of shit 💩'));
// {score: -7, comparative: -1.1666666666666667, words: [i, hate, you, piece, of, shit], good words: [], badword: [[hate, -3], [shit, -4]]}

Implementation

static SentimentResult analysis(
  String text, {
  Map<String, num>? customLang,
  bool emoji = false,
  String languageCode = 'en',
}) {
  try {
    if (text.isEmpty) throw ('The provided text mus not be empty.');
    var sentiments = <String, num>{};
    if (emoji) sentiments.addAll(emojis);
    if (customLang == null) {
      switch (languageCode) {

        /// english
        case 'en':
          sentiments.addAll(en);
          break;

        /// italian
        case 'it':
          sentiments.addAll(it);
          break;

        /// french
        case 'fr':
          sentiments.addAll(fr);
          break;

        /// german
        case 'de':
          sentiments.addAll(de);
          break;
        default:
          throw ('The provided language code is not supported.');
      }
    } else {
      sentiments.addAll(customLang);
    }
    var score = 0.0;
    var goodWords = <String, num>{}, badWords = <String, num>{};
    var wordlist = emoji
        ? text
            .toLowerCase()
            .replaceAll('\n', ' ')
            .replaceAll('s\s+', ' ')
            .replaceAll(RegExp(r'[.,\/#!?$%\^&\*;:{}=_`\"~()]'), '')
            .trim()
            .split(' ')
        : text
            .toLowerCase()
            .replaceAll('\n', ' ')
            .replaceAll('s\s+', ' ')
            .replaceAll(RegExp(r'[.,\/#!?$%\^&\*;:{}=_`\"~()]'), '')
            .removemoji
            .trim()
            .split(' ');
    for (var i = 0; i < wordlist.length; i++) {
      sentiments.forEach((key, value) {
        if (key == wordlist[i]) {
          score += value;
          if (value < 0) {
            badWords[key] = value;
          } else {
            goodWords[key] = value;
          }
        }
      });
    }
    return SentimentResult(
        score: score,
        comparative: wordlist.isNotEmpty ? score / wordlist.length : 0,
        words: SentimentWordCategories(
            all: wordlist, good: goodWords, bad: badWords));
  } catch (e) {
    throw Exception(e);
  }
}