This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from yashikota/morph
- Loading branch information
Showing
12 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type: object | ||
required: | ||
- WordList | ||
properties: | ||
WordList: | ||
type: string | ||
example: '[[[日本語],[を],[分析],[し],[ます]]]' | ||
description: 形態素解析結果 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |