Created
April 26, 2018 14:13
-
-
Save toVersus/d2a998eff69aabb24d53d56569bd049c to your computer and use it in GitHub Desktop.
[Language Processing 100 Essentials] #71: Check whether the lists of stop word contain the specified word
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"reflect" | |
) | |
var StopWords = []string{"i", "me", "my", "myself", "we", "our", "ours", "ourselves", | |
"you", "your", "yours", "yourself", "yourselves", "he", "him", "his", "himself", | |
"she", "her", "hers", "herself", "it", "its", "itself", "they", "them", "their", "theirs", "themselves", | |
"what", "which", "who", "whom", "this", "that", "these", "those", "am", "is", "are", "was", "were", | |
"be", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing", | |
"a", "an", "the", "and", "but", "if", "or", "because", "as", "until", "while"} | |
func main() { | |
var word string | |
flag.StringVar(&word, "word", "", "specify a word to be checked") | |
flag.Parse() | |
dict := make(map[string]struct{}, len(StopWords)) | |
for _, w := range StopWords { | |
dict[w] = struct{}{} | |
} | |
isStopWord := false | |
if _, ok := dict[word]; ok { | |
isStopWord = true | |
} | |
fmt.Println(reflect.ValueOf(isStopWord)) | |
} |
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
package main | |
import ( | |
"reflect" | |
"testing" | |
) | |
var stopWordTests = []struct { | |
name string | |
word string | |
want bool | |
}{ | |
{ | |
name: "should return true because the specified word is contained in stop words", | |
word: "is", | |
want: true, | |
}, | |
{ | |
name: "should return false because the specified word is not contained in stop words", | |
word: "isssss", | |
want: false, | |
}, | |
} | |
func TestContains(t *testing.T) { | |
for _, testcase := range stopWordTests { | |
t.Log(testcase.name) | |
dict := make(map[string]struct{}, len(StopWords)) | |
for _, w := range StopWords { | |
dict[w] = struct{}{} | |
} | |
result := false | |
if _, ok := dict[testcase.word]; ok { | |
result = true | |
} | |
if !reflect.DeepEqual(result, testcase.want) { | |
t.Errorf("want => %#v\n expect => %#v", result, testcase.want) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment