/*
Tokenize text into words.
- Keeps only letters and digits
- Splits on punctuation and spaces
*/
fun tokenize(text: String): Set<String> {
val words: MutableSet<String> = mutableSetOf()
val sb = StringBuilder()
for (c in text) {
if (c.isLetterOrDigit()) {
sb.append(c.lowercaseChar())
} else if (sb.isNotEmpty()) {
words.add(sb.toString())
sb.clear()
}
}
if (sb.isNotEmpty()) {
words.add(sb.toString())
}
return words
}
/*
// Find keyword matches across THREE OR MORE texts
// -------------------------------------------------------------
This function receives a vector of sets.
It returns the intersection of ALL sets.
*/
fun findMatchesMultiple(allSets: List<Set<String>>): Set<String> {
if (allSets.isEmpty()) return emptySet()
// Start with the first set
var result: Set<String> = allSets[0].toSet()
// Intersect with each remaining set
for (i in 1 until allSets.size) {
val temp: MutableSet<String> = mutableSetOf()
for (w in result) {
if (allSets[i].contains(w)) {
temp.add(w)
}
}
result = temp
}
return result
}
fun main() {
// -------------------------------------------------------------
// Three text blocks to compare
// -------------------------------------------------------------
val text1 =
"Machine learning allows computers to learn from data. " +
"It is widely used in modern applications."
val text2 =
"Data science uses machine learning techniques. " +
"Applications rely on data-driven models."
val text3 =
"Modern applications of machine learning include data analysis, " +
"automation, and intelligent systems."
// -------------------------------------------------------------
// Tokenize all texts
// -------------------------------------------------------------
val words1 = tokenize(text1)
val words2 = tokenize(text2)
val words3 = tokenize(text3)
// Put them into a vector for multi-text comparison
val allSets = listOf(words1, words2, words3)
// -------------------------------------------------------------
// Find keyword matches across ALL texts
// -------------------------------------------------------------
val matches = findMatchesMultiple(allSets)
// -------------------------------------------------------------
// Output results
// -------------------------------------------------------------
println("Matched Keywords Across ALL Texts:")
for (w in matches) {
print("$w ")
}
}
/*
run:
Matched Keywords Across ALL Texts:
machine learning data applications
*/