-
Notifications
You must be signed in to change notification settings - Fork 1
/
Aiken.go
59 lines (52 loc) · 1.4 KB
/
Aiken.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
package Aiken
import (
"io/ioutil"
"regexp"
"strings"
)
type Options struct {
Desc string `json:"desc"`
Answer string `json:"answer"`
}
type Aiken struct {
Question string `json:"question"`
Options []Options `json:"options"`
Answer string `json:"answer"`
}
func ReadAiken(path string) (result []Aiken, err error) {
names, err := ioutil.ReadFile(path)
//question := make(map[string]string)
if err == nil {
start := true
content := string(names)
contentPerLines := strings.Split(content, "\n")
var tmpData Aiken
var tmpChoices []Options
for _, val := range contentPerLines {
line := strings.TrimSuffix(strings.Trim(val, " \r\n"), "\r\n")
if line != "" {
isAnswer, _ := regexp.MatchString("^ANSWER:{1}", line)
if start {
tmpData.Question = strings.TrimSuffix(line, "\r")
start = false
} else if isAnswer {
answer := regexp.MustCompile(`^ANSWER:{1}`).ReplaceAllString(line, "")
tmpData.Answer = strings.TrimSuffix(answer, "\r")
tmpData.Options = tmpChoices
result = append(result, tmpData)
start = true
tmpData = Aiken{}
tmpChoices = []Options{}
} else {
options := strings.SplitN(line, " ", 2)
choice := Options{
Answer: regexp.MustCompile(`\.|\)`).ReplaceAllString(options[0], ""),
Desc: strings.Trim(options[1], " \r"),
}
tmpChoices = append(tmpChoices, choice)
}
}
}
}
return
}