forked from institutoazmina/elasnocongressobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize_tweets.py
70 lines (49 loc) · 2.24 KB
/
normalize_tweets.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
import re
def norm(text):
text = text.strip(); # trim
text = removeNone(text)
text = truncLongName(text)
return text
def removeNone(text):
newText = text.replace('\n📕 Nome: None.', '').replace( '\n🕙 Última atualização: None.', '').replace( '\n↪️ Situação: None.', '').replace('\n🔈 Tramitação: None.', '').replace(', de autoria de None,', '').replace('Atualização: None.', '')
return newText
def removeEmoji(text):
newText = text.replace('📕', '').replace( '🕙', '').replace( '↪️', '').replace('🔈', '').replace('🔗', '').replace('💡', '')
return newText
def repNameV1(m):
names = m.group(2).split(', ')
# mais de um author
if (len(names) > 1):
return m.group(1) + names[0] + ' e outros(as)' + m.group(3)
return m.group(1) + names[0] + m.group(3)
def repNameV2(m):
names = m.group(2).split(', ')
firstName = names[0]
if (len(names) > 1):
if (len(firstName) > 20):
nameParts = firstName.split(' ')
firstName = ' '.join(nameParts[0:3]) + '…'
return m.group(1) + firstName + ' e outros(as)' + m.group(3)
if (len(firstName) > 20):
nameParts = firstName.split(' ')
firstName = ' '.join(nameParts[0:3]) + '…'
return m.group(1) + firstName + m.group(3)
def truncLongName(text):
if (len(text) < 280):
return text # no mods needed
newText = re.sub(r"(de autoria de )(.*)(, fala sobre)", repNameV1, text)
# try again with without "e outros...")
if (len(newText) > 280):
newText = re.sub(r"(de autoria de )(.*)(, fala sobre)", repNameV2, text)
# if still too long, remove a lot of info
if (len(newText) > 280):
newText = re.sub(r"de autoria de.*sofreu alterações em sua tramitação\. ", '', text)
if (len(newText) > 280):
newText = newText.replace("fala sobre o tema", "fala sobre")
if (len(newText) > 280):
newText = newText.replace("e sofreu alterações em sua tramitação", "teve alterações na tramitação")
if (len(newText) > 280):
newText = text.replace('🕙', '').replace('↪️', '').replace('🔗', '').replace('🔈', '')
if (len(newText) > 280):
newText = text.replace('\n', '')
return newText