forked from omroy07/AgriTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-chatbot.js
More file actions
210 lines (179 loc) · 5.84 KB
/
json-chatbot.js
File metadata and controls
210 lines (179 loc) · 5.84 KB
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
/**
* AgriTech JSON-based Chatbot Module
* Provides intelligent response matching with fuzzy search capabilities
*/
class JSONChatbot {
constructor() {
this.responses = [];
this.fallbackResponses = [];
this.isLoaded = false;
this.loadResponses();
}
/**
* Load responses from JSON file
*/
async loadResponses() {
try {
const response = await fetch('./chatbot-responses.json');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
this.responses = data.responses || [];
this.fallbackResponses = data.fallback_responses || [
"Sorry, I didn't understand that. Could you ask about farming topics?"
];
this.isLoaded = true;
console.log('✅ Chatbot responses loaded successfully');
} catch (error) {
console.error('❌ Failed to load chatbot responses:', error);
this.responses = [];
this.fallbackResponses = [
"I'm having trouble accessing my knowledge base. Please try asking about general farming topics!"
];
this.isLoaded = false;
}
}
/**
* Calculate similarity between two strings using Levenshtein distance
*/
calculateSimilarity(str1, str2) {
str1 = str1.toLowerCase().trim();
str2 = str2.toLowerCase().trim();
if (str1 === str2) return 1.0;
const longer = str1.length > str2.length ? str1 : str2;
const shorter = str1.length > str2.length ? str2 : str1;
if (longer.length === 0) return 1.0;
const distance = this.levenshteinDistance(longer, shorter);
return (longer.length - distance) / longer.length;
}
/**
* Calculate Levenshtein distance between two strings
*/
levenshteinDistance(str1, str2) {
const matrix = Array(str2.length + 1).fill(null).map(() => Array(str1.length + 1).fill(null));
for (let i = 0; i <= str1.length; i++) matrix[0][i] = i;
for (let j = 0; j <= str2.length; j++) matrix[j][0] = j;
for (let j = 1; j <= str2.length; j++) {
for (let i = 1; i <= str1.length; i++) {
const substitutionCost = str1[i - 1] === str2[j - 1] ? 0 : 1;
matrix[j][i] = Math.min(
matrix[j][i - 1] + 1, // insertion
matrix[j - 1][i] + 1, // deletion
matrix[j - 1][i - 1] + substitutionCost // substitution
);
}
}
return matrix[str2.length][str1.length];
}
/**
* Check if input contains key phrases from the query
*/
containsKeyPhrases(input, query) {
const inputWords = input.toLowerCase().split(/\s+/);
const queryWords = query.toLowerCase().split(/\s+/);
// Check for exact phrase match
if (input.toLowerCase().includes(query.toLowerCase())) {
return 1.0;
}
// Check for word matches
let matches = 0;
for (const queryWord of queryWords) {
if (queryWord.length > 2) { // Ignore short words
for (const inputWord of inputWords) {
if (inputWord.includes(queryWord) || queryWord.includes(inputWord)) {
matches++;
break;
}
}
}
}
return queryWords.length > 0 ? matches / queryWords.length : 0;
}
/**
* Find the best matching response for user input
*/
findBestMatch(userInput) {
if (!this.isLoaded || this.responses.length === 0) {
return this.getRandomFallback();
}
const input = userInput.toLowerCase().trim();
let bestMatch = null;
let bestScore = 0;
const threshold = 0.4; // Minimum similarity threshold
for (const responseObj of this.responses) {
const query = responseObj.query.toLowerCase();
// Calculate multiple similarity metrics
const exactMatch = input === query ? 1.0 : 0;
const containsMatch = input.includes(query) || query.includes(input) ? 0.8 : 0;
const levenshteinSimilarity = this.calculateSimilarity(input, query);
const keyPhraseMatch = this.containsKeyPhrases(input, query);
// Weighted score combining different matching methods
const score = Math.max(
exactMatch,
containsMatch,
levenshteinSimilarity * 0.7,
keyPhraseMatch * 0.6
);
if (score > bestScore && score >= threshold) {
bestScore = score;
bestMatch = responseObj;
}
}
return bestMatch ? bestMatch.response : this.getRandomFallback();
}
/**
* Get a random fallback response
* (Fixed: Handles empty or undefined fallbackResponses gracefully)
*/
getRandomFallback() {
if (!Array.isArray(this.fallbackResponses) || this.fallbackResponses.length === 0) {
return "Sorry, I didn't understand that.";
}
const randomIndex = Math.floor(Math.random() * this.fallbackResponses.length);
return this.fallbackResponses[randomIndex];
}
/**
* Get response for user input
*/
async getResponse(userInput) {
// Wait for responses to load if they haven't already
if (!this.isLoaded) {
await this.loadResponses();
}
const response = this.findBestMatch(userInput);
return response;
}
/**
* Add new response to the system (runtime only, doesn't persist)
*/
addResponse(query, response) {
this.responses.push({ query: query.toLowerCase(), response });
console.log('✅ Added new response for:', query);
}
/**
* Get all available queries (for debugging/admin purposes)
*/
getAllQueries() {
return this.responses.map(r => r.query);
}
/**
* Check if chatbot is ready
*/
isReady() {
return this.isLoaded;
}
/**
* Reload responses from JSON file
*/
async reload() {
console.log('🔄 Reloading chatbot responses...');
await this.loadResponses();
}
}
// Create and export chatbot instance
window.JSONChatbot = JSONChatbot;
// For Node.js compatibility
if (typeof module !== 'undefined' && module.exports) {
module.exports = JSONChatbot;
}