aiden-shared-calculations-unified 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @fileoverview Aggregates the total number of posts per asset (ticker).
3
+ * This data can be used for a time-series trend (AssetPostsTrend) or
4
+ * for a "Top Tickers" card.
5
+ * This calculation ONLY uses the social post insights context.
6
+ */
7
+
8
+ class SocialAssetPostsTrend {
9
+ constructor() {
10
+ this.postsByTicker = {};
11
+ this.processed = false; // Flag to ensure this runs only once
12
+ }
13
+
14
+ /**
15
+ * @param {null} todayPortfolio - Not used.
16
+ * ... (other params not used) ...
17
+ * @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
18
+ * @param {object} yesterdaySocialPostInsights - Not used.
19
+ */
20
+ async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
21
+
22
+ if (this.processed || !todaySocialPostInsights) {
23
+ return;
24
+ }
25
+ this.processed = true;
26
+
27
+ const posts = Object.values(todaySocialPostInsights);
28
+
29
+ for (const post of posts) {
30
+ if (!post.tickers || !Array.isArray(post.tickers)) continue;
31
+
32
+ for (const ticker of post.tickers) {
33
+ // Ensure ticker is a string and valid
34
+ const validTicker = String(ticker).trim();
35
+ if (validTicker) {
36
+ this.postsByTicker[validTicker] = (this.postsByTicker[validTicker] || 0) + 1;
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ async getResult() {
43
+ // Returns an object like: { "AAPL": 15, "TSLA": 22 }
44
+ return this.postsByTicker;
45
+ }
46
+
47
+ reset() {
48
+ this.postsByTicker = {};
49
+ this.processed = false;
50
+ }
51
+ }
52
+
53
+ module.exports = SocialAssetPostsTrend;
@@ -0,0 +1,103 @@
1
+ /**
2
+ * @fileoverview Finds the top mentioned words in all posts for the day,
3
+ * filtering out common stop words, and calculates the trend from yesterday.
4
+ * This calculation ONLY uses the social post insights context.
5
+ */
6
+
7
+ class SocialTopMentionedWords {
8
+ constructor() {
9
+ this.todayWordCounts = {};
10
+ this.yesterdayWordCounts = {};
11
+ this.processed = false;
12
+
13
+ // A small, representative list of stop words.
14
+ // In a real implementation, this list would be much larger.
15
+ this.stopWords = new Set([
16
+ 'the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
17
+ 'have', 'has', 'had', 'do', 'does', 'did', 'but', 'if', 'or', 'and',
18
+ 'of', 'at', 'by', 'for', 'with', 'about', 'to', 'from', 'in', 'on',
19
+ 'it', 'i', 'me', 'my', 'you', 'your', 'he', 'she', 'we', 'they', 'them',
20
+ 'this', 'that', 'these', 'those', 'what', 'which', 'who', 'whom',
21
+ 'not', 'will', 'just', 'so', 'up', 'out', 'no', 'yes', 'all', 'any'
22
+ ]);
23
+ }
24
+
25
+ /**
26
+ * Helper to process posts and populate a word count map.
27
+ * @param {object} socialPostInsights - Map of { [postId]: postData }
28
+ * @param {object} targetCountMap - The object to store word counts.
29
+ */
30
+ _countWords(socialPostInsights, targetCountMap) {
31
+ if (!socialPostInsights) return;
32
+ const posts = Object.values(socialPostInsights);
33
+
34
+ for (const post of posts) {
35
+ const text = post.fullText?.toLowerCase() || '';
36
+ if (!text) continue;
37
+
38
+ // Get all words 3+ chars long
39
+ const words = text.match(/\b[a-z]{3,}\b/g) || [];
40
+
41
+ for (const word of words) {
42
+ if (!this.stopWords.has(word)) {
43
+ targetCountMap[word] = (targetCountMap[word] || 0) + 1;
44
+ }
45
+ }
46
+ }
47
+ }
48
+
49
+ /**
50
+ * @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
51
+ * @param {object} yesterdaySocialPostInsights - Map of { [postId]: postData } for yesterday.
52
+ */
53
+ async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
54
+
55
+ if (this.processed) {
56
+ return;
57
+ }
58
+ this.processed = true;
59
+
60
+ // Process today's posts
61
+ this._countWords(todaySocialPostInsights, this.todayWordCounts);
62
+
63
+ // Process yesterday's posts for trend calculation
64
+ this._countWords(yesterdaySocialPostInsights, this.yesterdayWordCounts);
65
+ }
66
+
67
+ async getResult() {
68
+ if (Object.keys(this.todayWordCounts).length === 0) {
69
+ return {};
70
+ }
71
+
72
+ const totalTodayWords = Object.values(this.todayWordCounts).reduce((a, b) => a + b, 0);
73
+
74
+ const topWords = Object.entries(this.todayWordCounts)
75
+ .sort(([, countA], [, countB]) => countB - countA)
76
+ .slice(0, 50); // Return the Top 50
77
+
78
+ const result = topWords.map(([term, count]) => {
79
+ const yesterdayCount = this.yesterdayWordCounts[term] || 0;
80
+ const trend = count - yesterdayCount;
81
+ const percent = (count / totalTodayWords) * 100;
82
+
83
+ return {
84
+ term,
85
+ count,
86
+ percent: parseFloat(percent.toFixed(2)),
87
+ trend
88
+ };
89
+ });
90
+
91
+ // Returns an array like:
92
+ // [ { term: "btc", count: 150, percent: 5.2, trend: 25 }, ... ]
93
+ return { top_words: result };
94
+ }
95
+
96
+ reset() {
97
+ this.todayWordCounts = {};
98
+ this.yesterdayWordCounts = {};
99
+ this.processed = false;
100
+ }
101
+ }
102
+
103
+ module.exports = SocialTopMentionedWords;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @fileoverview Aggregates counts of all topics extracted by the AI
3
+ * from social posts for the day. Used to build a "Topic Interest Area" chart.
4
+ * This calculation ONLY uses the social post insights context.
5
+ */
6
+
7
+ class SocialTopicInterestEvolution {
8
+ constructor() {
9
+ this.topicCounts = {};
10
+ this.processed = false;
11
+ }
12
+
13
+ /**
14
+ * @param {null} todayPortfolio - Not used.
15
+ * ... (other params not used) ...
16
+ * @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
17
+ * @param {object} yesterdaySocialPostInsights - Not used.
18
+ */
19
+ async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
20
+
21
+ if (this.processed || !todaySocialPostInsights) {
22
+ return;
23
+ }
24
+ this.processed = true;
25
+
26
+ const posts = Object.values(todaySocialPostInsights);
27
+
28
+ for (const post of posts) {
29
+ // Topics are in post.sentiment.topics
30
+ const topics = post.sentiment?.topics;
31
+ if (!topics || !Array.isArray(topics)) continue;
32
+
33
+ for (const topic of topics) {
34
+ // Normalize topic for consistent aggregation
35
+ const normalizedTopic = String(topic).toLowerCase().trim();
36
+ if (normalizedTopic) {
37
+ this.topicCounts[normalizedTopic] = (this.topicCounts[normalizedTopic] || 0) + 1;
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ async getResult() {
44
+ // Returns an object like: { "earnings": 45, "cpi": 12, "fed": 22 }
45
+ return this.topicCounts;
46
+ }
47
+
48
+ reset() {
49
+ this.topicCounts = {};
50
+ this.processed = false;
51
+ }
52
+ }
53
+
54
+ module.exports = SocialTopicInterestEvolution;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @fileoverview Tracks daily mentions of a pre-defined list of keywords.
3
+ * This calculation ONLY uses the social post insights context.
4
+ */
5
+
6
+ class SocialWordMentionsTrend {
7
+ constructor() {
8
+ // A pre-defined list of keywords to track
9
+ this.KEYWORDS = [
10
+ 'inflation', 'fed', 'fomc', 'cpi', 'earnings',
11
+ 'recession', 'bullish', 'bearish', 'buy', 'sell',
12
+ 'war', 'acquisition', 'ai'
13
+ ];
14
+ this.mentions = {};
15
+ this.processed = false;
16
+ }
17
+
18
+ /**
19
+ * @param {null} todayPortfolio - Not used.
20
+ * ... (other params not used) ...
21
+ * @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
22
+ * @param {object} yesterdaySocialPostInsights - Not used.
23
+ */
24
+ async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
25
+
26
+ if (this.processed || !todaySocialPostInsights) {
27
+ return;
28
+ }
29
+ this.processed = true;
30
+
31
+ // Initialize all keywords with 0 counts
32
+ this.mentions = this.KEYWORDS.reduce((acc, key) => {
33
+ acc[key] = 0;
34
+ return acc;
35
+ }, {});
36
+
37
+ const posts = Object.values(todaySocialPostInsights);
38
+
39
+ for (const post of posts) {
40
+ const text = post.fullText?.toLowerCase() || '';
41
+ if (!text) continue;
42
+
43
+ for (const keyword of this.KEYWORDS) {
44
+ // Count once per post, even if mentioned multiple times
45
+ if (text.includes(keyword)) {
46
+ this.mentions[keyword]++;
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ async getResult() {
53
+ // Returns an object like: { "inflation": 5, "fed": 12, "ai": 30 }
54
+ return this.mentions;
55
+ }
56
+
57
+ reset() {
58
+ this.mentions = {};
59
+ this.processed = false;
60
+ }
61
+ }
62
+
63
+ module.exports = SocialWordMentionsTrend;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [