Skip to content

Commit

Permalink
updating file names 001
Browse files Browse the repository at this point in the history
  • Loading branch information
Piper Wolters committed Jun 21, 2019
1 parent 3f4f15c commit 7112547
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 115 deletions.
30 changes: 0 additions & 30 deletions exercise-001-corpus/001/corpus/test.go

This file was deleted.

104 changes: 19 additions & 85 deletions exercise-001-corpus/001/corpus/word_count_test.go
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)
}

0 comments on commit 7112547

Please sign in to comment.