-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigrama_trigrama_console.py
75 lines (50 loc) · 1.75 KB
/
bigrama_trigrama_console.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import collections
#Para recursos não encontrados:
import nltk
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('rslp')
# coding=utf-8
# Importação das Bibliotecas
from nltk.tokenize import sent_tokenize
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
with open('memorial_de_ayres.txt', 'r', encoding="utf-8") as myfile:
data=myfile.read().replace('\n', ' ')
txtfiltrado = [w for w in word_tokenize(data.replace(',',' ').replace('.',' '))]
def bigramas(fonte, palavras):
bigrams = []
suggestions = []
for i in range(0, len(fonte)):
if (i == len(fonte)-1):
break
else:
if(fonte[i].lower()==palavras[1].lower() and fonte[i-1].lower()==palavras[0].lower()):
bigrams.append(fonte[i+1])
counter = collections.Counter(bigrams)
for i in range(0,len(counter.most_common())):
if(i>=3):
break
else:
suggestions.append(counter.most_common()[i][0])
return suggestions
def trigramas(fonte, palavras):
trigrams = []
suggestions = []
for i in range(0, len(fonte)):
if (i == len(fonte)-2):
break
else:
if(fonte[i].lower()==palavras[2].lower()
and fonte[i-1].lower()==palavras[1].lower()
and fonte[i-2].lower()==palavras[0].lower()):
trigrams.append(fonte[i+1])
counter = collections.Counter(trigrams)
for i in range(0,len(counter.most_common())):
if(i>=3):
break
else:
suggestions.append(counter.most_common()[i][0])
return suggestions
print(bigramas(txtfiltrado, ['a','casa']))
print(trigramas(txtfiltrado, ['podia','ser','um']))