-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
52 lines (40 loc) · 1.68 KB
/
app.py
File metadata and controls
52 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, request, jsonify
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import re
app = Flask(__name__)
# Função para dividir o texto em sentenças
def split_into_sentences(text):
sentencas = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
return [s.strip() for s in sentencas if s.strip()]
# Carregar o documento de texto
with open('internet.txt', 'r', encoding='utf-8') as file:
document = file.read()
# Dividir o documento em sentenças
sentencas = split_into_sentences(document)
# Criar o vetor TF-IDF para as sentenças
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(sentencas)
def answer_question(question, threshold=0.2):
# Pré-processar a pergunta
question = question.lower().strip()
# Converter a pergunta para o formato TF-IDF
question_tfidf = vectorizer.transform([question])
# Calcular similaridade de cosseno entre a pergunta e as sentenças do documento
similarity = cosine_similarity(question_tfidf, tfidf_matrix)
# Encontrar a sentença mais similar
most_similar_index = np.argmax(similarity)
# Verificar se a similaridade é alta o suficiente
if similarity[0, most_similar_index] >= threshold:
return sentencas[most_similar_index]
else:
return "Desculpe, não consegui encontrar uma resposta adequada."
@app.route('/do_answer', methods=['POST'])
def do_answer():
data = request.json
question = data.get('pergunta', '')
answer = answer_question(question)
return jsonify({'resposta': answer})
if __name__ == '__main__':
app.run(debug=True)