-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.go
67 lines (57 loc) · 1.72 KB
/
dict.go
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
// Archivo: dict.go
package mutator
import (
"math/rand"
"strings"
"time"
)
func isValidWord(word string, letters, numbers, special bool) bool {
for _, r := range word {
if (letters && isLetter(r)) || (numbers && isNumber(r)) || (special && isSpecialChar(r)) {
return true
}
}
return false
}
func CreateDict(content string, totalWords int, kind string) map[string]int {
var minRepetitions = 5
var minLength = 4
words := strings.Split(content, " ")
letters, numbers, special := processKind(kind)
wordCount := make(map[string]int)
filteredWords := make(map[string]int)
// Filtrar las palabras según el tipo de caracteres especificados
for _, word := range words {
word = strings.ReplaceAll(word, "\n", "")
if isValidWord(word, letters, numbers, special) {
wordCount[word]++
}
}
// Filtrar palabras que tienen al menos 5 repeticiones y longitud mínima de 4 caracteres
for word, count := range wordCount {
if count >= minRepetitions && len(word) >= minLength {
filteredWords[word] = count
}
}
// Si hay menos palabras filtradas que totalWords, retornar todas las palabras filtradas
if len(filteredWords) <= totalWords {
return filteredWords
}
rand.Seed(time.Now().UnixNano())
selectedWords := make(map[string]int)
usedIndices := make(map[int]bool)
filteredWordsList := make([]string, 0, len(filteredWords))
for word := range filteredWords {
filteredWordsList = append(filteredWordsList, word)
}
// Seleccionar palabras aleatorias sin repetición
for len(selectedWords) < totalWords {
index := rand.Intn(len(filteredWordsList))
if !usedIndices[index] {
word := filteredWordsList[index]
selectedWords[word] = filteredWords[word]
usedIndices[index] = true
}
}
return selectedWords
}