Skip to content

Commit

Permalink
Merge pull request #41 from anzharip/refactor/fix-lint-warning
Browse files Browse the repository at this point in the history
Refactor/fix lint warning
  • Loading branch information
anzharip authored Nov 22, 2020
2 parents b3c69cb + e4aecf2 commit d608f86
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/interfaces/summary.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { google } from "@google-cloud/language/build/protos/protos";

export interface Summary {
wordFrequency: [string, number][];
replyToStatusId: string;
sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse;
}
19 changes: 15 additions & 4 deletions src/outgoing.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { Summary } from "./interfaces/summary.interface";
import { logger } from "./utility/logger";
import { twitterClient } from "./utility/twitter/twitter-client";

export async function sendAnswer(summary: any): Promise<void> {
export async function sendAnswer(summary: Summary): Promise<void> {
const client = twitterClient();
const status = summary.wordFrequency
.splice(0, 5)
.map((element: any) => element[0])
.map((element: [string, number]) => element[0])
.join(", ");
if (summary.sentiment.documentSentiment === null) {
throw new Error("documentSentiment is null. ");
}
if (summary.sentiment.documentSentiment === undefined) {
throw new Error("documentSentiment is undefined. ");
}
const sentimentScore = summary.sentiment.documentSentiment.score || 0;
const sentimentScoreRounded = Number.parseFloat(sentimentScore).toFixed(2);
const sentimentScoreRounded = Number.parseFloat(
String(sentimentScore)
).toFixed(2);
const magnitudeScore = summary.sentiment.documentSentiment.magnitude || 0;
const magnitudeScoreRounded = Number.parseFloat(magnitudeScore).toFixed(2);
const magnitudeScoreRounded = Number.parseFloat(
String(magnitudeScore)
).toFixed(2);
let sentimentScoreString = "Neutral";
if (sentimentScore <= -0.25 && sentimentScore >= -1) {
sentimentScoreString = "Negative";
Expand Down
5 changes: 3 additions & 2 deletions src/utility/queue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Summary } from "../interfaces/summary.interface";
import { Tweet } from "../interfaces/twitter/tweet.interface";

export const queueQuestion: Tweet[] = [];
export const queueSummary: any[] = [];
export const queueReport: any[] = [];
export const queueSummary: Summary[] = [];
export const queueReport: unknown[] = [];
30 changes: 19 additions & 11 deletions src/utility/twitter/tweet-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sw from "stopword";
import TextCleaner from "text-cleaner";
import { Summary } from "../../interfaces/summary.interface";
import { Tweet } from "../../interfaces/twitter/tweet.interface";
import { WordFrequency } from "../../interfaces/word-frequency.interface";
import { axiosClient } from "../axios-retry-configuration";
import { analyseSentiment } from "../gcp/sentiment";
Expand All @@ -10,7 +12,9 @@ import { regexTwitterHandle, regexURL } from "../regex";
const token = process.env.TWITTER_BEARER_TOKEN || "";
const recentSearchURL = "https://api.twitter.com/2/tweets/search/recent";

export async function recentSearch(username: string) {
export async function recentSearch(
username: string
): Promise<{ length: number; data: [] }> {
// Edit query parameters below
const parameters = {
query: `from:${username} -is:retweet`,
Expand All @@ -30,17 +34,19 @@ export async function recentSearch(username: string) {
return response.data;
}

export async function generateWordFrequency(words: string[]) {
export async function generateWordFrequency(
words: string[]
): Promise<[string, number][]> {
if (words.length === 0) {
return;
throw new Error("The received words length is zero. ");
}
const wordFrequency: WordFrequency = {};
words.forEach((element: string) => {
// Keep track of occurences
if (Object.prototype.hasOwnProperty.call(wordFrequency, element)) {
wordFrequency[element] += 1;
wordFrequency[String(element)] += 1;
} else {
wordFrequency[element] = 1;
wordFrequency[String(element)] = 1;
}
});

Expand All @@ -53,13 +59,13 @@ export async function generateWordFrequency(words: string[]) {
});
}

export async function generateWordsArray(tweets: any) {
if (tweets.length === 0) {
return;
}
export async function generateWordsArray(tweets: {
length: number;
data: [];
}): Promise<string[]> {
let recentTweetsConcat = "";
if (tweets["data"]) {
tweets["data"].forEach((element: any) => {
tweets["data"].forEach((element: { text: string }) => {
if (element.text)
recentTweetsConcat = recentTweetsConcat + " " + element.text;
});
Expand All @@ -82,7 +88,9 @@ export async function generateWordsArray(tweets: any) {
]);
}

export async function generateSummary(question: any) {
export async function generateSummary(
question: Tweet
): Promise<Summary | void> {
try {
const recentTweets = await recentSearch(question.in_reply_to_user_id_str);
const wordsArray = await generateWordsArray(recentTweets);
Expand Down

0 comments on commit d608f86

Please sign in to comment.