-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
100 lines (97 loc) · 3.71 KB
/
parser.py
File metadata and controls
100 lines (97 loc) · 3.71 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
# parser.py
# ----------
# HMM_TMRCA Project
# Licensing Information: Please do not distribute.
# You are free to use and extend these code for educational purposes.
# ProblemSet written by professor Yun S. Song
# Solution and code written by Jae Young Ryoo (jay.ryoo@gmail.com) and Saba Khalilnaji
import sys, math, util, hmm
"""
pparser takes in a textFile and parses an initial_parameters.txt file
according to the example set in README
"""
class pparser:
def __init__(self):
self.p = {}
self.e = {}
self.a = {}
#def parse_States(self, textFile):
def parse_Parameters(self, textFile):
pFile = open(textFile)
i = 0
l = pFile.next()
try:
while True:
if i == 0:
if l[0] == '#':
l = pFile.next()
continue
elif l == '\n':
l = pFile.next()
l = pFile.next()
i = i+1
continue
else:
while l != '\n':
l = l.lstrip(' ')
l = l.rstrip(' ')
trip = l.partition(" ")
k = int(trip[0])
trip = trip[2].lstrip()
trip = trip.rstrip()
prob = float(trip)
self.p[k] = prob
l = pFile.next()
elif i == 1:
if l[0] == '#':
l = pFile.next()
continue
elif l == '\n':
l = pFile.next()
l = pFile.next()
i = i + 1
continue
else:
t = 1
while l != '\n':
l = l.lstrip()
l = l.rstrip()
trip = l.partition(" ")
one = float(trip[0])
trip = trip[2].lstrip()
trip = trip.partition(" ")
two = float(trip[0])
trip = trip[2].lstrip()
trip = trip.partition(" ")
three = float(trip[0])
trip = trip[2].lstrip()
trip = trip.partition(" ")
four = float(trip[0])
self.a[(t,1)] = one
self.a[(t,2)] = two
self.a[(t,3)] = three
self.a[(t,4)] = four
t = t + 1
l = pFile.next()
elif i == 2:
if l[0] == '#':
l = pFile.next()
continue
else:
while l != '\n':
l = l.lstrip()
l = l.rstrip()
trip = l.partition(" ")
kk = int(trip[0])
trip = trip[2].lstrip()
trip = trip.partition(" ")
II = float(trip[0])
trip = trip[2].lstrip()
DD = float(trip)
self.e[(kk, 'D')] = DD
self.e[(kk, 'I')] = II
l = pFile.next()
except:
pass
pFile.close()
return(self.p, self.a, self.e)