This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaketweet.py
76 lines (66 loc) · 2.32 KB
/
maketweet.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
75
76
import re
from os import getenv
import string
from itertools import chain
from random import choice
from google_language import get_entities
from models.corpus import update_corpus, update_count
def tweet_maker(the_tweet):
entities = get_entities(the_tweet)
the_new_tweet = the_tweet
the_string = the_tweet.translate(
str.maketrans('', '', string.punctuation)
).split()
update_count(the_string)
update_count(
[f'{the_string[i - 1]} {x}' for i, x in enumerate(the_string)][1]
)
for word, etype in entities.items():
if word == '&':
continue
update_corpus(word, etype)
if '@' in word:
continue
combos = list(chain.from_iterable(
[feed_me(repl, word, etype) for repl in get_repls()]
))
replacer = choice(combos)
the_new_tweet = the_new_tweet.replace(word, replacer)
the_new_tweet = the_new_tweet.replace('&', '&')
return replace_url(the_new_tweet)
def replace_url(in_string):
url = re.compile("http.+(?=\s|$)")
return url.sub(
"https://www.youtube.com/watch?v=zWHu95io9B4 ",
in_string
)
def get_repls():
return getenv('REPLACEMENTS').split(',')
def feed_me(repl, origin, entity):
return {
'UNKNOWN': lambda repl, orig: [orig],
'PERSON': lambda repl, orig: (
[x.title() for x in [f'{repl} man', f'{repl}', orig, ]]
if orig.title() == orig
else [f'{repl} man', f'{repl}', orig, ]
),
'LOCATION': lambda repl, orig: (
[x.title() for x in [f'{repl} land', f'{repl} city', ]]
if orig.title() == orig
else [f'{repl} land', f'{repl} city', ]
),
'ORGANIZATION': lambda repl, orig: [
f'{repl.title()} Conservancy',
f'Department of {repl.title()}',
f'{repl.title()} Association',
],
'EVENT': lambda repl, orig: [
f'{repl.title()} Day',
f'war of {repl.title()}',
],
'WORK_OF_ART': lambda repl, orig: [orig],
'CONSUMER_GOOD': lambda repl, orig: [f'{repl}'],
'OTHER': lambda repl, orig: [orig, f'{repl}', f'{repl}', ],
}.get(entity, lambda repl, orig: f'{repl}')(repl, origin)
if __name__ == '__main__':
print(tweet_maker(input('Enter some text:')))