Answered step by step
Verified Expert Solution
Question
1 Approved Answer
betty bought some butter but the butter was bitter so betty bought some better butter to make the bitter butter better use golang and comments
betty bought some butter but the butter was bitter so betty bought some better butter to make the bitter butter better
use golang and comments
1/ Find the top k most common words in a text document. // Input path: location of the document, K top words // Output: Slice of top K words // For this excercise, word is defined as characters separated by a whitespace // Note: You should use "checkError' to handle potential errors. package textproc import ( "fmt" "log" "sort" ) func topWords (path string, K int) []WordCount { // Your code here..... } DO NOT MODIFY- I/ A struct that represents how many times a word is observed in a document type WordCount struct { Word string Count int } // Method to convert struct to string format func (wc WordCount) String() string { return fmt. Sprintf("%v: %v", wc. Word, wc. Count) } // Helper function to sort a list of word counts in place. 1/ This sorts by the count in decreasing order, breaking ties using the word. func sortWordCounts(wordCounts []WordCount) { sort. Slice (wordCounts, func(i, j int) bool { Wc1 : = wordCounts[i] WC2 := wordCounts[j] if wc1.Count == wc2.Count { return wc1. Word WC2. Count }) } func checkError(err error) { if err != nil { log. Fatal(err) } } package textproc import ( "testing" ). func TestTopwords(t *testing. T) { topk := topWords("passage", 3) want := "butter: 4 better: 2 betty: 2 " got := for word := range topk { got += word. String() + } if got != want { t.Errorf("TopWords test failed, Want-{%s} Got-{%s}", want, got) } } 1/ Find the top k most common words in a text document. // Input path: location of the document, K top words // Output: Slice of top K words // For this excercise, word is defined as characters separated by a whitespace // Note: You should use "checkError' to handle potential errors. package textproc import ( "fmt" "log" "sort" ) func topWords (path string, K int) []WordCount { // Your code here..... } DO NOT MODIFY- I/ A struct that represents how many times a word is observed in a document type WordCount struct { Word string Count int } // Method to convert struct to string format func (wc WordCount) String() string { return fmt. Sprintf("%v: %v", wc. Word, wc. Count) } // Helper function to sort a list of word counts in place. 1/ This sorts by the count in decreasing order, breaking ties using the word. func sortWordCounts(wordCounts []WordCount) { sort. Slice (wordCounts, func(i, j int) bool { Wc1 : = wordCounts[i] WC2 := wordCounts[j] if wc1.Count == wc2.Count { return wc1. Word WC2. Count }) } func checkError(err error) { if err != nil { log. Fatal(err) } } package textproc import ( "testing" ). func TestTopwords(t *testing. T) { topk := topWords("passage", 3) want := "butter: 4 better: 2 betty: 2 " got := for word := range topk { got += word. String() + } if got != want { t.Errorf("TopWords test failed, Want-{%s} Got-{%s}", want, got) } }Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started