sikeaditya commited on
Commit
b8ecdad
·
verified ·
1 Parent(s): f78a214

Create chatbot.js

Browse files
Files changed (1) hide show
  1. chatbot.js +97 -0
chatbot.js ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Video data structure for recommendations
2
+ const videoData = [
3
+ {
4
+ id: 'XsLIkggkcw4',
5
+ title: 'Farming Techniques 101',
6
+ description: 'A quick overview of modern farming techniques to boost productivity.',
7
+ keywords: ['farming techniques', 'modern farming', 'productivity', 'basics', 'introduction']
8
+ },
9
+ {
10
+ id: 'QxK4YbPrWXk',
11
+ title: 'Organic Farming',
12
+ description: 'Learn how to switch to organic methods for sustainable agriculture.',
13
+ keywords: ['organic', 'sustainable', 'natural farming', 'eco-friendly']
14
+ },
15
+ {
16
+ id: 'Z9HAy9EYKKs',
17
+ title: 'Irrigation Systems',
18
+ description: 'Explore different irrigation systems and their benefits.',
19
+ keywords: ['irrigation', 'water management', 'watering systems', 'farm water']
20
+ },
21
+ {
22
+ id: 'XeNA6XdMoF8',
23
+ title: 'Crop Rotation Strategies',
24
+ description: 'Understanding the importance of crop rotation for soil health.',
25
+ keywords: ['crop rotation', 'soil health', 'farming strategy', 'sustainable farming']
26
+ },
27
+ {
28
+ id: 'L14woJZEJnk',
29
+ title: 'Soil Fertility',
30
+ description: 'Tips to improve soil fertility using natural and chemical methods.',
31
+ keywords: ['soil', 'fertility', 'soil health', 'nutrients', 'fertilizers']
32
+ }
33
+ ];
34
+
35
+ class ChatBot {
36
+ constructor() {
37
+ this.chatHistory = [];
38
+ }
39
+
40
+ // Process user input and return relevant recommendations
41
+ processInput(userInput) {
42
+ const input = userInput.toLowerCase();
43
+ const matches = [];
44
+
45
+ // Search through videos and calculate relevance score
46
+ videoData.forEach(video => {
47
+ let score = 0;
48
+ const searchText = `${video.title} ${video.description} ${video.keywords.join(' ')}`.toLowerCase();
49
+
50
+ // Check if input terms appear in video metadata
51
+ input.split(' ').forEach(term => {
52
+ if (searchText.includes(term)) {
53
+ score += 1;
54
+ }
55
+ });
56
+
57
+ if (score > 0) {
58
+ matches.push({
59
+ ...video,
60
+ score
61
+ });
62
+ }
63
+ });
64
+
65
+ // Sort by relevance score
66
+ matches.sort((a, b) => b.score - a.score);
67
+
68
+ // Generate response
69
+ if (matches.length > 0) {
70
+ const topMatches = matches.slice(0, 3).map(match => ({
71
+ ...match,
72
+ url: `https://www.youtube.com/watch?v=${match.id}`
73
+ }));
74
+ return {
75
+ message: `Based on your interest, I recommend these videos:`,
76
+ recommendations: topMatches
77
+ };
78
+ } else {
79
+ return {
80
+ message: "I couldn't find specific videos matching your query. Could you please try rephrasing or ask about specific farming topics like soil, irrigation, or organic farming?",
81
+ recommendations: []
82
+ };
83
+ }
84
+ }
85
+
86
+ // Add message to chat history
87
+ addToHistory(message, isUser) {
88
+ this.chatHistory.push({
89
+ message,
90
+ isUser,
91
+ timestamp: new Date().toISOString()
92
+ });
93
+ }
94
+ }
95
+
96
+ // Initialize chatbot
97
+ const chatbot = new ChatBot();