habulaj commited on
Commit
23fbfed
·
verified ·
1 Parent(s): c64b720

Update routers/summarizer.py

Browse files
Files changed (1) hide show
  1. routers/summarizer.py +38 -8
routers/summarizer.py CHANGED
@@ -1,13 +1,14 @@
1
- import nltk
 
2
  from textblob import TextBlob
 
 
3
 
4
  # Baixa os corpora necessários para o TextBlob
5
  nltk.download('punkt')
6
  nltk.download('averaged_perceptron_tagger')
7
 
8
- # Agora a API pode usar o TextBlob normalmente
9
- from fastapi import APIRouter, Query, HTTPException
10
-
11
  router = APIRouter()
12
 
13
  @router.get("/grammar/correct/")
@@ -16,19 +17,48 @@ def correct_grammar(
16
  ):
17
  """
18
  Corrige erros gramaticais e ortográficos no texto e retorna sugestões.
19
- Suporta correção em inglês.
20
  """
21
  try:
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Cria um objeto TextBlob para o texto fornecido
23
  blob = TextBlob(text)
24
 
25
  # Corrige o texto automaticamente
26
  corrected_text = blob.correct()
27
 
28
- # Retorna o texto corrigido
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  return {
30
  "original_text": text,
31
- "corrected_text": str(corrected_text)
 
 
 
 
32
  }
33
 
34
  except Exception as e:
@@ -36,4 +66,4 @@ def correct_grammar(
36
  raise HTTPException(
37
  status_code=500,
38
  detail=f"An error occurred during grammar correction: {str(e)}"
39
- )
 
1
+
2
+ from fastapi import APIRouter, Query, HTTPException
3
  from textblob import TextBlob
4
+ from langdetect import detect
5
+ import nltk
6
 
7
  # Baixa os corpora necessários para o TextBlob
8
  nltk.download('punkt')
9
  nltk.download('averaged_perceptron_tagger')
10
 
11
+ # Cria um roteador para a API de correção gramatical
 
 
12
  router = APIRouter()
13
 
14
  @router.get("/grammar/correct/")
 
17
  ):
18
  """
19
  Corrige erros gramaticais e ortográficos no texto e retorna sugestões.
20
+ Suporta correção em inglês e detecta o idioma do texto.
21
  """
22
  try:
23
+ # Detecta o idioma do texto
24
+ detected_language = detect(text)
25
+
26
+ # Se o texto não estiver em inglês, retorna uma mensagem indicando que não há suporte para correção
27
+ if detected_language != 'en':
28
+ return {
29
+ "original_text": text,
30
+ "corrected_text": text,
31
+ "detected_language": detected_language,
32
+ "message": "Grammatical correction is only supported for English text."
33
+ }
34
+
35
  # Cria um objeto TextBlob para o texto fornecido
36
  blob = TextBlob(text)
37
 
38
  # Corrige o texto automaticamente
39
  corrected_text = blob.correct()
40
 
41
+ # Conta o número de palavras antes e depois da correção
42
+ original_word_count = len(text.split())
43
+ corrected_word_count = len(str(corrected_text).split())
44
+
45
+ # Adiciona detalhes sobre as correções, como palavras corrigidas
46
+ corrections = []
47
+ for word, corrected_word in zip(text.split(), str(corrected_text).split()):
48
+ if word != corrected_word:
49
+ corrections.append({
50
+ "original_word": word,
51
+ "corrected_word": corrected_word
52
+ })
53
+
54
+ # Retorna o texto corrigido, idioma detectado e detalhes adicionais
55
  return {
56
  "original_text": text,
57
+ "corrected_text": str(corrected_text),
58
+ "detected_language": detected_language,
59
+ "original_word_count": original_word_count,
60
+ "corrected_word_count": corrected_word_count,
61
+ "corrections": corrections
62
  }
63
 
64
  except Exception as e:
 
66
  raise HTTPException(
67
  status_code=500,
68
  detail=f"An error occurred during grammar correction: {str(e)}"
69
+ )