|
from fastapi import APIRouter, Query, HTTPException |
|
from textblob import TextBlob |
|
from langdetect import detect |
|
import time |
|
|
|
|
|
router = APIRouter() |
|
|
|
@router.get("/textclas/") |
|
def analyze_text( |
|
text: str = Query(..., description="Texto a ser analisado") |
|
): |
|
""" |
|
Analisa o texto fornecido para sentimento, correção ortográfica, |
|
contagem de palavras e sentenças, e detecta o idioma. |
|
""" |
|
try: |
|
|
|
blob = TextBlob(text) |
|
|
|
|
|
start_time = time.time() |
|
|
|
|
|
sentiment = blob.sentiment |
|
polarity = sentiment.polarity |
|
subjectivity = sentiment.subjectivity |
|
sentiment_label = "Neutral" |
|
if polarity > 0: |
|
sentiment_label = "Positive" |
|
elif polarity < 0: |
|
sentiment_label = "Negative" |
|
|
|
|
|
corrected_text = str(blob.correct()) |
|
|
|
|
|
word_count = len(text.split()) |
|
sentence_count = len(blob.sentences) |
|
|
|
|
|
detected_language = detect(text) |
|
|
|
|
|
end_time = time.time() |
|
processing_time = round(end_time - start_time, 4) |
|
|
|
|
|
return { |
|
"original_text": text, |
|
"corrected_text": corrected_text, |
|
"sentiment": { |
|
"polarity": polarity, |
|
"subjectivity": subjectivity, |
|
"label": sentiment_label |
|
}, |
|
"word_count": word_count, |
|
"sentence_count": sentence_count, |
|
"detected_language": detected_language, |
|
"processing_time_seconds": processing_time |
|
} |
|
|
|
except Exception as e: |
|
|
|
raise HTTPException( |
|
status_code=500, |
|
detail=f"An error occurred during text analysis: {str(e)}" |
|
) |