File size: 2,890 Bytes
f0ff02c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Video data structure for recommendations
const videoData = [
  {
    id: 'XsLIkggkcw4',
    title: 'Farming Techniques 101',
    description: 'A quick overview of modern farming techniques to boost productivity.',
    keywords: ['farming techniques', 'modern farming', 'productivity', 'basics', 'introduction']
  },
  {
    id: 'QxK4YbPrWXk',
    title: 'Organic Farming',
    description: 'Learn how to switch to organic methods for sustainable agriculture.',
    keywords: ['organic', 'sustainable', 'natural farming', 'eco-friendly']
  },
  {
    id: 'Z9HAy9EYKKs',
    title: 'Irrigation Systems',
    description: 'Explore different irrigation systems and their benefits.',
    keywords: ['irrigation', 'water management', 'watering systems', 'farm water']
  },
  {
    id: 'XeNA6XdMoF8',
    title: 'Crop Rotation Strategies',
    description: 'Understanding the importance of crop rotation for soil health.',
    keywords: ['crop rotation', 'soil health', 'farming strategy', 'sustainable farming']
  },
  {
    id: 'L14woJZEJnk',
    title: 'Soil Fertility',
    description: 'Tips to improve soil fertility using natural and chemical methods.',
    keywords: ['soil', 'fertility', 'soil health', 'nutrients', 'fertilizers']
  }
];

class ChatBot {
  constructor() {
    this.chatHistory = [];
  }

  // Process user input and return relevant recommendations
  processInput(userInput) {
    const input = userInput.toLowerCase();
    const matches = [];

    // Search through videos and calculate relevance score
    videoData.forEach(video => {
      let score = 0;
      const searchText = `${video.title} ${video.description} ${video.keywords.join(' ')}`.toLowerCase();
      
      // Check if input terms appear in video metadata
      input.split(' ').forEach(term => {
        if (searchText.includes(term)) {
          score += 1;
        }
      });

      if (score > 0) {
        matches.push({
          ...video,
          score
        });
      }
    });

    // Sort by relevance score
    matches.sort((a, b) => b.score - a.score);

    // Generate response
    if (matches.length > 0) {
      const topMatches = matches.slice(0, 3).map(match => ({
        ...match,
        url: `https://www.youtube.com/watch?v=${match.id}`
      }));
      return {
        message: `Based on your interest, I recommend these videos:`,
        recommendations: topMatches
      };
    } else {
      return {
        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?",
        recommendations: []
      };
    }
  }

  // Add message to chat history
  addToHistory(message, isUser) {
    this.chatHistory.push({
      message,
      isUser,
      timestamp: new Date().toISOString()
    });
  }
}

// Initialize chatbot
const chatbot = new ChatBot();