Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mejorando Reto7, palabras acentuadas #276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,28 @@ func countWords(text: String) {

let mutableString = NSMutableString(string: text.lowercased())

let regex = try! NSRegularExpression(pattern: "[^a-z0-9]", options: [])
// añado dos patrones para excluir número que no se consideran palabras, y dejo las acentuadas
let regex = try! NSRegularExpression(pattern: "\\W", options: [])
let regex1 = try! NSRegularExpression(pattern: "\\d", options: [])
regex.replaceMatches(in: mutableString, options: [], range: NSMakeRange(0, mutableString.length), withTemplate: " ")
regex1.replaceMatches(in: mutableString, options: [], range: NSMakeRange(0, mutableString.length), withTemplate: " ")

var words: [String:Int] = [:]

String(mutableString).split(separator: " ").forEach { word in
String(mutableString).split(separator: " ").forEach{ word in
let key = String(word)
if words[key] != nil {
if words[key] != nil{
words[key]! += 1
} else {
words[key] = 1
}
}

words.forEach { key, value in
print("\(key) se ha repetido \(value) \(value == 1 ? "vez" : "veces")")
}
// words.forEach { key, value in
// print("\(key) se ha repetido \(value) \(value == 1 ? "vez" : "veces")")
// }

print(words)
}

countWords(text: "Hola, mi nombre es brais. Mi nombre completo es Brais Moure (MoureDev).")
countWords(text: "Hola, mi nombre es brais. Mi nombre completo es Brais Moure (MoureDev), qué qué tal qué?")