Merge pull request #6 from Huy-Ngo/gh-actions

Create a GitHub action for linting
This commit is contained in:
Ngô Ngọc Đức Huy 2020-10-30 17:57:49 +07:00 committed by GitHub
commit dfed27b3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 17 deletions

38
.github/workflows/go.yml vendored Normal file
View File

@ -0,0 +1,38 @@
name: Go
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- uses: Jerome1337/golint-action@v1.0.2
with:
golint-path: './main.go'
- name: Build
run: go build -v .

41
main.go
View File

@ -11,50 +11,57 @@ import (
"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) {
func parseHTML(htmlText string) (string) {
doc, err := html.Parse(strings.NewReader(htmlText))
if err != nil {
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 {
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 {
var definition Definition
switch typ := value.(type) {
case map[string]interface{}:
plain_def := ParseHTML(typ["definition"].(string))
definition.Def = plain_def
plainDef := parseHTML(typ["definition"].(string))
definition.Def = plainDef
if typ["examples"] != nil {
switch ex := typ["examples"].(type) {
case []interface{}:
for _, s := range ex {
plain_example := ParseHTML(s.(string))
definition.Examples = append(definition.Examples, plain_example)
plainExample := parseHTML(s.(string))
definition.Examples = append(definition.Examples, plainExample)
}
default:
fmt.Println("Error: some other typ")
@ -68,7 +75,7 @@ func ParseDefinitions(json []interface{}) (definitions []Definition) {
return
}
func ParseUsages(json map[string]interface{}) (usages []Usage) {
func parseUsages(json map[string]interface{}) (usages []Usage) {
for key, value := range json {
var usage Usage
switch typ := value.(type) {
@ -78,7 +85,7 @@ func ParseUsages(json map[string]interface{}) (usages []Usage) {
case map[string]interface{}:
usage.PartOfSpeech = v["partOfSpeech"].(string)
usage.Lang = v["language"].(string)
usage.Definitions = ParseDefinitions(v["definitions"].([]interface{}))
usage.Definitions = parseDefinitions(v["definitions"].([]interface{}))
default:
fmt.Println("Some other type", v)
}
@ -121,7 +128,7 @@ func main() {
os.Exit(0)
}
usages := ParseUsages(parsedResponse)
usages := parseUsages(parsedResponse)
for _, usage := range usages {
fmt.Println(usage.Lang)
fmt.Println("Part of Speech:", usage.PartOfSpeech)