Spaces:
Running
Running
// 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(); |