-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (150 loc) · 4.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"encoding/json"
"flag"
"fmt"
"golang.org/x/net/html"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
// Usage is a struct storing the information for each usage in a Wiktionary entry.
// Each word can have several usages in the same or different languages.
type Usage struct {
// Part of speech of the word, e.g. Noun, Adjective, Interjection
PartOfSpeech string `json:"partOfSpeech"`
// Language for this usage
Lang string `json::"language"`
// List of definitions for this usage
Definitions []Definition `json:"definitions"`
}
// Definition is a struct storing information of a definition of a word usage
type Definition struct {
// The definition
Def string `json:"definition"`
// The examples for this definition
Examples []string `json:"examples"`
}
func parseHTML(htmlText string) string {
doc, err := html.Parse(strings.NewReader(htmlText))
if err != nil {
log.Fatal(err)
}
return parseDocTree(doc)
}
func parseDocTree(n *html.Node) string {
if n.Type == html.TextNode {
return n.Data
}
plain := ""
for c := n.FirstChild; c != nil; c = c.NextSibling {
plain += parseDocTree(c)
}
return plain
}
func parseDefinitions(json []interface{}) (definitions []Definition) {
for _, value := range json {
var definition Definition
switch typ := value.(type) {
case map[string]interface{}:
plainDef := parseHTML(typ["definition"].(string))
definition.Def = plainDef
if typ["examples"] != nil {
switch ex := typ["examples"].(type) {
case []interface{}:
for _, s := range ex {
plainExample := parseHTML(s.(string))
definition.Examples = append(definition.Examples, plainExample)
}
default:
fmt.Println("Error: some other typ")
}
}
default:
fmt.Println("Some other type", value)
}
definitions = append(definitions, definition)
}
return
}
func parseUsages(json map[string]interface{}, language string) (usages []Usage) {
for key, value := range json {
if language != "all" && key != language {
continue
}
var usage Usage
switch typ := value.(type) {
case []interface{}:
for _, u := range typ {
switch v := u.(type) {
case map[string]interface{}:
usage.PartOfSpeech = v["partOfSpeech"].(string)
usage.Lang = v["language"].(string)
usage.Definitions = parseDefinitions(v["definitions"].([]interface{}))
usages = append(usages, usage)
default:
fmt.Println("Some other type", v)
}
}
default:
fmt.Println(key, "is some other type")
}
}
return
}
func makeGreen(text string) string {
return "\033[32m" + text + "\033[0m"
}
func makeRed(text string) string {
return "\033[31m" + text + "\033[0m"
}
func makeBlue(text string) string {
return "\033[34m" + text + "\033[0m"
}
func main() {
langPtr := flag.String("l", "all", "2-letter code for the language you want to search,\nuse \"all\" to include all language")
wordPtr := flag.String("w", os.Args[1], "The word you want to look up")
flag.Parse()
word := *wordPtr
fmt.Println("Definition for", makeGreen(word))
response, err := http.Get("https://en.wiktionary.org/api/rest_v1/page/definition/" + word)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var result interface{}
err = json.Unmarshal([]byte(responseData), &result)
parsedResponse := result.(map[string]interface{})
if parsedResponse["title"] == "Not found." {
fmt.Println("That word does not exist.")
os.Exit(0)
}
usages := parseUsages(parsedResponse, *langPtr)
var currentLang string
for _, usage := range usages {
if currentLang != usage.Lang {
fmt.Println(usage.Lang)
currentLang = usage.Lang
}
fmt.Println("\tPart of Speech:", makeRed(usage.PartOfSpeech))
fmt.Println(makeBlue("\tDefinitions:"))
for i, definition := range usage.Definitions {
fmt.Print("\t", i+1, ". ")
fmt.Println(definition.Def)
if definition.Examples != nil {
fmt.Println("\t", "Examples")
for _, example := range definition.Examples {
fmt.Print("\t", "- ")
fmt.Println(example)
}
}
}
fmt.Println()
}
}