Create mised_read.py
Browse files- mised_read.py +106 -0
mised_read.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from urllib import request
|
3 |
+
|
4 |
+
class MISeDData:
|
5 |
+
def __init__(self, input):
|
6 |
+
# URL of the raw file. Kinda dangerous but whateva.
|
7 |
+
self.input = input
|
8 |
+
self.url = f"https://raw.githubusercontent.com/tykiww/MISeD/main/mised/{self.input}.jsonl"
|
9 |
+
self.local = f'mised/{input}.jsonl'
|
10 |
+
self.options = ['train', 'test', 'validation']
|
11 |
+
|
12 |
+
def parse_indexes(self, entry, meeting_length):
|
13 |
+
"""Finds the start and end points"""
|
14 |
+
# unpack values
|
15 |
+
vals = [value for d in entry for value in d.values()]
|
16 |
+
|
17 |
+
# if there are no values to unpack, retrieve the entire meeting.
|
18 |
+
if len(vals)==0:
|
19 |
+
return [0, meeting_length]
|
20 |
+
else:
|
21 |
+
return [min(vals), max(vals)]
|
22 |
+
|
23 |
+
def parse_speakers(self, entry):
|
24 |
+
"""Finds the names of the speakers in the meeting"""
|
25 |
+
speakers = [i['speakerName'] for i in entry]
|
26 |
+
return list(set(speakers))
|
27 |
+
|
28 |
+
def index_meeting(self, meeting):
|
29 |
+
"""Gets the correct meeting snippet"""
|
30 |
+
combine = [i['speakerName'] + ': ' + i['text'] for i in meeting]
|
31 |
+
return '\n'.join(combine)
|
32 |
+
|
33 |
+
def retrieve_data_local(self): # kinda defunct, but useful.
|
34 |
+
"""retrieve MISeD data. Only works if done locally"""
|
35 |
+
if self.input not in self.options:
|
36 |
+
print('ha, sucka. choose train, test, or validation')
|
37 |
+
return None
|
38 |
+
else:
|
39 |
+
with open(self.local, 'r') as file:
|
40 |
+
data = [json.loads(line) for line in file]
|
41 |
+
return data
|
42 |
+
|
43 |
+
def retrieve_data_url(self):
|
44 |
+
if self.input not in self.options:
|
45 |
+
print('ha, sucka. choose train, test, or validation')
|
46 |
+
return None
|
47 |
+
else:
|
48 |
+
with request.urlopen(self.url) as response:
|
49 |
+
file_content = response.read().decode('utf-8')
|
50 |
+
|
51 |
+
# Process the content as JSON Lines
|
52 |
+
data = [json.loads(line) for line in file_content.splitlines()]
|
53 |
+
return data
|
54 |
+
|
55 |
+
# Convert necessary data
|
56 |
+
def get(self,read_locally=True):
|
57 |
+
|
58 |
+
# Get the data
|
59 |
+
if read_locally:
|
60 |
+
data = self.retrieve_data_local()
|
61 |
+
else:
|
62 |
+
data = self.retrieve_data_url()
|
63 |
+
|
64 |
+
# empty list that we will eventually return.
|
65 |
+
collect = []
|
66 |
+
|
67 |
+
# Create a question-answer pair.
|
68 |
+
for i in range(len(data)):
|
69 |
+
# Get meeting and speakers
|
70 |
+
record = data[i]
|
71 |
+
mtg = record['meeting']['transcriptSegments']
|
72 |
+
mtg_len = len(mtg)
|
73 |
+
speakers = self.parse_speakers(mtg)
|
74 |
+
# prepare questions of interest.
|
75 |
+
questions = record['dialog']['dialogTurns']
|
76 |
+
|
77 |
+
# keep character length for summary
|
78 |
+
|
79 |
+
for n in range(len(questions)):
|
80 |
+
# Get speakers, question of interest, and response.
|
81 |
+
query = questions[n]['query']
|
82 |
+
response = questions[n]['response']
|
83 |
+
|
84 |
+
# Get the meeting portion of interest
|
85 |
+
if 'responseAttribution' in questions[n].keys():
|
86 |
+
indx = questions[n]['responseAttribution']['indexRanges']
|
87 |
+
indx = self.parse_indexes(indx, mtg_len)
|
88 |
+
else:
|
89 |
+
# default to entire meeting
|
90 |
+
indx = [0, mtg_len]
|
91 |
+
snippet = mtg[indx[0]:indx[1]]
|
92 |
+
snippet = self.index_meeting(snippet)
|
93 |
+
|
94 |
+
collect.append({'query': query,
|
95 |
+
'response': response,
|
96 |
+
'snippet': snippet,
|
97 |
+
'speakers': speakers,
|
98 |
+
})
|
99 |
+
|
100 |
+
return collect
|
101 |
+
|
102 |
+
|
103 |
+
if __name__ == "__main__":
|
104 |
+
# example to pull training dataset
|
105 |
+
retriever = MISeDData('train')
|
106 |
+
data = retriever.get()
|