Upload 5 files
Browse files- conceptnet_antonym.txt +0 -0
- conceptnet_entity.csv +0 -0
- gen_train_data.py +344 -0
- get_vocab.py +56 -0
- negation.txt +33 -0
conceptnet_antonym.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
conceptnet_entity.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
gen_train_data.py
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import random
|
| 3 |
+
import re
|
| 4 |
+
import copy
|
| 5 |
+
from nltk.corpus import stopwords
|
| 6 |
+
import nltk
|
| 7 |
+
pos_tag = nltk.pos_tag
|
| 8 |
+
from nltk.stem import WordNetLemmatizer
|
| 9 |
+
lemma = WordNetLemmatizer().lemmatize
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
function_word = [".", ",", "!", "?", "male", "female", "neutral"]
|
| 13 |
+
def get_avail_phrases():
|
| 14 |
+
sw = set(stopwords.words('english'))
|
| 15 |
+
avail_phrases = set()
|
| 16 |
+
fin = open("./conceptnet_entity.csv", 'r')
|
| 17 |
+
for i, line in enumerate(fin):
|
| 18 |
+
avail_phrases.add(' '.join(line.strip().split("|||")[:-1]))
|
| 19 |
+
avail_phrases = avail_phrases - sw
|
| 20 |
+
fin.close()
|
| 21 |
+
|
| 22 |
+
fin = open("./negation.txt", 'r')
|
| 23 |
+
negation_word = []
|
| 24 |
+
for i, line in enumerate(fin):
|
| 25 |
+
word = ' '.join(line.strip().split()[1:])
|
| 26 |
+
negation_word.append(word)
|
| 27 |
+
avail_phrases.add(word)
|
| 28 |
+
fin.close()
|
| 29 |
+
|
| 30 |
+
for w in function_word:
|
| 31 |
+
avail_phrases.add(w)
|
| 32 |
+
|
| 33 |
+
with open("avail_phrases.txt", "w") as fout:
|
| 34 |
+
for w in avail_phrases:
|
| 35 |
+
fout.write(w+"\n")
|
| 36 |
+
return avail_phrases, negation_word
|
| 37 |
+
|
| 38 |
+
avail_phrases, negation_word = get_avail_phrases()
|
| 39 |
+
|
| 40 |
+
def output(st, fout):
|
| 41 |
+
if "w" in data_dir:
|
| 42 |
+
fout.write(" ".join(st)+"\n")
|
| 43 |
+
else:
|
| 44 |
+
for sen in st:
|
| 45 |
+
fout.write(sen+"\n")
|
| 46 |
+
fout.write("-"*5+"\n")
|
| 47 |
+
|
| 48 |
+
def repeat_sentence(st):
|
| 49 |
+
# repeat one sentence and delete the original sentence
|
| 50 |
+
idx = np.random.choice(np.arange(len(st))[1:], 1 + int(len(st)/2), replace=False).tolist()
|
| 51 |
+
s = min(idx)
|
| 52 |
+
tmp_st = copy.deepcopy(st)
|
| 53 |
+
for l in idx:
|
| 54 |
+
tmp_st[l] = copy.deepcopy(tmp_st[s])
|
| 55 |
+
return tmp_st
|
| 56 |
+
|
| 57 |
+
def repeat_ngram(st):
|
| 58 |
+
# repeat ngram in one sentence 1~4
|
| 59 |
+
def repeat_sen_gram(st):
|
| 60 |
+
flag = True
|
| 61 |
+
for _ in range(10):
|
| 62 |
+
try:
|
| 63 |
+
idx = np.random.choice(np.arange(len(st))[1:])
|
| 64 |
+
gram_num = np.random.choice(np.arange(5)[1:])
|
| 65 |
+
split_sen = st[idx].strip().split()
|
| 66 |
+
pointer_st = np.random.choice(np.arange(len(split_sen)))
|
| 67 |
+
pointer_ed = pointer_st + gram_num
|
| 68 |
+
if pointer_ed > len(split_sen):
|
| 69 |
+
pointer_ed = pointer_st
|
| 70 |
+
pointer_st = pointer_ed - gram_num
|
| 71 |
+
if pointer_st < 0:
|
| 72 |
+
continue
|
| 73 |
+
else:
|
| 74 |
+
flag = False
|
| 75 |
+
break
|
| 76 |
+
except:
|
| 77 |
+
continue
|
| 78 |
+
if flag:
|
| 79 |
+
return copy.deepcopy(st)
|
| 80 |
+
sen1, sen2, sen3 = " ".join(split_sen[:pointer_st]), " ".join(split_sen[pointer_st:pointer_ed]), " ".join(split_sen[pointer_ed:])
|
| 81 |
+
tmp_st = copy.deepcopy(st)
|
| 82 |
+
tmp_st[idx] = " ".join([sen1, sen2, sen2, sen3]).strip()
|
| 83 |
+
return tmp_st
|
| 84 |
+
for i in range(int(len(st)/2)):
|
| 85 |
+
st = repeat_sen_gram(st)
|
| 86 |
+
return st
|
| 87 |
+
|
| 88 |
+
def replace_sentence(st):
|
| 89 |
+
flag = True
|
| 90 |
+
for _ in range(10):
|
| 91 |
+
try:
|
| 92 |
+
tmp_st = copy.deepcopy(st)
|
| 93 |
+
idxs = np.random.choice(np.arange(len(st))[1:], np.random.choice(np.arange(1, len(st))), replace=False)
|
| 94 |
+
replace_st_id = np.random.choice(np.arange(len(story)))
|
| 95 |
+
for idx in idxs:
|
| 96 |
+
tmp_st[idx] = np.random.choice(story[replace_st_id])
|
| 97 |
+
flag = False
|
| 98 |
+
break
|
| 99 |
+
except:
|
| 100 |
+
continue
|
| 101 |
+
if flag:
|
| 102 |
+
return copy.deepcopy(st)
|
| 103 |
+
return tmp_st
|
| 104 |
+
|
| 105 |
+
def change_neg_helper(sen):
|
| 106 |
+
def pro(s):
|
| 107 |
+
final_sen = " ".join(s)
|
| 108 |
+
return final_sen
|
| 109 |
+
sen = sen.strip().split()
|
| 110 |
+
for i, n in enumerate(sen):
|
| 111 |
+
if n in negation_word:
|
| 112 |
+
del sen[i]
|
| 113 |
+
return pro(sen)
|
| 114 |
+
neg_list = ["not", "n't"]
|
| 115 |
+
for i, n in enumerate(sen):
|
| 116 |
+
if n in ["would", "will", "can", "could", "may", "might", "shall", "should", "do", "does", "did", "am", "is", "are", "was", "were", "be", "been"]:
|
| 117 |
+
sen.insert(i+1, np.random.choice(neg_list))
|
| 118 |
+
return pro(sen)
|
| 119 |
+
pos_sen = pos_tag(sen)
|
| 120 |
+
for i, n in enumerate(pos_sen):
|
| 121 |
+
if n[1] == "VB":
|
| 122 |
+
sen.insert(i, "do " + np.random.choice(neg_list))
|
| 123 |
+
return pro(sen)
|
| 124 |
+
elif n[1] == "VBD":
|
| 125 |
+
sen[i] = lemma(sen[i], "v")
|
| 126 |
+
sen.insert(i, "did " + np.random.choice(neg_list))
|
| 127 |
+
return pro(sen)
|
| 128 |
+
elif n[1] == "VBG":
|
| 129 |
+
sen.insert(i, np.random.choice(neg_list))
|
| 130 |
+
return pro(sen)
|
| 131 |
+
elif n[1] == "VBN":
|
| 132 |
+
sen.insert(i, np.random.choice(neg_list))
|
| 133 |
+
return pro(sen)
|
| 134 |
+
elif n[1] == "VBP":
|
| 135 |
+
sen.insert(i, "do " + np.random.choice(neg_list))
|
| 136 |
+
return pro(sen)
|
| 137 |
+
elif n[1] == "VBZ":
|
| 138 |
+
sen[i] = lemma(sen[i], "v")
|
| 139 |
+
sen.insert(i, "does " + np.random.choice(neg_list))
|
| 140 |
+
return pro(sen)
|
| 141 |
+
print("VERB ERROR")
|
| 142 |
+
return None
|
| 143 |
+
|
| 144 |
+
anotomy_word = {}
|
| 145 |
+
all_num, anotomy_num = 0, 0
|
| 146 |
+
with open("./conceptnet_antonym.txt", "r") as fin:
|
| 147 |
+
for line in fin:
|
| 148 |
+
tmp = line.strip().split("|||")
|
| 149 |
+
if len(tmp) == 3:
|
| 150 |
+
h, t = tmp[0], tmp[2].split()
|
| 151 |
+
if h in anotomy_word:
|
| 152 |
+
anotomy_word[h] += t
|
| 153 |
+
else:
|
| 154 |
+
anotomy_word[h] = t[:]
|
| 155 |
+
|
| 156 |
+
def change_neg_sentence(st):
|
| 157 |
+
flag = True
|
| 158 |
+
for _ in range(10):
|
| 159 |
+
try:
|
| 160 |
+
tmp_st = copy.deepcopy(st)
|
| 161 |
+
idxs = np.random.choice(np.arange(len(st))[1:], np.random.choice(np.arange(1, len(st))), replace=False)
|
| 162 |
+
for idx in idxs:
|
| 163 |
+
tmp_st_idx = change_neg_helper(st[idx])
|
| 164 |
+
if tmp_st_idx is not None:
|
| 165 |
+
tmp_st[idx] = tmp_st_idx
|
| 166 |
+
flag = False
|
| 167 |
+
if flag == False:
|
| 168 |
+
break
|
| 169 |
+
except:
|
| 170 |
+
continue
|
| 171 |
+
if flag:
|
| 172 |
+
return copy.deepcopy(st)
|
| 173 |
+
return tmp_st
|
| 174 |
+
|
| 175 |
+
def replace_word(st):
|
| 176 |
+
global all_num, anotomy_num
|
| 177 |
+
def replace_one_word(st):
|
| 178 |
+
anotomy = False
|
| 179 |
+
flag = True
|
| 180 |
+
for _ in range(100):
|
| 181 |
+
tmp_st = copy.deepcopy(st)
|
| 182 |
+
idx = np.random.choice(np.arange(len(st))[1:])
|
| 183 |
+
split_sen = tmp_st[idx].split()
|
| 184 |
+
pos_split_sen = pos_tag(split_sen)
|
| 185 |
+
avail_w_id = []
|
| 186 |
+
for w_id, w in enumerate(split_sen):
|
| 187 |
+
if (w in avail_phrases and w not in function_word and "[" not in w):
|
| 188 |
+
avail_w_id.append(w_id)
|
| 189 |
+
if len(avail_w_id) == 0: continue
|
| 190 |
+
word_id = np.random.choice(avail_w_id)
|
| 191 |
+
if pos_split_sen[word_id][1] not in pos_vocab_entity: continue
|
| 192 |
+
lemma_word = lemma(pos_split_sen[word_id][0], 'v' if pos_split_sen[word_id][1][0] == 'V' else 'n')
|
| 193 |
+
if lemma_word in anotomy_word:
|
| 194 |
+
replace_word = np.random.choice(anotomy_word[lemma_word])
|
| 195 |
+
anotomy = True
|
| 196 |
+
else:
|
| 197 |
+
word_freq = pos_vocab_entity[pos_split_sen[word_id][1]]
|
| 198 |
+
replace_word = ""
|
| 199 |
+
flag_in = True
|
| 200 |
+
for _ in range(10):
|
| 201 |
+
replace_word = np.random.choice(word_freq["word"], p=word_freq["freq"]/np.sum(word_freq["freq"]))
|
| 202 |
+
if len(word_freq["word"]) == 1 or replace_word != pos_split_sen[word_id][0]:
|
| 203 |
+
flag_in = False
|
| 204 |
+
break
|
| 205 |
+
if flag_in:
|
| 206 |
+
replace_word = pos_split_sen[word_id][0]
|
| 207 |
+
anotomy = False
|
| 208 |
+
tmp_split_sen = copy.deepcopy(split_sen)
|
| 209 |
+
split_sen[word_id] = replace_word
|
| 210 |
+
tmp_st[idx] = " ".join(split_sen)
|
| 211 |
+
flag = False
|
| 212 |
+
break
|
| 213 |
+
if flag:
|
| 214 |
+
return copy.deepcopy(st), False
|
| 215 |
+
return tmp_st, anotomy
|
| 216 |
+
num = 0
|
| 217 |
+
for idx in np.arange(len(st))[1:]:
|
| 218 |
+
for word in st[idx].split():
|
| 219 |
+
if word in avail_phrases:
|
| 220 |
+
num += 1
|
| 221 |
+
try:
|
| 222 |
+
final_num = np.random.choice(np.arange(1, int(num*0.15+1)))
|
| 223 |
+
except:
|
| 224 |
+
final_num = 1
|
| 225 |
+
for _ in range(final_num):
|
| 226 |
+
st, anotomy = replace_one_word(st)
|
| 227 |
+
all_num += 1
|
| 228 |
+
if anotomy: anotomy_num += 1
|
| 229 |
+
return st
|
| 230 |
+
|
| 231 |
+
def shuffle_sentence(st, n_sentence):
|
| 232 |
+
def exchange(l, ids, target_ids):
|
| 233 |
+
tmp_l = copy.deepcopy(l)
|
| 234 |
+
for o_id, t_id in zip(ids, target_ids):
|
| 235 |
+
tmp_l[o_id] = copy.deepcopy(l[t_id])
|
| 236 |
+
return tmp_l
|
| 237 |
+
# exchange n sentences
|
| 238 |
+
flag = True
|
| 239 |
+
for _ in range(10):
|
| 240 |
+
sen_ids = np.random.choice(np.arange(len(st))[1:], n_sentence, replace=False)
|
| 241 |
+
target_ids = np.random.permutation(sen_ids)
|
| 242 |
+
tmp_st = exchange(st, sen_ids, target_ids)
|
| 243 |
+
if st != tmp_st:
|
| 244 |
+
flag = False
|
| 245 |
+
break
|
| 246 |
+
if flag:
|
| 247 |
+
return copy.deepcopy(st)
|
| 248 |
+
return tmp_st
|
| 249 |
+
def get_pos_vocab(dir):
|
| 250 |
+
pos_vocab_entity = {}
|
| 251 |
+
with open("%s/entity_vocab.txt"%dir, "r") as fin:
|
| 252 |
+
for line in fin:
|
| 253 |
+
tmp = line.strip().split("|||")
|
| 254 |
+
word = tmp[0].split()[0]
|
| 255 |
+
pos = tmp[1:]
|
| 256 |
+
for p in pos:
|
| 257 |
+
pp = p.split()
|
| 258 |
+
if pp[0] in pos_vocab_entity:
|
| 259 |
+
pos_vocab_entity[pp[0]]["word"].append(word)
|
| 260 |
+
pos_vocab_entity[pp[0]]["freq"].append(float(pp[1]))
|
| 261 |
+
else:
|
| 262 |
+
pos_vocab_entity[pp[0]] = {"word":[word], "freq":[float(pp[1])]}
|
| 263 |
+
return pos_vocab_entity
|
| 264 |
+
# ========================================================================================
|
| 265 |
+
|
| 266 |
+
name_list = ["test", "dev", "train"]
|
| 267 |
+
data_dir = "./%s/ini_data"%("WritingPrompts" if "w" in sys.argv[1] else "ROCStories")
|
| 268 |
+
output_dir = "%s/train_data"%("WritingPrompts" if "w" in sys.argv[1] else "ROCStories")
|
| 269 |
+
|
| 270 |
+
# type_dict = {"repeat":0.6, "replace":0.15, "shuffle":0.15, "neg":0.1}
|
| 271 |
+
type_dict = {"repeat":0.1, "replace":0.3, "shuffle":0.4, "neg":0.2}
|
| 272 |
+
type_list = list(type_dict.keys())
|
| 273 |
+
type_prob_list = []
|
| 274 |
+
for t in type_list:
|
| 275 |
+
type_prob_list.append(type_dict[t])
|
| 276 |
+
|
| 277 |
+
time_list = [1,2,3,4]
|
| 278 |
+
# time_prob_list = [0.2,0.4,0.3,0.1]
|
| 279 |
+
time_prob_list = [0.5,0.2,0.2,0.1]
|
| 280 |
+
|
| 281 |
+
pos_vocab_entity = get_pos_vocab(data_dir)
|
| 282 |
+
for name in name_list:
|
| 283 |
+
if "w" in data_dir.lower():
|
| 284 |
+
with open("%s/%s.wp_source"%(data_dir, name), "r") as fin1:
|
| 285 |
+
with open("%s/%s.wp_target"%(data_dir, name), "r") as fin2:
|
| 286 |
+
story, tmp = [], []
|
| 287 |
+
for k, line in enumerate(fin2):
|
| 288 |
+
src = fin1.readline().strip()
|
| 289 |
+
if src[-1].isalpha():
|
| 290 |
+
src = src + " ."
|
| 291 |
+
tmp.append(src)
|
| 292 |
+
for sen in line.strip().split(".")[:-1]:
|
| 293 |
+
if sen.strip() != "":
|
| 294 |
+
tmp.append(sen.strip()+" .")
|
| 295 |
+
if len(tmp) >= 4:
|
| 296 |
+
story.append(tmp)
|
| 297 |
+
tmp = []
|
| 298 |
+
else:
|
| 299 |
+
with open("%s/%s.txt"%(data_dir, name), "r") as fin:
|
| 300 |
+
story, tmp = [], []
|
| 301 |
+
for k, line in enumerate(fin):
|
| 302 |
+
i = k + 1
|
| 303 |
+
if i % 6 == 0:
|
| 304 |
+
story.append(tmp)
|
| 305 |
+
tmp = []
|
| 306 |
+
else:
|
| 307 |
+
sen = line.strip()
|
| 308 |
+
tmp.append(sen+" ." if sen[-1].isalpha() else sen)
|
| 309 |
+
|
| 310 |
+
with open("%s/%s_human.txt"%(output_dir, name), "w") as fout:
|
| 311 |
+
for st_id, st in enumerate(story):
|
| 312 |
+
output(st, fout)
|
| 313 |
+
|
| 314 |
+
prefix = "%s/%s_negative"%(output_dir, name)
|
| 315 |
+
with open("%s.txt"%(prefix), "w") as fout:
|
| 316 |
+
for st_id, st in enumerate(story):
|
| 317 |
+
chaotic_list = np.random.choice(type_list,
|
| 318 |
+
np.random.choice(time_list, p=time_prob_list), replace=False, p=type_prob_list/np.sum(type_prob_list)).tolist()
|
| 319 |
+
print(chaotic_list)
|
| 320 |
+
for c in chaotic_list:
|
| 321 |
+
if c == "repeat":
|
| 322 |
+
if random.random() < 0.7:
|
| 323 |
+
st = repeat_sentence(st)
|
| 324 |
+
else:
|
| 325 |
+
st = repeat_ngram(st)
|
| 326 |
+
if c == "replace":
|
| 327 |
+
if random.random() < 0.5:
|
| 328 |
+
# replace one sentence
|
| 329 |
+
st = replace_sentence(st)
|
| 330 |
+
else:
|
| 331 |
+
# replace one word
|
| 332 |
+
st = replace_word(st)
|
| 333 |
+
if c == "shuffle":
|
| 334 |
+
n_sentence = int(np.random.choice(np.arange(1,len(st)-1)+1))
|
| 335 |
+
st = shuffle_sentence(st, n_sentence)
|
| 336 |
+
if c == "neg":
|
| 337 |
+
st = change_neg_sentence(st)
|
| 338 |
+
output(st, fout)
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
print("Anotomy:", anotomy_num)
|
| 343 |
+
print("All:", all_num)
|
| 344 |
+
|
get_vocab.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from nltk.stem import WordNetLemmatizer
|
| 2 |
+
lemma = WordNetLemmatizer().lemmatize
|
| 3 |
+
import nltk
|
| 4 |
+
pos_tag = nltk.pos_tag
|
| 5 |
+
from nltk.corpus import stopwords
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
mode = sys.argv[1]
|
| 9 |
+
file_dir = "./WritingPrompts/ini_data/" if "w" in mode else "./ROCStories/ini_data/"
|
| 10 |
+
file_name = "train.wp_target" if "w" in mode else "train.txt"
|
| 11 |
+
|
| 12 |
+
def get_avail_phrases():
|
| 13 |
+
sw = set(stopwords.words('english'))
|
| 14 |
+
avail_phrases = set()
|
| 15 |
+
fin = open("./conceptnet_entity.csv", 'r')
|
| 16 |
+
for i, line in enumerate(fin):
|
| 17 |
+
avail_phrases.add(' '.join(line.strip().split("|||")[:-1]))
|
| 18 |
+
avail_phrases = avail_phrases - sw
|
| 19 |
+
fin.close()
|
| 20 |
+
|
| 21 |
+
fin = open("./negation.txt", 'r')
|
| 22 |
+
for i, line in enumerate(fin):
|
| 23 |
+
avail_phrases.add(' '.join(line.strip().split()[1:]))
|
| 24 |
+
fin.close()
|
| 25 |
+
|
| 26 |
+
for w in [".", ",", "!", "?", "male", "female", "neutral"]:
|
| 27 |
+
avail_phrases.add(w)
|
| 28 |
+
|
| 29 |
+
return avail_phrases
|
| 30 |
+
|
| 31 |
+
avail_phrases = get_avail_phrases()
|
| 32 |
+
|
| 33 |
+
vocab = {}
|
| 34 |
+
with open("%s/%s"%(file_dir, file_name), "r") as fin1:
|
| 35 |
+
for kkk, line in enumerate(fin1):
|
| 36 |
+
if kkk % 1000 == 0:
|
| 37 |
+
print(kkk)
|
| 38 |
+
tmp = line.strip().split()
|
| 39 |
+
pos = pos_tag(tmp)
|
| 40 |
+
for word_pos in pos:
|
| 41 |
+
if lemma(word_pos[0], 'v' if word_pos[1][0] == 'V' else 'n') not in avail_phrases:
|
| 42 |
+
continue
|
| 43 |
+
if word_pos[0] in vocab:
|
| 44 |
+
vocab[word_pos[0]]["number"] += 1
|
| 45 |
+
if word_pos[1] in vocab[word_pos[0]]:
|
| 46 |
+
vocab[word_pos[0]][word_pos[1]] += 1
|
| 47 |
+
else:
|
| 48 |
+
vocab[word_pos[0]][word_pos[1]] = 1
|
| 49 |
+
else:
|
| 50 |
+
vocab[word_pos[0]] = {word_pos[1]:1, "number":1}
|
| 51 |
+
vocab_list = sorted(vocab, key=lambda x: vocab[x]["number"], reverse=True)
|
| 52 |
+
with open("%s/entity_vocab.txt"%file_dir, "w") as fout:
|
| 53 |
+
for v in vocab_list:
|
| 54 |
+
pos_list = sorted(vocab[v], key=vocab[v].get, reverse=True)
|
| 55 |
+
pos_list.remove("number")
|
| 56 |
+
fout.write("%s %d|||"%(v, vocab[v]["number"]) + "|||".join(["%s %d"%(p, vocab[v][p]) for p in pos_list]) + "\n")
|
negation.txt
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 no
|
| 2 |
+
0 not
|
| 3 |
+
0 n't
|
| 4 |
+
0 none
|
| 5 |
+
0 never
|
| 6 |
+
0 nobody
|
| 7 |
+
0 nothing
|
| 8 |
+
0 nowhere
|
| 9 |
+
0 neither
|
| 10 |
+
0 nobody
|
| 11 |
+
0 hardly
|
| 12 |
+
0 scarcely
|
| 13 |
+
0 barely
|
| 14 |
+
0 seldom
|
| 15 |
+
0 cannot
|
| 16 |
+
0 can 't
|
| 17 |
+
0 may not
|
| 18 |
+
0 would n't
|
| 19 |
+
0 would not
|
| 20 |
+
0 should n't
|
| 21 |
+
0 should not
|
| 22 |
+
0 do n't
|
| 23 |
+
0 do not
|
| 24 |
+
0 does 't
|
| 25 |
+
0 dose not
|
| 26 |
+
0 did n't
|
| 27 |
+
0 did not
|
| 28 |
+
0 is n't
|
| 29 |
+
0 is not
|
| 30 |
+
0 are n't
|
| 31 |
+
0 are not
|
| 32 |
+
0 was n't
|
| 33 |
+
0 was not
|