-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piper Wolters
committed
Jun 21, 2019
1 parent
3f4f15c
commit 7112547
Showing
2 changed files
with
19 additions
and
115 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,30 @@ | ||
package main | ||
Package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func check(e error) { | ||
if e != nil { | ||
panic(e) | ||
} | ||
} | ||
|
||
|
||
func scanWords(path string) []string { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
|
||
scanner.Split(bufio.ScanWords) | ||
|
||
var words []string | ||
|
||
for scanner.Scan() { | ||
word := strings.Replace(scanner.Text(), ".", "", -1) | ||
word = strings.Replace(scanner.Text(), ",", "", -1) | ||
word = strings.Replace(scanner.Text(), "?", "", -1) | ||
word = strings.Replace(scanner.Text(), ":", "", -1) | ||
if word[0] == '"' { | ||
word = word[1:] | ||
} | ||
if word[len(word)-1] == '"' { | ||
word = word[:len(word)-1] | ||
} | ||
words = append(words, word) | ||
} | ||
|
||
return words | ||
} | ||
|
||
func word_count_test(t *testing.T) { | ||
words := scanWords("../7oldsamr.txt") | ||
|
||
func sortWords(words []string, m map[string]int) { | ||
for _, word := range words { | ||
m[strings.ToLower(word)] += 1 | ||
} | ||
|
||
// Creating another map to swap the positions of keys and values | ||
n := map[int][]string{} | ||
var a []int | ||
length := len(words) | ||
v1 := words[0] | ||
v2 := words[100] | ||
|
||
// Filling the map | ||
for k, v := range m { | ||
n[v] = append(n[v], k) | ||
if length != 497 { | ||
t.Error( | ||
"For the length, expected 497, got", length, | ||
) | ||
} | ||
for k := range n { | ||
a = append(a, k) | ||
if v1 != "THE" { | ||
t.Error( | ||
"For words[0], expected THE, got", words[0], | ||
) | ||
} | ||
|
||
// Reverse sorting the word frequencies and their respective keys | ||
sort.Sort(sort.Reverse(sort.IntSlice(a))) | ||
|
||
// Printing the output | ||
for _, k := range a { | ||
for _, s := range n[k] { | ||
fmt.Printf("%s, %d\n", s, k) | ||
} | ||
if v2 != "Emperor" { | ||
t.Error( | ||
"For words[100], expected Emperor, got", words[100], | ||
) | ||
} | ||
} | ||
|
||
|
||
func main() { | ||
|
||
// Checking for the necessary command line argument | ||
if len(os.Args) <= 1 { | ||
fmt.Println("Command line argument missing") | ||
return | ||
} | ||
|
||
// Initilizing a map | ||
m := make(map[string]int) | ||
|
||
// Scanning through the text file and adding initial info to the map | ||
words := scanWords(os.Args[1]) | ||
|
||
// Sort the words by frequencies | ||
sortWords(words, m) | ||
} | ||
|