Sign inSign up

simooowann/mecab-service

By simooowann

Updated about 1 year ago

Image
0

110

simooowann/mecab-service repository overview

MeCab Microservice API Documentation

Overview

The MeCab Microservice provides RESTful API endpoints for Japanese morphological analysis using MeCab. It supports various output formats and analysis modes.

Base URL

http://localhost:8081

Authentication

No authentication is required for the API endpoints.

Endpoints

Health Check

GET /health

Check the service health and version information.

Response:

{
  "status": "healthy",
  "version": "mecab of 0.996",
  "timestamp": "2024-01-01T00:00:00Z"
}
Morphological Analysis

POST /analyze

Perform standard morphological analysis on Japanese text.

Request Body:

{
  "text": "日本語の文章を解析します"
}

Response:

{
  "original_text": "日本語の文章を解析します",
  "results": [
    {
      "surface": "日本語",
      "pos": "名詞",
      "pos_detail_1": "一般",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugation": "*",
      "conj_form": "*",
      "base_form": "日本語",
      "reading": "ニホンゴ",
      "pronunciation": "ニホンゴ"
    },
    {
      "surface": "の",
      "pos": "助詞",
      "pos_detail_1": "連体化",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugation": "*",
      "conj_form": "*",
      "base_form": "の",
      "reading": "ノ",
      "pronunciation": "ノ"
    }
  ]
}
Word Segmentation (Wakati)

POST /analyze/wakati

Perform word segmentation (わかち書き) on Japanese text.

Request Body:

{
  "text": "日本語の文章を解析します"
}

Response:

{
  "original_text": "日本語の文章を解析します",
  "wakati": "日本語 の 文章 を 解析 します"
}
Reading Pronunciation (Yomi)

POST /analyze/yomi

Get the reading pronunciation (読み) of Japanese text.

Request Body:

{
  "text": "日本語の文章を解析します"
}

Response:

{
  "original_text": "日本語の文章を解析します",
  "yomi": "ニホンゴノブンショウヲカイセキシマス"
}
ChaSen Compatible Format

POST /analyze/chasen

Get analysis results in ChaSen compatible format.

Request Body:

{
  "text": "日本語の文章を解析します"
}

Response:

{
  "original_text": "日本語の文章を解析します",
  "results": [...],
  "raw_output": "日本語\t名詞,一般,*,*,*,*,日本語,ニホンゴ,ニホンゴ\nの\t助詞,連体化,*,*,*,*,の,ノ,ノ\n..."
}
N-Best Analysis

POST /analyze/nbest

Get multiple possible parsing results (N-best solutions).

Request Body:

{
  "text": "今日もしないとね",
  "n_best": 2
}

Response:

{
  "original_text": "今日もしないとね",
  "n_best": 2,
  "results": [
    [
      {
        "surface": "今日",
        "pos": "名詞",
        "...": "..."
      }
    ],
    [
      {
        "surface": "今日",
        "pos": "名詞", 
        "...": "..."
      }
    ]
  ]
}
Batch Processing

POST /analyze/batch

Process multiple texts in a single request.

Request Body:

[
  "最初の文章です",
  "二つ目の文章です", 
  "三つ目の文章です"
]

Response:

{
  "results": [
    {
      "original_text": "最初の文章です",
      "results": [...]
    },
    {
      "original_text": "二つ目の文章です",
      "results": [...]
    },
    {
      "original_text": "三つ目の文章です", 
      "results": [...]
    }
  ]
}

Error Responses

400 Bad Request
{
  "error": "Text is required"
}
500 Internal Server Error
{
  "error": "MeCab execution failed"
}

Usage Examples

cURL Examples

Health Check:

curl http://localhost:8081/health

Basic Analysis:

curl -X POST http://localhost:8081/analyze \
  -H "Content-Type: application/json" \
  -d '{"text":"日本語のテストです"}'

Word Segmentation:

curl -X POST http://localhost:8081/analyze/wakati \
  -H "Content-Type: application/json" \
  -d '{"text":"太郎はこの本を二郎を見た女性に渡した"}'

N-Best Analysis:

curl -X POST http://localhost:8081/analyze/nbest \
  -H "Content-Type: application/json" \
  -d '{"text":"今日もしないとね", "n_best": 3}'
Go Client Example
package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "time"
)

type MeCabClient struct {
    BaseURL string
    Client  *http.Client
}

func NewMeCabClient(baseURL string) *MeCabClient {
    return &MeCabClient{
        BaseURL: baseURL,
        Client: &http.Client{Timeout: 30 * time.Second},
    }
}

func (c *MeCabClient) Analyze(text string) (map[string]interface{}, error) {
    reqBody := map[string]interface{}{"text": text}
    jsonData, _ := json.Marshal(reqBody)
    
    resp, err := c.Client.Post(c.BaseURL+"/analyze", "application/json", bytes.NewBuffer(jsonData))
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    
    var result map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&result)
    return result, nil
}

// Usage
func main() {
    client := NewMeCabClient("http://localhost:8081")
    result, _ := client.Analyze("日本語の文章")
    fmt.Println(result)
}

Rate Limiting

No rate limiting is currently implemented. For production use, consider implementing rate limiting based on your requirements.

Performance Considerations

  • The service uses a mutex to ensure thread-safe MeCab execution
  • Batch processing is available for multiple texts
  • Response times depend on text length and complexity
  • Typical response time: < 100ms for average sentences

Monitoring

The service includes:

  • Health check endpoint
  • Structured logging with zap
  • Log rotation with lumberjack
  • Docker health checks

Troubleshooting

Common Issues
  1. MeCab not available: Ensure the MeCab base image is built first
  2. Port conflicts: Check if port 8081 is available
  3. Encoding issues: Text should be UTF-8 encoded
Logs

Logs are stored in ./mecab-service/logs/mecab-service.log with rotation configured.

Health Check

Use the health endpoint to verify service status:

curl http://localhost:8081/health

Tag summary

Content type

Image

Digest

sha256:5f192ebf3

Size

50.4 MB

Last updated

about 1 year ago

docker pull simooowann/mecab-service:mecab-0.996