-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (43 loc) · 1.24 KB
/
Copy pathutils.py
File metadata and controls
51 lines (43 loc) · 1.24 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
def read_words(filename):
words = []
for line in open(filename):
words.append(line.strip())
return words
def read_vectors(filename):
vectors = []
for line in open(filename):
vectors.append(map(float, line.strip().split()))
return vectors
def write_tree(words, hierarchy, filename):
f = open(filename, "w")
print >> f, len(words)
for word in words:
print >> f, word
print >> f, len(hierarchy)
for children in hierarchy:
print >> f, len(children), " ".join(map(str, children))
def convert_fastcluster(cluster_data):
hierarchy = []
for n1, n2, _, _ in cluster_data:
hierarchy.append((int(n1), int(n2)))
return hierarchy
def depth(words, hierarchy):
depths = [1] * len(words)
for children in hierarchy:
new_depth = 1
for child in children:
new_depth = max(new_depth, depths[child] + 1)
depths.append(new_depth)
return depths[-1]
def read_tree(filename):
f = open(filename, "r")
words = []
num_words = int(f.readline().strip())
for i in range(num_words):
words.append(f.readline().strip())
tree = []
num_merges = int(f.readline().strip())
for i in range(num_merges):
children = map(int, f.readline().strip().split())
tree.append(tuple(children[1:]))
return words, tree