blob: fc60a6fb96245c46e24627e6f842f37b84aa5664 (
plain)
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
68
69
70
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import os
fn main() {
mut path := 'cinderella.txt'
if os.args.len != 2 {
println('usage: word_counter [text_file]')
println('using $path')
} else {
path = os.args[1]
}
contents := os.read_file(path.trim_space()) or {
println('failed to open $path')
return
}
mut m := map[string]int{}
for word in extract_words(contents) {
m[word]++
}
// Sort the keys
mut keys := m.keys()
keys.sort()
// Print the map
for key in keys {
val := m[key]
println('$key => $val')
}
}
// Creates an array of words from a given string
fn extract_words(contents string) []string {
mut splitted := []string{}
for space_splitted in contents.to_lower().split(' ') {
if space_splitted.contains('\n') {
splitted << space_splitted.split('\n')
} else {
splitted << space_splitted
}
}
mut results := []string{}
for s in splitted {
result := filter_word(s)
if result == '' {
continue
}
results << result
}
return results
}
// Removes punctuation
fn filter_word(word string) string {
if word == '' || word == ' ' {
return ''
}
mut i := 0
for i < word.len && !word[i].is_letter() {
i++
}
start := i
for i < word.len && word[i].is_letter() {
i++
}
end := i
return word[start..end]
}
|