All files parser.ts

100% Statements 43/43
88.89% Branches 16/18
100% Functions 12/12
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 1451x   1x                                                             1x             7x     1x 9x 9x 9x 7x               9x 7x                   3x 3x 3x 2x                     2x 1x   1x         1x         4x 4x 4x 3x 3x 3x 1x   2x 2x 1x 1x     1x     1x 1x     1x       1x 1x     2x 1x         1x       1x                        
import * as watson from 'watson-developer-cloud';
 
const confidencethreshold = 0.55;
 
export interface ICredentials {
  toneAnalyzer: {
    username: string;
    password: string;
  };
  nlAnalyzer: {
    username: string;
    password: string;
  };
}
 
export interface ITone {
  tone_id: string;
  tone_name: string;
  score: number;
}
 
export interface IParsedTone {
  watsonTone: {
    strongestTone: ITone;
    allTones: ITone[];
  };
}
 
export interface IParsedSentiment {
  label: string;
  score: number;
}
 
export class Parser {
  private naturalLanguageAnalyzer!: watson.NaturalLanguageUnderstandingV1;
  private toneAnalyzer!: watson.ToneAnalyzerV3;
 
  private watsonCredentials: ICredentials;
 
  constructor() {
    this.watsonCredentials = {} as ICredentials;
  }
 
  public setCredentials(credentials: ICredentials) {
    this.watsonCredentials = credentials;
    Eif (credentials) {
      if (credentials.nlAnalyzer) {
        this.naturalLanguageAnalyzer = new watson.NaturalLanguageUnderstandingV1({
          username: credentials.nlAnalyzer.username, // tslint:disable-next-line:object-literal-sort-keys
          password: credentials.nlAnalyzer.password,
          version: '2018-04-05',
          url: 'https://gateway.watsonplatform.net/natural-language-understanding/api/',
        });
      }
 
      if (credentials.toneAnalyzer) {
        this.toneAnalyzer = new watson.ToneAnalyzerV3({
          username: credentials.toneAnalyzer.username, // tslint:disable-next-line:object-literal-sort-keys
          password: credentials.toneAnalyzer.password,
          version: 'v3',
          version_date: '2016-05-19',
        });
      }
    }
  }
 
  public parseSentiment(sentence: string): Promise<IParsedSentiment> {
    return new Promise<IParsedSentiment>((resolve: any, reject: any) => {
      if (this.watsonCredentials!.nlAnalyzer) {
        this.naturalLanguageAnalyzer.analyze(
          {
            text: sentence, // tslint:disable-next-line:object-literal-sort-keys
            features: {
              concepts: {},
              keywords: {},
              sentiment: { document: true },
            },
            language: 'en',
          },
          (err: Error, response: any) => {
            if (err) {
              reject(err);
            } else {
              resolve(response.sentiment.document as IParsedSentiment);
            }
          },
        );
      } else {
        reject(new Error('You must set credentials in order to use the Watson Natural Language Credentials'));
      }
    });
  }
 
  public parseEmotion(sentence: string): Promise<IParsedTone> {
    return new Promise<IParsedTone>((resolve: any, reject: any) => {
      if (this.watsonCredentials!.toneAnalyzer) {
        const parsedSentiment = {} as IParsedTone;
        this.toneAnalyzer.tone({ text: sentence }, (err: Error, tone: any) => {
          if (err) {
            reject(err);
          } else {
            let resolved = false;
            tone.document_tone.tone_categories.forEach((tonecategory: any) => {
              Eif (tonecategory.category_id === 'emotion_tone') {
                const maxConfidence = Math.max.apply(
                  Math,
                  tonecategory.tones.map((examinedTone: any) => {
                    return examinedTone.score;
                  }),
                );
                const strongestTone = tonecategory.tones.find((examinedTone: any) => {
                  return examinedTone.score === maxConfidence;
                });
                // tslint:disable-next-line:no-console
                parsedSentiment.watsonTone = {
                  allTones: tonecategory.tones as ITone[],
                  strongestTone: strongestTone as ITone,
                };
                resolved = true;
                resolve(parsedSentiment);
              }
            });
            if (!resolved) {
              reject(new Error('Unknown problem. Possible no tone categories.'));
            }
          }
        });
      } else {
        reject(new Error('You must set credentials in order to use the Watson Natural Language Credentials'));
      }
    });
  }
}
 
/*const inspect = require('unist-util-inspect');
const unified = require('unified');
const english = require('retext-english');
const sentiment = require('retext-sentiment');
 
 
const processor = unified()
	.use(english)
    .use(sentiment);
    */