-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
81 lines (64 loc) · 2.08 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
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
from string import punctuation, digits
import csv
import numpy as np
import matplotlib.pyplot as plt
import sys
if sys.version_info[0] < 3:
PYTHON3 = False
else:
PYTHON3 = True
def load_data(path_data):
"""Return a dictionary for the state descriptions displayed to player"""
global PYTHON3
data = []
if PYTHON3:
f_data = open(path_data, encoding="latin1")
else:
f_data = open(path_data)
reader = csv.reader(f_data, delimiter='\t')
for row in reader:
data.append(row)
f_data.close()
return data
def ewma(a, alpha=0.9):
"""Computes the exponentially weighted moving average of a"""
b = np.array(a)
n = b.size
w0 = np.ones(n) * alpha
p = np.arange(n - 1, -1, -1)
return np.average(b, weights=w0 ** p)
def extract_words(input_string):
"""
Helper function for bag_of_words()
Inputs a text string
Returns a list of lowercase words in the string.
Punctuation and digits are separated out into their own words.
"""
for c in punctuation + digits:
input_string = input_string.replace(c, ' ' + c + ' ')
return input_string.lower().split()
def bag_of_words(texts):
"""
Inputs a list of string descriptions
Returns a dictionary of unique unigrams occurring over the input
"""
dictionary = {} # maps word to unique index
for text in texts:
word_list = extract_words(text[0])
for word in word_list:
if word not in dictionary:
dictionary[word] = len(dictionary)
return dictionary
def extract_bow_feature_vector(state_desc, dictionary):
"""
Inputs a string state description
Inputs the dictionary of words as given by bag_of_words
Returns the bag-of-words vector representation of the state
The returned vector is of dimension m, where m the total number of entries in the dictionary.
"""
state_vector = np.zeros([len(dictionary)])
word_list = extract_words(state_desc)
for word in word_list:
if word in dictionary:
state_vector[dictionary[word]] += 1
return state_vector