How to find keyword matching between multiple text blocks in C++

1 Answer

0 votes
#include <iostream>
#include <set>
#include <vector>
#include <algorithm> // transform
#include <cctype>
  
/*
    Tokenize text into words.
    - Keeps only letters and digits
    - Splits on punctuation and spaces
*/
std::set<std::string> tokenize(const std::string &text) {
    std::set<std::string> words;
    std::string word;
  
    for (char c : text) {
        if (std::isalnum(c)) {
            word += std::tolower(c);
        } else if (!word.empty()) {
            words.insert(word);
            word.clear();
        }
    }
    if (!word.empty()) words.insert(word);
  
    return words;
}
  
/*
    // Find keyword matches across THREE OR MORE texts
    // -------------------------------------------------------------
    This function receives a vector of sets.
    It returns the intersection of ALL sets.
*/
std::set<std::string> findMatchesMultiple(
        const std::vector<std::set<std::string>> &allSets)
{
    if (allSets.empty()) return {};
  
    // Start with the first set
    std::set<std::string> result = allSets[0];
  
    // Intersect with each remaining set
    for (size_t i = 1; i < allSets.size(); i++) {
        std::set<std::string> temp;
  
        for (const std::string &w : result) {
            if (allSets[i].count(w)) {
                temp.insert(w);
            }
        }
  
        result = std::move(temp);
    }
  
    return result;
}
  
int main() {
    // -------------------------------------------------------------
    // Three text blocks to compare
    // -------------------------------------------------------------
    std::string text1 =
        "Machine learning allows computers to learn from data. "
        "It is widely used in modern applications.";
  
    std::string text2 =
        "Data science uses machine learning techniques. "
        "Applications rely on data-driven models.";
  
    std::string text3 =
        "Modern applications of machine learning include data analysis, "
        "automation, and intelligent systems.";
  
    // -------------------------------------------------------------
    // Tokenize all texts
    // -------------------------------------------------------------
    std::set<std::string> words1 = tokenize(text1);
    std::set<std::string> words2 = tokenize(text2);
    std::set<std::string> words3 = tokenize(text3);
  
    // Put them into a vector for multi-text comparison
    std::vector<std::set<std::string>> allSets = { words1, words2, words3 };
  
    // -------------------------------------------------------------
    // Find keyword matches across ALL texts
    // -------------------------------------------------------------
    std::set<std::string> matches = findMatchesMultiple(allSets);
  
    // -------------------------------------------------------------
    // Output results
    // -------------------------------------------------------------
    std::cout << "Matched Keywords Across ALL Texts:\n";
    for (const std::string &w : matches) 
        std::cout << w << " ";
}
  
  
  
/*
run:
  
Matched Keywords Across ALL Texts:
applications data learning machine 
  
*/

 



answered May 21 by avibootz
edited May 22 by avibootz

Related questions

...