Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribedimage text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

1.what is the significance of Taxonomy ?

Answered: 1 week ago

Question

What are the advantages and disadvantages of leasing ?

Answered: 1 week ago

Question

Name is needed for identifying organisms ?

Answered: 1 week ago