Fix linting issues
This commit is contained in:
parent
eb17d2b829
commit
356c77af7f
41
main.go
41
main.go
|
@ -11,50 +11,57 @@ import (
|
||||||
"strings"
|
"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 {
|
type Usage struct {
|
||||||
|
// Part of speech of the word, e.g. Noun, Adjective, Interjection
|
||||||
PartOfSpeech string `json:"partOfSpeech"`
|
PartOfSpeech string `json:"partOfSpeech"`
|
||||||
|
// Language for this usage
|
||||||
Lang string `json::"language"`
|
Lang string `json::"language"`
|
||||||
|
// List of definitions for this usage
|
||||||
Definitions []Definition `json:"definitions"`
|
Definitions []Definition `json:"definitions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Definition is a struct storing information of a definition of a word usage
|
||||||
type Definition struct {
|
type Definition struct {
|
||||||
|
// The definition
|
||||||
Def string `json:"definition"`
|
Def string `json:"definition"`
|
||||||
|
// The examples for this definition
|
||||||
Examples []string `json:"examples"`
|
Examples []string `json:"examples"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseHTML(htmlText string) (string) {
|
func parseHTML(htmlText string) (string) {
|
||||||
doc, err := html.Parse(strings.NewReader(htmlText))
|
doc, err := html.Parse(strings.NewReader(htmlText))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
return parseHTML(doc)
|
return parseDocTree(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseHTML(n *html.Node) (string) {
|
func parseDocTree(n *html.Node) (string) {
|
||||||
if n.Type == html.TextNode {
|
if n.Type == html.TextNode {
|
||||||
return n.Data
|
return n.Data
|
||||||
} else {
|
|
||||||
plain := ""
|
|
||||||
for c := n.FirstChild; c!= nil; c = c.NextSibling {
|
|
||||||
plain += parseHTML(c)
|
|
||||||
}
|
|
||||||
return plain
|
|
||||||
}
|
}
|
||||||
|
plain := ""
|
||||||
|
for c := n.FirstChild; c!= nil; c = c.NextSibling {
|
||||||
|
plain += parseDocTree(c)
|
||||||
|
}
|
||||||
|
return plain
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseDefinitions(json []interface{}) (definitions []Definition) {
|
func parseDefinitions(json []interface{}) (definitions []Definition) {
|
||||||
for _, value := range json {
|
for _, value := range json {
|
||||||
var definition Definition
|
var definition Definition
|
||||||
switch typ := value.(type) {
|
switch typ := value.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
plain_def := ParseHTML(typ["definition"].(string))
|
plainDef := parseHTML(typ["definition"].(string))
|
||||||
definition.Def = plain_def
|
definition.Def = plainDef
|
||||||
if typ["examples"] != nil {
|
if typ["examples"] != nil {
|
||||||
switch ex := typ["examples"].(type) {
|
switch ex := typ["examples"].(type) {
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
for _, s := range ex {
|
for _, s := range ex {
|
||||||
plain_example := ParseHTML(s.(string))
|
plainExample := parseHTML(s.(string))
|
||||||
definition.Examples = append(definition.Examples, plain_example)
|
definition.Examples = append(definition.Examples, plainExample)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Println("Error: some other typ")
|
fmt.Println("Error: some other typ")
|
||||||
|
@ -68,7 +75,7 @@ func ParseDefinitions(json []interface{}) (definitions []Definition) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseUsages(json map[string]interface{}) (usages []Usage) {
|
func parseUsages(json map[string]interface{}) (usages []Usage) {
|
||||||
for key, value := range json {
|
for key, value := range json {
|
||||||
var usage Usage
|
var usage Usage
|
||||||
switch typ := value.(type) {
|
switch typ := value.(type) {
|
||||||
|
@ -78,7 +85,7 @@ func ParseUsages(json map[string]interface{}) (usages []Usage) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
usage.PartOfSpeech = v["partOfSpeech"].(string)
|
usage.PartOfSpeech = v["partOfSpeech"].(string)
|
||||||
usage.Lang = v["language"].(string)
|
usage.Lang = v["language"].(string)
|
||||||
usage.Definitions = ParseDefinitions(v["definitions"].([]interface{}))
|
usage.Definitions = parseDefinitions(v["definitions"].([]interface{}))
|
||||||
default:
|
default:
|
||||||
fmt.Println("Some other type", v)
|
fmt.Println("Some other type", v)
|
||||||
}
|
}
|
||||||
|
@ -121,7 +128,7 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
usages := ParseUsages(parsedResponse)
|
usages := parseUsages(parsedResponse)
|
||||||
for _, usage := range usages {
|
for _, usage := range usages {
|
||||||
fmt.Println(usage.Lang)
|
fmt.Println(usage.Lang)
|
||||||
fmt.Println("Part of Speech:", usage.PartOfSpeech)
|
fmt.Println("Part of Speech:", usage.PartOfSpeech)
|
||||||
|
|
Loading…
Reference in New Issue