-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_dictionary.py
More file actions
207 lines (175 loc) · 5.57 KB
/
file_dictionary.py
File metadata and controls
207 lines (175 loc) · 5.57 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
"""
Created on Tue May 31 02:10:18 2016
@author: jessica
This module deals with files and dictionary features
"""
from os import listdir
from os.path import isfile, join
from collections import defaultdict, Counter
def list_to_file(L, filename, append=False):
"""
Writes a list of lists into the given file. It assumes each inner list has at
least one item
Example:
input list: [[hey1, hey2, hey3], [hey4]]
output file:
hey1, hey2, hey3
hey4
Parameter
-----------
L (list[list[str]]): Lists of lists to write
filename(str): location to store L
Returns
--------
Nothing
"""
action = 'w'
if append:
action = 'a'
with open(filename, action) as myfile:
for item in L:
for i in xrange(len(item)-1):
myfile.write(item[i]+',')
myfile.write(item[-1]+'\n')
def file_to_list(filename):
"""
Does the opposite of list_to_file. Takes in a filename and returns the nested list
"""
ans = []
with open(filename, 'r') as my_file:
for line in my_file:
ans.append([])
temp = line.split(",")
for item in temp:
ans[-1].append(item.strip())
if ans[-1] == []:
ans.pop()
return ans
def dict_to_file(d, filename):
"""
Converts a feature dictionary to a text file and stores it in filename
Parameters
----------
d(dict{str:float}): dictionary to store
filename(str): location to store dictionary
Return
---------
Nothing
"""
with open(filename, 'w') as myfile:
for key in d:
try:
myfile.write("{}:{}\n".format(key, d[key]))
except:
print("ERROR: Unable to input {}:{}".format(key, d[key]))
def file_to_dict(filename):
"""
Converts a file into a dictionary (opposite of dict to file)
Parameters:
---------------
filename(str): name of file containing dictionary
Returns:
---------
dict{str:float}
"""
d = defaultdict(float)
with open(filename, 'r') as myFile:
for line in myFile:
temp = line.split(":")
if(len(temp) == 2):
d[temp[0]] = float(temp[1].strip())
return d
def folder_to_dict(folder_path):
"""
Helper function that runs through an entire folder and creates a dictionary
out of each internal file's contents.
Parameter
---------------
folder_path (str):
Path to folder containing text files of dictionaries with format key:value\n
Returns
----------------
dict{str:defaultdict{str, float or int}}
Dictionary that maps a filename to the dictionry of that filename in the folder_path
"""
ans = {}
filenames = [f for f in listdir(folder_path) if isfile(join(folder_path, f))]
for filename in filenames:
ans[filename[:-4]] = file_to_dict(join(folder_path,filename))
return ans
def zip_entries(dict_list):
"""
Creates a new dictionary that has all the keys in each dict_list item added
together
Parameter
-----------
dict_list(list[dict{str:float}]):
List of dictionaries to zip together
Return
------------
defaultdict(str:float): Final zipped up dictionary
"""
ans = defaultdict(float)
for d in dict_list:
for key in d:
ans[key]+=d[key]
return ans
def combine_entries(d, keys, new_key):
"""
combines the given two keys in the dictionary and deletes the original entries
Parameters
----------------
d (dict{str:defaultdict{str:float or int}}): (can also be a Counter)
dictionary we wish to combine the keys of i
keys(list[str]):
the keys we wish to combine
new_key(str):
the name of the new key that the combined keys will be in
Returns
----------------
Nothing
"""
#perform checks
for key in keys:
if key not in d:
raise ValueError("all keys (2nd parameter) must be in the dictionary")
if(new_key not in d):
d[new_key] = defaultdict(float)
for key in keys:
if(key != new_key):
for item in d[key]:
d[new_key][item]+=d[key][item]
del d[key]
def store_nested_dict(d, filename):
"""
Stores a nested dictionary into a file
"""
with open(filename, 'w') as myFile:
for key in d:
myFile.write(key + "|||")
for key2 in d[key]:
myFile.write(key2 + ":" + str(d[key][key2]) + ",")
myFile.write(";;;")
def load_nested_dict(filename):
"""
loads a nested dictionary from a file
"""
with open(filename, 'r') as my_file:
temp = my_file.readline()
#obtain the _tag_features from first line
d1 = {}
collection = temp.split(";;;") #separates different events in collection
for event in collection:
e_split = event.split("|||") #split event from their tags
if(len(e_split) == 2):
#initialize new event entry
key = e_split[0].strip()
d1[key] = defaultdict(float)
#split each tag and add them to the event dict
t_split = e_split[1].strip().split(",")
for tag in t_split:
d_split = tag.split(':')
if(len(d_split) == 2):
d1[key][d_split[0]] = float(d_split[1])
return d1