-
Notifications
You must be signed in to change notification settings - Fork 628
/
Copy pathutils.py
32 lines (28 loc) · 1.34 KB
/
utils.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
import pandas as pd
import numpy as np
def get_data(filename):
df = pd.read_csv(filename,delim_whitespace=True,names=['word','label'])
beg_indices = list(df[df['word'] == 'BOS'].index)+[df.shape[0]]
sents,labels,intents = [],[],[]
for i in range(len(beg_indices[:-1])):
sents.append(df[beg_indices[i]+1:beg_indices[i+1]-1]['word'].values)
labels.append(df[beg_indices[i]+1:beg_indices[i+1]-1]['label'].values)
intents.append(df.loc[beg_indices[i+1]-1]['label'])
return np.array(sents, dtype=object), np.array(labels, dtype=object), np.array(intents, dtype=object)
def get_data2(filename):
with open(filename) as f:
contents = f.read()
sents,labels,intents = [],[],[]
for line in contents.strip().split('\n'):
words,labs = [i.split(' ') for i in line.split('\t')]
sents.append(words[1:-1])
labels.append(labs[1:-1])
intents.append(labs[-1])
return np.array(sents, dtype=object), np.array(labels, dtype=object), np.array(intents, dtype=object)
read_method = {'Data/data2/atis-2.dev.w-intent.iob':get_data,
'Data/data2/atis.train.w-intent.iob':get_data2,
'Data/data2/atis.test.w-intent.iob':get_data,
'Data/data2/atis-2.train.w-intent.iob':get_data2}
def fetch_data(fname):
func = read_method[fname]
return func(fname)