This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
213 lines (182 loc) · 6.29 KB
/
app.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import * as chrono from "chrono-node";
import dayjs from "dayjs";
import weekOfYear from "dayjs/plugin/weekOfYear.js";
import weekYear from "dayjs/plugin/weekYear.js";
import advancedFormat from "dayjs/plugin/advancedFormat.js";
import dateFormat from "dateformat";
// Extending dayjs with required plugins
dayjs.extend(weekOfYear);
dayjs.extend(weekYear);
dayjs.extend(advancedFormat);
function speechDate(message) {
// Getting the current date and time using dayjs
const now = dayjs();
// Sample user message and parsing it with chrono
const user_message_date = chrono.parseDate(message);
const user_chrono = chrono.parse(message);
const message_date = chrono.parseDate(message);
// Keywords indicating the past timeframe
const past_keywords = ["past", "last"];
console.log(`-User message: ${message}`);
console.log(
`-Today: ${now.format("dddd MMMM Do YYYY")} / Week ${now.format("ww YYYY")}`
);
console.log(`-Message date: ${message_date}`);
function checkForPastKeywords(message) {
for (const keyword of past_keywords) {
if (message.includes(keyword)) {
return true;
}
}
return false;
}
// Function to process user messages based on certain keywords
function processUserBasedOnKeywords(message) {
// Check if the user message contains any past keywords
if (checkForPastKeywords(message)) {
// If past keywords are found, process the user message
processUserMessage(message);
console.log(
"-Past keywords found in the user message. Running the function..."
);
} else {
// If no past keywords are found, check for specific keyword "week"
if (message.includes("week")) {
// Output the week and year using the dayjs library
console.log(
`OUTPUT Week ${dayjs(user_message_date).week()} ${dayjs(
user_message_date
).year()}`
);
} else {
// If neither past keywords nor "week" keyword is found, process the user message chronologically
processUserChrono(user_chrono);
}
console.log("-No past keywords found in the user message.");
}
}
function processUserMessage(message) {
if (message.includes("day")) {
pastDays(message);
}
if (message.includes("week")) {
pastWeeks(message);
} else if (message.includes("month")) {
pastMonths(message);
} else if (message.includes("year")) {
pastYears(message);
} else {
console.log("-No matching keyword found in the user message.");
}
function pastDays(message) {
if (message.includes("days")) {
let total_days = dayjs().diff(dayjs(user_message_date), "day");
console.log("This day: " + dayjs().day());
console.log("Total days: " + total_days);
const daysArray = [];
for (let i = 0; i < total_days; i++) {
const day = dayjs(now).subtract(i, "day");
daysArray.push(
`${day.format("dddd MMMM Do YYYY")} / Week ${day.format("ww YYYY")}`
);
}
console.log(daysArray);
console.log("Contained days");
} else {
console.log(
`OUTPUT ${dayjs(user_message_date)} ${dayjs(user_message_date)}`
);
console.log("Contained day");
}
}
function pastWeeks(message) {
if (message.includes("weeks")) {
console.log("Contained weeks");
let total_weeks = dayjs().diff(dayjs(user_message_date), "week");
const weeksArray = [];
for (let i = 0; i < total_weeks; i++) {
const week = dayjs(now).subtract(i, "week");
weeksArray.push(`Week ${week.week()} ${week.year()}`);
}
console.log(weeksArray);
} else {
console.log("Contained week");
console.log(
`OUTPUT Week ${dayjs(user_message_date).week()} ${dayjs(
user_message_date
).year()}`
);
}
}
function pastMonths(message) {
if (message.includes("months")) {
let total_months = dayjs().diff(dayjs(user_message_date), "month");
console.log("-This month: " + dayjs().month());
console.log("-Total months: " + total_months);
const monthsArray = [];
for (let i = 0; i < total_months; i++) {
const month = dayjs(now).subtract(i, "month");
monthsArray.push(`${month.format("MMMM")} ${month.format("YYYY")}`);
}
console.log(monthsArray);
} else {
console.log(
`OUTPUT ${dayjs(user_message_date).format("MMMM")} ${dayjs(
user_message_date
).format("YYYY")}`
);
console.log("-Contained month");
}
}
function pastYears(message) {
if (message.includes("years")) {
let total_years = dayjs().year() - dayjs(user_message_date).year();
console.log("-This year: " + dayjs().year());
console.log("-Total years: " + total_years);
const yearsArray = [];
for (let i = 0; i < total_years; i++) {
const yearNumber = dayjs(now).subtract(i, "year").year();
yearsArray.push(yearNumber);
}
console.log(yearsArray);
console.log("-Contained years");
} else {
console.log(`OUTPUT ${dayjs(user_message_date).year()}`);
console.log("-Contained year");
}
}
}
function processUserChrono(user_chrono) {
for (const { start } of user_chrono) {
let conditionMet = false;
["day", "month", "year"].forEach((unit) => {
if (conditionMet) {
return;
}
const isCertain = start.isCertain(unit);
if (unit === "day" && isCertain) {
console.log(
`OUTPUT ${dayjs(message_date).format("dddd MMMM Do YYYY")}`
);
console.log("-Certain day");
conditionMet = true;
}
if (unit === "month" && isCertain) {
console.log(`OUTPUT ${dayjs(message_date).format("MMMM YYYY")}`);
console.log("-Certain month");
conditionMet = true;
}
if (unit === "year" && isCertain) {
console.log(`OUTPUT ${dayjs(message_date).format("YYYY")}`);
console.log("-Certain year");
conditionMet = true;
}
});
if (conditionMet) {
break;
}
}
}
processUserBasedOnKeywords(message);
}
export default speechDate;