-
Notifications
You must be signed in to change notification settings - Fork 2
/
Encoder.py
43 lines (28 loc) · 1.14 KB
/
Encoder.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
import numpy as np
Document = open("Input.txt", "r").read()
Dictionary, EncoderOutput, = {}, np.array([])
def InitializeDict(file):
for char in file:
if char not in Dictionary.keys():
Dictionary[char] = len(Dictionary)
np.save("InitializedDictionaryKeys.npy", np.array(
list(Dictionary.keys())), allow_pickle=True)
np.save("InitializedDictionaryValues.npy", np.array(
list(Dictionary.values())), allow_pickle=True)
def RunLZW(file):
global Dictionary
global EncoderOutput
index = 0
while index < len(file):
TempIndex, TrackingString = index+1, file[index]
while TempIndex < len(file) and (TrackingString + file[TempIndex]) in Dictionary.keys():
TrackingString += file[TempIndex]
TempIndex += 1
EncoderOutput = np.append(EncoderOutput, Dictionary[TrackingString])
if TempIndex < len(file):
Dictionary[TrackingString+file[TempIndex]] = len(Dictionary)
index = TempIndex
np.save("EncoderOutput.npy", EncoderOutput, allow_pickle=True)
if __name__ == "__main__":
InitializeDict(Document)
RunLZW(Document)