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 |
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cctype> // Function to convert a string to lowercase std::string toLowerCase(const std::string& str) { std::string lowerStr = str; std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower); return lowerStr; } // Function to simulate keyword detection in speech data bool detectKeyword(const std::string& speech, const std::vector<std::string>& keywords) { std::string lowerSpeech = toLowerCase(speech); for (const auto& keyword : keywords) { if (lowerSpeech.find(toLowerCase(keyword)) != std::string::npos) { return true; // Keyword detected } } return false; // No keywords detected } // Function to simulate basic frequency analysis of words in speech data void frequencyAnalysis(const std::string& speech) { std::string lowerSpeech = toLowerCase(speech); std::string word; std::map<std::string, int> wordCount; for (char c : lowerSpeech) { if (isalpha(c) || c == ' ') { word += c; } else if (c == ' ') { if (!word.empty()) { wordCount[word]++; word.clear(); } } } if (!word.empty()) { wordCount[word]++; } std::cout << "Word Frequency Analysis:\n"; for (const auto& [word, count] : wordCount) { std::cout << word << ": " << count << "\n"; } } int main() { // Example speech data std::string speech = "Hello, welcome to the simulation of speech processing algorithms. This simulation processes speech data."; // Keywords to detect std::vector<std::string> keywords = {"simulation", "speech"}; // Detect keywords in speech if (detectKeyword(speech, keywords)) { std::cout << "Keyword detected in speech!\n"; } else { std::cout << "No keywords detected in speech.\n"; } // Perform frequency analysis frequencyAnalysis(speech); return 0; } |
Explanation
- Helper Functions:
toLowerCase
Function:- Converts a given string to lowercase to facilitate case-insensitive comparisons.
AdvertisementdetectKeyword
Function:- Checks if any of the specified keywords are present in the speech data. Converts both the speech and keywords to lowercase for a case-insensitive search.
AdvertisementfrequencyAnalysis
Function:- Analyzes the frequency of each word in the speech data. It uses a simple map to count occurrences of each word.
- Main Function:
- Speech Data: Contains a sample speech text.
- Keywords: A list of keywords to search for in the speech.
- Keyword Detection: Calls
detectKeyword
to check for the presence of keywords. - Frequency Analysis: Calls
frequencyAnalysis
to print out the frequency of each word in the speech data.
Advertisement
Usage
- Keyword Detection: Checks if any predefined keywords are present in the speech data, providing basic keyword spotting functionality.
- Frequency Analysis: Performs a simple frequency analysis of words to understand word usage in the speech data.