-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPT_Definition_Generation.py
152 lines (118 loc) · 4.98 KB
/
GPT_Definition_Generation.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
) # for exponential backoff
import openai
import time
from nltk.corpus import wordnet as wn
import nltk
from tqdm import tqdm
openai.api_key = "" # Add your API Key here
@retry(wait=wait_random_exponential(min=30, max=180), stop=stop_after_attempt(3))
def get_target_word_pos(text, word):
word_index = text.index(word)
prev_text = text[:word_index]; next_text = text[word_index+len(word):]
prev_tokens = nltk.tokenize.word_tokenize(prev_text); next_tokens = nltk.tokenize.word_tokenize(next_text)
tokens = prev_tokens + [word] + next_tokens
token_index = len(prev_tokens)
tag = nltk.pos_tag(tokens)
nltk_pos = tag[token_index][1]
if nltk_pos.startswith('NN'): pos = 'n';
elif nltk_pos.startswith('V'): pos = 'v';
elif nltk_pos.startswith('JJ'): pos = 'adj';
elif nltk_pos.startswith('RB'): pos = 'adv';
else: pos = 'n'
return tag[token_index], pos
sense_definitions = []
def completions_with_backoff(**kwargs):
return openai.Completion.create(**kwargs)
def data_loader(data_file_path, gold_file_path = None):
text_data = {}
fin_data = open(data_file_path)
for data_index, line in enumerate(fin_data):
line = line.strip()
if not line: continue
cols = line.split('\t')
target_word = cols[0]; context = cols[1]
candidates = cols[2:]
sense_definitions = []
target_senses = wn.synsets(target_word)
for synset in target_senses:
if synset.pos() == 'n':
sense_definition = synset.definition().split(';')[0]
sense_definitions.append(sense_definition)
text_data[data_index] = {'target_word': target_word,
'sense_definitions': sense_definitions,
'context': context,
'candidates': candidates}
fin_data.close()
if gold_file_path:
fin_gold = open(gold_file_path)
for gold_index, line in enumerate(fin_gold):
line = line.strip()
if not line.strip(): continue
gold = line.strip()
text_data[gold_index]['gold'] = gold
return text_data
wait_time = 0.1
batch_size = 20
data_file_path = "datapath/train/train_v1/train.data.v1.txt"
gold_file_path = "datapath/train/train_v1/train.gold.v1.txt"
text_data = data_loader(data_file_path, gold_file_path)
total_len = len(text_data)
print(total_len)
start_pos = 0
i = 0
output_filename = "GPT_Definitions.txt"
if os.path.isfile(output_filename):
file = open(output_filename, 'r')
Lines = file.readlines()
contextList = []
for l in Lines:
try:
l = l.strip("\n")
if (len(l) > 0) and (len(l.split("\t")) == 3):
contextWord = l.split("\t")[1]
contextList.append(contextWord)
except Exception as e:
print(e)
print(l)
print(l.split("\t"))
print("prev l", l)
prev_l = l
else:
contextList = []
print(len(contextList))
for idx, data in tqdm(text_data.items()):
if data['context'] in contextList:
contextList.remove(data['context'])
else:
_, pos = get_target_word_pos(data['context'], data['target_word'])
#print(data['target_word'], data['context'], pos)
#prompt = "Define \"{}\" in {}. {} ({}.):".format(data['target_word'], data['context'], data['target_word'], pos)
#prompt = "Define \"{}\" in the context of \"{}\". {} ({}.):".format(data['target_word'], data['context'], data['target_word'], pos)
prompt = "Define {} ({}.)".format(data['target_word'], pos)
#print(prompt)
try:
response = completions_with_backoff(model="text-davinci-002", prompt=prompt, temperature=1.0, max_tokens=64)
# response = openai.Completion.create(model="text-davinci-003", prompt=prompt, temperature=0.7, max_tokens=64)
definition = response["choices"][0]['text']
strt_idx = definition.find("\n\n")
definition = definition[strt_idx+2:]
format_def = definition.replace("\n", " ")
#print(format_def)
# print("{}\t{}\t{}\n".format(data['target_word'], data['context'], format_def))
with open(output_filename, "a") as out_file:
out_file.write("{}\t{}\t{}\n".format(data['target_word'], data['context'], format_def))
except Exception as e:
print(openai.Completion.create(model="text-davinci-003", prompt=prompt, temperature=0.7, max_tokens=64))
raise("Exception", e)
#print("Sleeping for {} secs".format(wait_time))
time.sleep(wait_time)
i += 1
# break
# response = openai.Completion.create(model="text-davinci-003", prompt="Define pollosimo gallinacean", temperature=0.7, max_tokens=128)
# print(response)
print("Done!")