How to find keyword matching between multiple text blocks in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "unicode"
    "strings"
)

/*
    Tokenize text into words.
    - Keeps only letters and digits
    - Splits on punctuation and spaces
*/
func tokenize(text string) map[string]bool {
    words := make(map[string]bool)
    var word strings.Builder

    for _, c := range text {
        if unicode.IsLetter(c) || unicode.IsDigit(c) {
            word.WriteRune(unicode.ToLower(c))
        } else if word.Len() > 0 {
            words[word.String()] = true
            word.Reset()
        }
    }

    if word.Len() > 0 {
        words[word.String()] = true
    }

    return words
}

/*
    // Find keyword matches across THREE OR MORE texts
    // -------------------------------------------------------------
    This function receives a vector of sets.
    It returns the intersection of ALL sets.
*/
func findMatchesMultiple(allSets []map[string]bool) map[string]bool {
    if len(allSets) == 0 {
        return map[string]bool{}
    }

    // Start with the first set
    result := make(map[string]bool)
    for w := range allSets[0] {
        result[w] = true
    }

    // Intersect with each remaining set
    for i := 1; i < len(allSets); i++ {
        temp := make(map[string]bool)

        for w := range result {
            if allSets[i][w] {
                temp[w] = true
            }
        }

        result = temp
    }

    return result
}

func main() {

    // -------------------------------------------------------------
    // Three text blocks to compare
    // -------------------------------------------------------------
    text1 :=
        "Machine learning allows computers to learn from data. " +
            "It is widely used in modern applications."

    text2 :=
        "Data science uses machine learning techniques. " +
            "Applications rely on data-driven models."

    text3 :=
        "Modern applications of machine learning include data analysis, " +
            "automation, and intelligent systems."

    // -------------------------------------------------------------
    // Tokenize all texts
    // -------------------------------------------------------------
    words1 := tokenize(text1)
    words2 := tokenize(text2)
    words3 := tokenize(text3)

    // Put them into a vector for multi-text comparison
    allSets := []map[string]bool{words1, words2, words3}

    // -------------------------------------------------------------
    // Find keyword matches across ALL texts
    // -------------------------------------------------------------
    matches := findMatchesMultiple(allSets)

    // -------------------------------------------------------------
    // Output results
    // -------------------------------------------------------------
    fmt.Println("Matched Keywords Across ALL Texts:")
    for w := range matches {
        fmt.Print(w, " ")
    }

    fmt.Println()
}


/*
run:

Matched Keywords Across ALL Texts:
learning applications machine data 

*/

 



answered May 22 by avibootz

Related questions

...