Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #142 from yashikota/morph
Browse files Browse the repository at this point in the history
  • Loading branch information
yashikota authored Oct 27, 2024
2 parents 337e6b2 + a7f85a7 commit 0c1b75f
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 0 deletions.
43 changes: 43 additions & 0 deletions api/v1/morph/morph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package morph

import (
"log/slog"
"net/http"

"github.com/yashikota/chronotes/model/v1"
"github.com/yashikota/chronotes/pkg/utils"
)

func GetMorphHandler(w http.ResponseWriter, r *http.Request) {
// Validate token
user := model.NewUser()
user.UserID = r.Context().Value(utils.TokenKey).(utils.Token).UserID

// Check if token exists
key := "jwt:" + user.UserID
if _, err := utils.GetToken(key); err != nil {
utils.ErrorJSONResponse(w, http.StatusBadRequest, err)
return
}

slog.Info("Validation passed")

sentence, err := utils.GetQueryParam(r, "sentence", true)
if err != nil {
utils.ErrorJSONResponse(w, http.StatusBadRequest, err)
return
}

slog.Info("Parse request body passed")

result, err := utils.GetMorph(sentence)
if err != nil {
utils.ErrorJSONResponse(w, http.StatusInternalServerError, err)
return
}

slog.Info("morph passed")

// Response
utils.SuccessJSONResponse(w, result)
}
4 changes: 4 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/yashikota/chronotes/api/v1/images"
"github.com/yashikota/chronotes/api/v1/notes"
"github.com/yashikota/chronotes/api/v1/users"
"github.com/yashikota/chronotes/api/v1/morph"
"github.com/yashikota/chronotes/pkg/db"
"github.com/yashikota/chronotes/pkg/elastic"
"github.com/yashikota/chronotes/pkg/minio"
Expand Down Expand Up @@ -86,6 +87,9 @@ func main() {
r.HandleFunc("POST /images", images.UploadImageHandler)
r.HandleFunc("GET /images", images.GetImageHandler)
r.HandleFunc("DELETE /images", images.DeleteImageHandler)

// Morph routes
r.HandleFunc("GET /morph", morph.GetMorphHandler)
})

// Admin routes
Expand Down
39 changes: 39 additions & 0 deletions docs/api/bundled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tags:
- name: Notes
- name: Images
- name: Admin
- name: Morph
paths:
/api/v1/admin/notes:
post:
Expand Down Expand Up @@ -296,6 +297,35 @@ paths:
- Images
security:
- BearerAuth: []
/api/v1/morph:
get:
operationId: Morhpological_getMorph
summary: Morhpological analysis
description: 文字列を送ると形態素解析した結果を返す
parameters:
- name: sentence
in: query
required: true
schema:
type: string
explode: false
responses:
'200':
description: The request has succeeded.
content:
application/json:
schema:
$ref: '#/components/schemas/Morph'
default:
description: An unexpected error response.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
tags:
- Morph
security:
- BearerAuth: []
/api/v1/notes:
get:
operationId: Notes_getNotes
Expand Down Expand Up @@ -756,6 +786,15 @@ components:
minLength: 8
maxLength: 20
description: パスワード。8文字以上20文字以下
Morph:
type: object
required:
- WordList
properties:
WordList:
type: string
example: '[[[日本語],[を],[分析],[し],[ます]]]'
description: 形態素解析結果
Note:
type: object
required:
Expand Down
8 changes: 8 additions & 0 deletions docs/api/components/schemas/Morph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: object
required:
- WordList
properties:
WordList:
type: string
example: '[[[日本語],[を],[分析],[し],[ます]]]'
description: 形態素解析結果
3 changes: 3 additions & 0 deletions docs/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tags:
- name: Notes
- name: Images
- name: Admin
- name: Morph
paths:
/api/v1/admin/notes:
$ref: paths/api_v1_admin_notes.yaml
Expand All @@ -22,6 +23,8 @@ paths:
$ref: paths/api_v1_health.yaml
/api/v1/images:
$ref: paths/api_v1_images.yaml
/api/v1/morph:
$ref: paths/api_v1_morph.yaml
/api/v1/notes:
$ref: paths/api_v1_notes.yaml
/api/v1/notes/search:
Expand Down
28 changes: 28 additions & 0 deletions docs/api/paths/api_v1_morph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
get:
operationId: Morhpological_getMorph
summary: Morhpological analysis
description: 文字列を送ると形態素解析した結果を返す
parameters:
- name: sentence
in: query
required: true
schema:
type: string
explode: false
responses:
'200':
description: The request has succeeded.
content:
application/json:
schema:
$ref: ../components/schemas/Morph.yaml
default:
description: An unexpected error response.
content:
application/json:
schema:
$ref: ../components/schemas/ErrorResponse.yaml
tags:
- Morph
security:
- BearerAuth: []
1 change: 1 addition & 0 deletions docs/spec/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "./users.tsp";
import "./notes.tsp";
import "./images.tsp";
import "./admin.tsp";
import "./morph.tsp";

