-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrephrase_for_target_polarity.py
executable file
·91 lines (84 loc) · 2.2 KB
/
rephrase_for_target_polarity.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# -*- coding: utf-8
from textblob import TextBlob, Word
import sys, os
from random import Random
random=Random()
poolsize=100
expandTries=1000
def guessSingular(w):
""" assume singular unless non-collective noun where form is equivalent to plural """
wW=Word(w)
ws=wW.singularize()
wp=Word(ws).pluralize()
singular=True
if(w.lower()!=ws.lower()):
if(w.lower()==wp.lower()):
singular=False
return singular
def tagFormatConvert(t):
pfx=t[0].lower()
if(pfx=="j"):
return "a"
return pfx
if(len(sys.argv)<3):
print("Usage: "+sys.argv[0]+" sentence target\nTranslates sentence into a (theoretically synonymous) new sentence with the same approximate sentiment as target (which may be a sentiment polarity value or a model sentence).")
sys.exit()
try:
target=float(sys.argv[2])
except:
target=TextBlob(sys.argv[2]).sentiment.polarity
sentence=sys.argv[1]
blob=TextBlob(sentence)
options=[]
optionSyns=[]
for tag in blob.tags:
word=Word(tag[0])
syns=[]
try:
syns=word.get_synsets(tagFormatConvert(tag[1]))
except:
syns=word.get_synsets()
syns_clean=[tag[0]]
singular=True
if(tag[1][0]=="N"):
singular=guessSingular(tag[0])
for syn in syns:
lemmanames=syn.lemma_names()
for name in lemmanames:
if(name.find(".")>=0):
name=name[:name.find(".")]
if(not singular):
name=Word(name).pluralize()
name=name.replace("_", " ")
syns_clean.append(name)
optionSyns.append(syns_clean)
i=0
while i<expandTries and len(options)<poolsize:
rephrase=[]
for word in optionSyns:
if(len(word)>1):
rephrase.append(random.choice(word))
else:
rephrase.append(word[0])
rephraseS=" ".join(rephrase)
if not (rephraseS in options):
options.append(rephraseS)
expandTries+=1
optionsByDelta={}
for option in options:
optionblob=TextBlob(option)
delta=abs(target-(optionblob.sentiment.polarity))
# print(str(delta)+"\t"+str(optionblob.sentiment.polarity)+"\t"+option)
if(delta in optionsByDelta):
optionsByDelta[delta].append(option)
else:
optionsByDelta[delta]=[option]
keys=optionsByDelta.keys()
best=optionsByDelta[min(keys)]
if(len(best)>1):
#print(str(min(keys))+"\t"+random.choice(best))
print(random.choice(best))
else:
#print(str(min(keys))+"\t"+best[0])
print(best[0])