-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnondescript.py
executable file
·70 lines (66 loc) · 2.99 KB
/
nondescript.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
# Outputs two texts:
# changewords(text)[0] has suggestions for replacing words (human-directed).
# changewords(text)[1] has randomly replaced words (automatic).
from random import randint
from nltk.corpus import wordnet as wn
from nltk import word_tokenize as tok
from wordfilter import Wordfilter
wf = Wordfilter() #https://github.com/dariusk/wordfilter
#Words that may be offensive or that have undesirable results in WordNet:
ignore = ['will','more','must','there','john','screw','queer','crap','shit','ass','fuck','fucker','motherfucker','fucks','fucked','fucking']
def changewords(text):
"""Returns two texts [T1, T2]: T1 text with certain words (in all caps) followed by
potential synonyms in parentheses, T2 text with randomly-chosen synonyms in all caps
that replace certain words."""
i = 0
text = text.split()
textprint = [] #Text will appear as so: she SHOUTED (shout out, call...
luckyprint = [] #Random synonym will be chosen, as so: She shout out... (No tense consideration)
for w in text:
w = w.lower()
syn = wn.synsets(w)
if wf.blacklisted(w) == True: #do nothing with bad words
textprint.append(w)
luckyprint.append(w)
elif len(w) < 3: #do nothing with words with only a few synsets
textprint.append(w)
luckyprint.append(w)
elif w.lower() in ignore: #do nothing with bad words
textprint.append(w)
luckyprint.append(w)
elif 2 < len(syn) < 8: #only consider words with 3-7 synsets
wlist = []
for s in range(len(syn)-1):
try:
newall = syn[s].lemma_names() #mysterious errors
except:
newall = syn[s].lemma_names
for new in newall:
if new.lower() == w: #avoid same word
pass
elif new in wlist: #avoid duplicates
pass
elif new[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": #avoid names
pass
elif wf.blacklisted(new) == True: #avoid bad words
pass
else:
if '_' in new:
new = new.split('_') #multi-word expressions
new = ' '.join(new)
wlist.append(new)
wprint = ' (' #link together the parenthetical list of suggestions
for n in wlist[1:]:
wprint += (n + ', ')
r = randint(0,len(wlist)-1)
randword = wlist[r] #choose random word for luckyprint
if len(wlist) < 3:
textprint.append(w)
luckyprint.append(w)
else:
textprint.append((w.upper() + wprint[:-2] + ')'))
luckyprint.append(randword.upper())
else:
textprint.append(w)
luckyprint.append(w)
return [' '.join(textprint), ' '.join(luckyprint)]