-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcion_resumen_texto.py
50 lines (40 loc) · 1.35 KB
/
funcion_resumen_texto.py
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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 3 09:03:33 2021
@author: Pablo
"""
# Funcionalidad resumen
import nltk
import string
from heapq import nlargest
#%%
# Manejo funcionalidad
def resumelo(text):
if text.count(". ") > 20:
length = int(round(text.count(". ")/10, 0))
else:
length = 1
nopuch =[char for char in text if char not in string.punctuation]
nopuch = "".join(nopuch)
processed_text = [word for word in nopuch.split() if word.lower() not in nltk.corpus.stopwords.words('english')]
word_freq = {}
for word in processed_text:
if word not in word_freq:
word_freq[word] = 1
else:
word_freq[word] = word_freq[word] + 1
max_freq = max(word_freq.values())
for word in word_freq.keys():
word_freq[word] = (word_freq[word]/max_freq)
sent_list = nltk.sent_tokenize(text)
sent_score = {}
for sent in sent_list:
for word in nltk.word_tokenize(sent.lower()):
if word in word_freq.keys():
if sent not in sent_score.keys():
sent_score[sent] = word_freq[word]
else:
sent_score[sent] = sent_score[sent] + word_freq[word]
summary_sents = nlargest(length, sent_score, key=sent_score.get)
summary = " ".join(summary_sents)
return summary