-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateChainTable.py
More file actions
45 lines (31 loc) · 1.08 KB
/
generateChainTable.py
File metadata and controls
45 lines (31 loc) · 1.08 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
# -*- coding: utf-8 -*-
import os
import numpy as np
import re
import constants as const
pattern = re.compile("[,]")
dirname = os.path.dirname(__file__)
sourceFile = os.path.join(dirname, const.sourceFile)
outFile = os.path.join(dirname, const.chainTable)
#Create ouputs directory if doesn't exist
import pathlib
pathlib.Path(outFile).parent.mkdir(parents=True, exist_ok=True)
count = np.zeros((const.arraySize, const.arraySize, const.arraySize), dtype='int32')
with open(sourceFile, "r") as lines:
for line in lines:
# Split the comma
word, wordOccStr = pattern.split(line)
#transform String to Number
wordOccurences = int(wordOccStr)
#Add a endline symbol to tell the probability that a charater ends a word
word = word + "\n"
i = 0
j = 0
#transform the "word" in an unicode array
ordList = [ord(c) for c in list(word)]
for k in ordList:
count[i, j, k] += wordOccurences
i = j
j = k
count.tofile(outFile)
print ("Finished!")