├── README.md ├── LICENSE └── toefl.go /README.md: -------------------------------------------------------------------------------- 1 | # TOEFL word random generator 2 | 3 | This program print a TOEFL word randomly from the word book "新东方·TOEFL词汇" by 王玉梅. 4 | 5 | ## Installation 6 | First, [install the Go tools](https://golang.org/doc/install#install) 7 | 8 | Second, add `$HOME/go/bin` to your `PATH`. (Otherwise, you can only run this program using its full path, for example `~/go/bin/toefl`.) 9 | 10 | After that, run the command below in your terminal 11 | ``` 12 | $ go get github.com/ladrift/toefl 13 | ``` 14 | and type `toefl` to get a word to learn! 15 | 16 | ## Example 17 | ``` 18 | $ toefl | cowsay 19 | _______________________________________ 20 | / wholesome \ 21 | | | 22 | | adj. 有益健康的, [同]healthful, | 23 | \ beneficial / 24 | --------------------------------------- 25 | \ ^__^ 26 | \ (oo)\_______ 27 | (__)\ )\/\ 28 | ||----w | 29 | || || 30 | 31 | ``` 32 | 33 | I put this command in my shell startup script, which making me learning and reviewing TOEFL words all the day. :smile: 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ziyi Yan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /toefl.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "math/rand" 7 | "strings" 8 | "time" 9 | "unicode" 10 | ) 11 | 12 | func main() { 13 | word := random() 14 | fmt.Println(word) 15 | } 16 | 17 | type word struct { 18 | spelling string 19 | meanings []string 20 | } 21 | 22 | func (w word) String() string { 23 | var buf bytes.Buffer 24 | 25 | buf.WriteString(w.spelling) 26 | buf.WriteRune('\n') 27 | for _, meaning := range w.meanings { 28 | buf.WriteRune('\n') 29 | buf.WriteString(meaning) 30 | } 31 | 32 | return buf.String() 33 | } 34 | 35 | func random() word { 36 | words := load("words/wangyumei-toefl-words.txt") 37 | rand.Seed(time.Now().UnixNano()) 38 | idx := rand.Intn(len(words)) 39 | return words[idx] 40 | } 41 | 42 | func load(path string) (words []word) { 43 | asset := MustAsset(path) 44 | for _, line := range strings.Split(string(asset), "\n") { 45 | line = strings.TrimFunc(line, unicode.IsSpace) 46 | if line == "" { 47 | continue 48 | } 49 | 50 | words = append(words, parse(line)) 51 | } 52 | return 53 | } 54 | 55 | func parse(raw string) word { 56 | parts := strings.Split(raw, "#") 57 | spelling := parts[0] 58 | 59 | var meanings []string 60 | for _, meaning := range strings.Split(parts[1], ";") { 61 | meaning := strings.TrimFunc(meaning, unicode.IsSpace) 62 | meanings = append(meanings, meaning) 63 | } 64 | return word{spelling, meanings} 65 | } 66 | --------------------------------------------------------------------------------