From 7dd39b201ea1d1a8a6e3e61249c266ccd1f54b95 Mon Sep 17 00:00:00 2001 From: schktjm Date: Sun, 12 Jul 2020 18:15:27 +0900 Subject: [PATCH] :recycle: ... --- .gitignore | 4 +- backend/Dockerfile | 2 +- backend/controllers/v1/keigo.go | 69 +++++++++++++++++++++------------ backend/go.mod | 4 +- backend/main.go | 18 +++++++-- backend/models/kenjyo.go | 20 ++++++---- backend/models/sonkei.go | 5 +-- backend/utils/utils.go | 16 ++++---- 8 files changed, 89 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 1c0b8d4..f48b1c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .vscode/ -.idea/ \ No newline at end of file +.idea/ + +vendor/ diff --git a/backend/Dockerfile b/backend/Dockerfile index b0460fa..62969a2 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -13,4 +13,4 @@ RUN go build -o /keigo_server FROM alpine:latest as production-stage COPY --from=build-stage /keigo_server . EXPOSE 3000 -ENTRYPOINT ["./keigo_server"] \ No newline at end of file +ENTRYPOINT ["./keigo_server"] diff --git a/backend/controllers/v1/keigo.go b/backend/controllers/v1/keigo.go index fa56a2e..b8a0e2f 100644 --- a/backend/controllers/v1/keigo.go +++ b/backend/controllers/v1/keigo.go @@ -1,38 +1,57 @@ package v1 import ( - "github.com/gin-gonic/gin" - "keigo/models" "net/http" -) -type KeigoController struct{} + "github.com/aizu-go-kapro/keiGo/backend/models" + "github.com/gin-gonic/gin" +) const ( - Teinei string = "teinei" - Sonkei string = "sonkei" - Kenjyo string = "kenjyo" + teineiKind = "teinei" + sonkeiKind = "sonkei" + kenjyoKind = "kenjyo" ) +// Keigo is controller of keigo +type Keigo struct { + teinei models.Teinei + sonkei models.Sonkei + kenjyo models.Kenjyo +} + +// NewKeigo create keigo controller +func NewKeigo() *Keigo { + return &Keigo{} +} + +// ConvertKeigo is controller +func (kc *Keigo) ConvertKeigo(c *gin.Context) { + var ( + request models.KeigoRequest + response models.KeigoResponse + ) -func (kc *KeigoController) ConvertKeigo(c *gin.Context) { kind := c.Query("kind") - print(kind) - request := models.KeigoRequest{} - response := models.KeigoResponse{} + if kind == "" { + return c.JSON( + http.StatusBadRequest, + map[string]string{"status": "error", "message": "kind is empty"} + // '{"status": "error", "message": "kind is empty"}' + ) + } + if err := c.BindJSON(&request); err != nil { - c.Status(http.StatusBadRequest) - } else { - switch kind { - case Teinei: - teinei := models.Teinei{} - response.ConvertedBody = teinei.Convert(request.Body) - case Sonkei: - sonkei := models.Sonkei{} - response.ConvertedBody = sonkei.Convert(request.Body) - case Kenjyo: - kenjyo := models.Kenjyo{} - response.ConvertedBody = kenjyo.Convert(request.Body) - } - c.JSON(http.StatusOK, response) + return c.Status(http.StatusBadRequest) } + + switch kind { + case teineiKind: + response.ConvertedBody = kc.teinei.Convert(request.Body) + case sonkeiKind: + response.ConvertedBody = kc.sonkei.Convert(request.Body) + case kenjyoKind: + response.ConvertedBody = kc.kenjyo.Convert(request.Body) + } + + return c.JSON(http.StatusOK, response) } diff --git a/backend/go.mod b/backend/go.mod index c5c1436..65f59bb 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,6 +1,6 @@ -module keigo +module github.com/aizu-go-kapro/keiGo/backend -go 1.13 +go 1.14 require ( github.com/gin-gonic/gin v1.6.3 diff --git a/backend/main.go b/backend/main.go index bc606e8..2fbf7c0 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,16 +1,28 @@ package main import ( + "io/ioutil" + + controllers "github.com/aizu-go-kapro/keiGo/backend/controllers/v1" + "github.com/aizu-go-kapro/keiGo/backend/utils" "github.com/gin-gonic/gin" - controllers "keigo/controllers/v1" ) +// go fmt ... + func main() { + kenjo, err := ioutil.ReadFile("./utils/" + "kenjo.json") + if err != nil { + panic(err) + } + util := utils.Utils{ + Kenjo: kenjo, + } router := gin.Default() + kC := controllers.NewKeigo() api := router.Group("/api/v1") { - kc := new(controllers.KeigoController) - api.GET("/keigo", kc.ConvertKeigo) + api.GET("/keigo", kC.ConvertKeigo) } router.Run(":3000") } diff --git a/backend/models/kenjyo.go b/backend/models/kenjyo.go index 0cb11ec..757130b 100644 --- a/backend/models/kenjyo.go +++ b/backend/models/kenjyo.go @@ -2,20 +2,27 @@ package models import ( "fmt" - "keigo/utils" "strings" + "github.com/aizu-go-kapro/keiGo/backend/utils" + "github.com/ikawaha/kagome/tokenizer" ) -type Kenjyo struct{} +type Kenjyo struct{ + Utils: utils, +} + -func (k *Kenjyo) Convert(body string) string { +func (k *Kenjyo) Convert(body string) (string, error) { kagome := Kagome{} tokens := kagome.MorphologicalAnalysis(body) utils := utils.Utils{} - conversionRules := utils.JsonDecoder("kenjyo.json") + conversionRules, err := utils.JsonDecoder("kenjyo.json") + if err != nil { + return "", err + } var convertedBody = "" @@ -23,9 +30,8 @@ func (k *Kenjyo) Convert(body string) string { for { if tokens[endOfSentenceTokenIndex].Features()[0] == "記号" { endOfSentenceTokenIndex-- - } else { - break - } + } + break } for _, token := range tokens { diff --git a/backend/models/sonkei.go b/backend/models/sonkei.go index dfc1917..75df14e 100644 --- a/backend/models/sonkei.go +++ b/backend/models/sonkei.go @@ -2,9 +2,10 @@ package models import ( "fmt" - "keigo/utils" "strings" + "github.com/aizu-go-kapro/keiGo/backend/utils" + "github.com/ikawaha/kagome/tokenizer" ) @@ -14,9 +15,7 @@ func (s *Sonkei) Convert(body string) string { kagome := Kagome{} tokens := kagome.MorphologicalAnalysis(body) - utils := utils.Utils{} conversionRules := utils.JsonDecoder("sonkei.json") - var convertedBody = "" endOfSentenceTokenIndex := len(tokens) - 1 diff --git a/backend/utils/utils.go b/backend/utils/utils.go index c463660..657e4a0 100644 --- a/backend/utils/utils.go +++ b/backend/utils/utils.go @@ -1,13 +1,14 @@ -package utils +package converter import ( "encoding/json" "io/ioutil" - "log" ) type ( - Utils struct{} + Utils struct { + Path string // json ファイルのパス + } ConversionRule struct { Original string `json:"original"` @@ -15,19 +16,20 @@ type ( } ) -func (u *Utils) JsonDecoder(filename string) []ConversionRule { +func (u *Utils) JsonDecoder(filename string) ([]ConversionRule, error) { // File openは実行時パスから見た相対パスになる bytes, err := ioutil.ReadFile("./utils/" + filename) if err != nil { - log.Fatal(err) + return nil, err } + // JSONデコード var conversionRules []ConversionRule if err := json.Unmarshal(bytes, &conversionRules); err != nil { - log.Fatal(err) + return nil, err } - return conversionRules + return conversionRules, nil } func (u *Utils) FindConvertedFromConversionRule(conversionRules []ConversionRule, original string) string {