-
Notifications
You must be signed in to change notification settings - Fork 4
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 #4 from ppirch/Tag-APi
Implement Tag API And Word Vector API
- Loading branch information
Showing
4 changed files
with
130 additions
and
13 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
fastapi | ||
pythainlp==2.1.4 | ||
uvicorn | ||
pytest | ||
pytest | ||
gensim | ||
numpy |
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 |
---|---|---|
@@ -1,15 +1,65 @@ | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter | ||
from pythainlp import tag | ||
from fastapi import APIRouter, Query | ||
from enum import Enum | ||
from typing import List, Optional, Tuple | ||
from pydantic import BaseModel | ||
from pythainlp import tag, tokenize | ||
from pythainlp.tag.named_entity import ThaiNameTagger | ||
|
||
router = APIRouter() | ||
ner = ThaiNameTagger() | ||
|
||
|
||
@router.get("/tag/pos_tag", tags=["tag"]) | ||
def part_of_speech_tagging(q: str, engine: str = None, corpus: str = None): | ||
words = pythainlp.tokenize.word_tokenize(q) | ||
if not engine: | ||
engine = "perceptron" | ||
if not corpus: | ||
corpus = "orchid" | ||
return tag.pos_tag(words, engine, corpus) | ||
class PosTagEngine(str, Enum): | ||
perceptron = "perceptron" | ||
unigram = "unigram" | ||
artagger = "artagger" | ||
|
||
|
||
class CorpusEngine(str, Enum): | ||
orchid = "orchid" | ||
orchid_ud = "orchid_ud" | ||
pud = "pud" | ||
|
||
|
||
class PosTag(BaseModel): | ||
word: str | ||
pos: str | ||
|
||
|
||
class NERTag(BaseModel): | ||
word: str | ||
pos: str | ||
ner: str | ||
|
||
|
||
class PosTagResponse(BaseModel): | ||
pos_tags: List[PosTag] = [] | ||
|
||
|
||
class NERResponse(BaseModel): | ||
ner_tags: List[NERTag] = [] | ||
|
||
|
||
@router.get('/pos', response_model=PosTagResponse) | ||
def pos_tag(q: str = "", words: List[str] = Query(None), engine: PosTagEngine = "perceptron", corpus: CorpusEngine = "orchid"): | ||
if len(q) != 0: | ||
words = tokenize.word_tokenize(q) | ||
tags = tag.pos_tag(words, engine=engine, corpus=corpus) | ||
res = [{"word": word, "pos": pos} for word, pos in tags] | ||
return {"pos_tags": res} | ||
|
||
|
||
@router.get('/provinces', response_model=PosTagResponse) | ||
def tag_provinces(q: str = "", words: List[str] = Query(None)): | ||
if len(q) != 0: | ||
words = tokenize.word_tokenize(q) | ||
tags = tag.tag_provinces(words) | ||
res = [{"word": word, "pos": pos} for word, pos in tags] | ||
return {"pos_tags": res} | ||
|
||
|
||
@router.get('/ner', response_model=NERResponse) | ||
def get_ner(q: str = ""): | ||
tags = ner.get_ner(q) | ||
res = [{"word": word, "pos": pos, "ner": ner} for word, pos, ner in tags] | ||
return {"ner_tags": res} |
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,63 @@ | ||
from fastapi import APIRouter, Query, HTTPException | ||
from pythainlp import word_vector | ||
from enum import Enum | ||
from typing import List, Optional | ||
from pydantic import BaseModel | ||
|
||
router = APIRouter() | ||
|
||
|
||
class MostSimilarCosmulWord(BaseModel): | ||
word: str | ||
score: float | ||
|
||
|
||
class DoesntMatchResponse(BaseModel): | ||
doesnt_match: str = "" | ||
|
||
|
||
class MostSimilarCosmulResponse(BaseModel): | ||
most_similar_cosmul: List[MostSimilarCosmulWord] = [] | ||
|
||
|
||
class SentenceVectorizerResponse(BaseModel): | ||
sentence_vectorizer: List[List[float]] = [] | ||
|
||
|
||
class SimilarityResponse(BaseModel): | ||
similarity: float = -1 | ||
|
||
|
||
@router.get('/doesnt-match', response_model=DoesntMatchResponse) | ||
def doesnt_match(words: List[str] = Query(None)): | ||
try: | ||
return {"doesnt_match": word_vector.doesnt_match(words)} | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e).replace("\"", "")) | ||
|
||
|
||
@router.get('/most-similar-cosmul', response_model=MostSimilarCosmulResponse) | ||
def most_similar_cosmul(listPositive: List[str] = Query([]), listNegative: List[str] = Query([])): | ||
try: | ||
words = word_vector.most_similar_cosmul(listPositive, listNegative) | ||
res = [{"word": word, "score": score} for word, score in words] | ||
return {"most_similar_cosmul": res} | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e).replace("\"", "")) | ||
|
||
|
||
@router.get('/sentence-vectorizer', response_model=SentenceVectorizerResponse) | ||
def sentence_vectorizer(q: str = ""): | ||
try: | ||
vector = word_vector.sentence_vectorizer(q, use_mean=True).tolist() | ||
return {"sentence_vectorizer": vector} | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e).replace("\"", "")) | ||
|
||
|
||
@router.get('/similarity', response_model=SimilarityResponse) | ||
def similarity(word1: str = "", word2: str = ""): | ||
try: | ||
return {"similarity": word_vector.similarity(word1, word2)} | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e).replace("\"", "")) |