diff --git a/data/dict.db b/data/dict.db new file mode 100644 index 0000000..eb354d9 Binary files /dev/null and b/data/dict.db differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..0499473 --- /dev/null +++ b/main.go @@ -0,0 +1,160 @@ +package main + +import ( + "os" + "io/ioutil" + /*"encoding/json"*/ + "fmt" + "regexp" + "html/template" + "log" + "net/http" + "database/sql" + _ "github.com/mattn/go-sqlite3" + +) + +type Entry struct { + ErrorMsg string + SearchResult string + Esp string + Eng string +} +/* +type Pronouns struct { + Prons []string `json:"pronouns"` +}*/ + + +func index(writer http.ResponseWriter, request *http.Request) { + // message := []byte("Hello, web!") + tmpl, err := os.Open("template/index.htm") + check(err) + + tmpl_content , err := ioutil.ReadAll(tmpl) + check(err) + fmt.Fprint(writer, string(tmpl_content)) + + // ins := Ins{author: "Tan Kian-ting", age: 30} + // err = tmpl.Execute() + // _, err := writer.Write(message) + check(err) +} + +func check(err error) { + if err != nil { + log.Fatal(err) + } +} + +func contain(slice []string, str string) bool{ + for _, item := range slice{ + if item == str{ + return true + } + } + return false +} + +func get_lemma(str string) string{ + verb_ptn := regexp.MustCompile(`(?P[\-a-zA-Z]+?)(?P((ant|int|ont)(an?|ajn?|on?|ojn?|e))|as|is|os|u|i|us)$`) + noun_ptn := regexp.MustCompile(`(?P[\-a-zA-Z]+?)(?P(on?|ojn?))$`) + adj_ptn := regexp.MustCompile(`(?P[\-a-zA-Z]+?)(?P(an?|ajn?))$`) + + var lemma string + verb_match := verb_ptn.FindStringSubmatch(str) + noun_match := noun_ptn.FindStringSubmatch(str) + adj_match := adj_ptn.FindStringSubmatch(str) + + if verb_match != nil { + lemma = verb_match[1] + "i" + } else if noun_match != nil { + lemma = noun_match[1] + "o" + } else if adj_match != nil { + lemma = adj_match[1] + "a" + } else{ + lemma = str + } + + return lemma + +} + + +func result_esp(result string, writer http.ResponseWriter, request *http.Request){ + + db, err := sql.Open("sqlite3", "data/dict.db") + check(err) + + + var pron_slice []string + + res, err := db.Query("SELECT * FROM Pronoun") + check(err) + + for res.Next(){ + var esp_pron string + err = res.Scan(&esp_pron) + pron_slice = append(pron_slice, esp_pron) + + } + + + var lemma string + + if contain(pron_slice, result) != true{ + lemma = get_lemma(result) // estas -> esti + //fmt.Println(lemma) + // if it's a pronoun, let lemma as a result + } else{ + lemma = result + } + + + res, err = db.Query("SELECT * FROM Dict where Esperanto=?", lemma) + check(err) + + var entry Entry + + entry.ErrorMsg = "找不到 " + result + " 的詞條,請重新尋找。" // preset it. + for res.Next() { + var esperanto string + var english string + err = res.Scan(&esperanto, &english) + check(err) + // total_result := esperanto + "\n" + english + + + + entry.SearchResult = result + entry.Esp = esperanto + entry.Eng = english + entry.ErrorMsg = "" + + } + + tmpl, err := template.ParseFiles("template/result.htm") + check(err) + err = tmpl.Execute(writer, entry) + // _, err = writer.Write([]byte(total_result)) + check(err) +} + + +func result(writer http.ResponseWriter, request *http.Request) { + // message := []byte("Hello, web!") + result := request.FormValue("word") + lang := request.FormValue("lang_select") + + if lang == "esp"{ + result_esp(result, writer, request) + + } +} + +func main() { + http.HandleFunc("/index.html", index) + http.HandleFunc("/result.html", result) + err := http.ListenAndServe("localhost:8080", nil) + log.Fatal(err) +} diff --git a/template/index.htm b/template/index.htm new file mode 100644 index 0000000..dd8fd3a --- /dev/null +++ b/template/index.htm @@ -0,0 +1,18 @@ + + +A mirror ESPDIC, an Esperanto-English Dictionary + + +
+
+Search words or meaning:
+
+Language: + + +
+
+ + + + diff --git a/template/result.htm b/template/result.htm new file mode 100644 index 0000000..e517961 --- /dev/null +++ b/template/result.htm @@ -0,0 +1,23 @@ + + +A mirror ESPDIC, an Esperanto-English Dictionary. Search of {{.SearchResult}} + + +
+
+Search words or meaning:
+
+Language: + + +
+
+
+ +
{{.ErrorMsg}}
+
{{.Esp}}
+
{{.Eng}}
+ + + +