v / examples / word_counter
Raw file | 70 loc (65 sloc) | 1.37 KB | Latest commit hash 017ace6ea
1// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4import os
5
6fn main() {
7 mut path := 'cinderella.txt'
8 if os.args.len != 2 {
9 println('usage: word_counter [text_file]')
10 println('using ${path}')
11 } else {
12 path = os.args[1]
13 }
14 contents := os.read_file(path.trim_space()) or {
15 println('failed to open ${path}')
16 return
17 }
18 mut m := map[string]int{}
19 for word in extract_words(contents) {
20 m[word]++
21 }
22 // Sort the keys
23 mut keys := m.keys()
24 keys.sort()
25 // Print the map
26 for key in keys {
27 val := m[key]
28 println('${key} => ${val}')
29 }
30}
31
32// Creates an array of words from a given string
33fn extract_words(contents string) []string {
34 mut splits := []string{}
35 for space_split in contents.to_lower().split(' ') {
36 if space_split.contains('\n') {
37 splits << space_split.split('\n')
38 } else {
39 splits << space_split
40 }
41 }
42
43 mut results := []string{}
44 for s in splits {
45 result := filter_word(s)
46 if result == '' {
47 continue
48 }
49 results << result
50 }
51
52 return results
53}
54
55// Removes punctuation
56fn filter_word(word string) string {
57 if word == '' || word == ' ' {
58 return ''
59 }
60 mut i := 0
61 for i < word.len && !word[i].is_letter() {
62 i++
63 }
64 start := i
65 for i < word.len && word[i].is_letter() {
66 i++
67 }
68 end := i
69 return word[start..end]
70}