-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path5_readData.py
129 lines (104 loc) · 3.79 KB
/
5_readData.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
from __future__ import unicode_literals, print_function, division
from io import open
import unicodedata
import string
import re
import random
import torch
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
SOS_token = 0
EOS_token = 1
class Lang:
def __init__(self, name):
self.name = name
self.word2index = {}
self.word2count = {}
self.index2word = {0: "SOS", 1: "EOS"}
self.n_words = 2 # Count SOS and EOS
def addSentence(self, sentence):
for word in sentence.split(' '):
self.addWord(word)
def addWord(self, word):
if word not in self.word2index:
self.word2index[word] = self.n_words
self.word2count[word] = 1
self.index2word[self.n_words] = word
self.n_words += 1
else:
self.word2count[word] += 1
# Turn a Unicode string to plain ASCII, thanks to
# https://stackoverflow.com/a/518232/2809427
def unicodeToAscii(s):
return ''.join(
c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'
)
# Lowercase, trim, and remove non-letter characters
def normalizeString(s):
s = unicodeToAscii(s.lower().strip())
s = re.sub(r"([.!?])", r" \1", s)
s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
return s
def readLines(filename):
lines = open(filename, encoding='utf-8').read().strip().split('\n')
return [unicodeToAscii(line) for line in lines]
def normalizeWindowsString(s):
s = unicodeToAscii(s.lower().strip())
s = re.sub(r"([.!?])", r" \1", s)
s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
return s
def randomTrainingExample(pairs, labels):
rand_key = random.choice(list(pairs))
return pairs[rand_key], labels[rand_key]
MAX_RAW_LENGTH = 30
MAX_RNN_LENGTH = 10
all_categories = []
def stripWords2MaxLen(sent):
allwords = sent.split(' ')
if(len(allwords) < MAX_RNN_LENGTH):
return sent
sent = ' '.join(allwords[i] for i in range(MAX_RNN_LENGTH-1))
#print(sent)
return sent
def filterSentencePair(sent0, sent1):
return len(sent0.split(' ')) < MAX_RAW_LENGTH and \
len(sent1.split(' ')) < MAX_RAW_LENGTH
def readClassificationData(lang1):
print("Reading Lines...")
# Read the file and split into lines
#with open('data/dev_w_id.txt', 'rb') as f:
# lines = f.readlines()#.strip().split('\n')
#print(lines[0])
#lines = open('data/dev_w_id.txt').read().strip().split('\n')
lines = open('data/train_w_id.txt', encoding='windows-1252').readlines()
print(len(lines))
input_lang = Lang(lang1)
pairs = {}
labels = {}
# Split every line into id, query, passage, label
for l in lines:
[id, sent0, sent1, label] = l.strip().replace('\n','').split('\t')
if(filterSentencePair(sent0, sent1)):
sent0 = stripWords2MaxLen(normalizeString(sent0))
sent1 = stripWords2MaxLen(normalizeString(sent1))
#print([id, normalizeWindowsString(sent0), normalizeWindowsString(sent1), label])
pairs[id] = ([(sent0), (sent1)])
labels[id] = (label)
if label not in all_categories:
all_categories.append((label))
input_lang.addSentence((sent0))
input_lang.addSentence((sent1))
return input_lang, pairs, labels
def prepareClassificationData(lang1):
input_lang, pairs, labels = readClassificationData(lang1)
if( len(pairs) != len(labels)):
print("Number of sentence pairs is not equal to label count")
# exit(0)
print("Read %s sentence pairs and labels" % len(pairs))
print("Counted words:")
print(input_lang.name, input_lang.n_words)
return input_lang, pairs, labels