using TypeSpec.Http;
using TypeSpec.Rest;
Expand Down
6 changes: 6 additions & 0 deletions docs/spec/model.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ namespace Chronotes {
createdAt: User.createdAt;
updatedAt: User.updatedAt;
}

model Morph {
@doc("形態素解析結果")
@example("[[[日本語],[を],[分析],[し],[ます]]]")
WordList: string;
}
}
13 changes: 13 additions & 0 deletions docs/spec/morph.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using TypeSpec.Http;

namespace Chronotes {
@route("/api/v1/morph")
@tag("Morph")
@useAuth(BearerAuth)
interface Morhpological {
@get
@doc("文字列を送ると形態素解析した結果を返す")
@summary("Morhpological analysis")
getMorph(@query sentence: string): Morph | ErrorResponse;
}
}
3 changes: 3 additions & 0 deletions model/v1/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
db "github.com/yashikota/chronotes/model/v1/db"
morph "github.com/yashikota/chronotes/model/v1/morph"
)

type (
Expand All @@ -14,6 +15,8 @@ type (
Summary = db.Summary
Password = db.Password
Accounts = db.Accounts
MorphRequest = morph.MorphRequest
MorphResponse = morph.MorphResponse
)

const (
Expand Down
14 changes: 14 additions & 0 deletions model/v1/morph/morph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

type MorphRequest struct {
AppID string `json:"app_id"`
RequestID string `json:"request_id,omitempty"`
Sentence string `json:"sentence"`
InfoFilter string `json:"info_filter,omitempty"`
}

type MorphResponse struct {
InfoFilter string `json:"info_filter"`
RequestID string `json:"request_id"`
WordList [][][]string `json:"word_list"`
}
53 changes: 53 additions & 0 deletions pkg/utils/morph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package utils

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"

"github.com/yashikota/chronotes/model/v1"
)

func GetMorph(sentence string) (model.MorphResponse, error) {
appID := os.Getenv("GOO_LAB_TOKEN")
if appID == "" {
return model.MorphResponse{}, errors.New("GOO_LAB_TOKEN is required")
}

req := model.MorphRequest{
AppID: appID,
Sentence: sentence,
}

reqBody, err := json.Marshal(req)
if err != nil {
slog.Error("Error json marshal")
return model.MorphResponse{}, err
}

url := "https://labs.goo.ne.jp/api/morph"
res, err := http.Post(url, "application/json", bytes.NewBuffer(reqBody))
if err != nil {
return model.MorphResponse{}, err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return model.MorphResponse{}, err
}

var morph model.MorphResponse
if err := json.Unmarshal(body, &morph); err != nil {
slog.Error("json unmarshal is error")
}

fmt.Println("morph", morph)

return morph, nil
}

0 comments on commit 0c1b75f

Please sign in to comment.