-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/CogStack/CogStack-NiFi
- Loading branch information
Showing
5 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import sys | ||
from utils.cerner_blob import DecompressLzwCernerBlob | ||
|
||
# This needs to be investigated, records might have different charsets, | ||
# currently only tested with "iso-8859-1" | ||
# other frequently used encodings: "utf-16le", "utf-16be" | ||
# In some cases you will need to figure this out yourself, depending on | ||
# the data source | ||
INPUT_CHARSET = "iso-8859-1" | ||
|
||
# expected (optional) | ||
OUTPUT_CHARSET = "windows-1252" | ||
|
||
# possible values: | ||
# - binary: output binary code | ||
# - string: output string after decompression | ||
OUTPUT_MODE = "binary" | ||
|
||
input_cerner_blob = str(sys.stdin.buffer.read(), INPUT_CHARSET).encode(INPUT_CHARSET) | ||
|
||
for arg in sys.argv: | ||
_arg = arg.split("=", 1) | ||
|
||
if _arg[0] == "input_charset": | ||
INPUT_CHARSET = str(_arg[1]).lower() | ||
elif _arg[0] == "output_charset": | ||
OUTPUT_CHARSET = str(_arg[1]).lower() | ||
elif _arg[0] == "output_mode": | ||
OUTPUT_MODE = str(_arg[1]).lower() | ||
|
||
decompress_blob = DecompressLzwCernerBlob() | ||
decompress_blob.decompress(input_cerner_blob) | ||
|
||
if OUTPUT_MODE == "binary": | ||
sys.stdout.buffer.write(bytes(decompress_blob.output_stream)) | ||
else: | ||
sys.stdout.write(decompress_blob.output_stream.decode(OUTPUT_CHARSET)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from typing import List | ||
|
||
|
||
class LzwItem: | ||
def __init__(self, _prefix: int = 0, _suffix: int = 0) -> None: | ||
self.prefix = _prefix | ||
self.suffix = _suffix | ||
|
||
|
||
class DecompressLzwCernerBlob: | ||
def __init__(self) -> None: | ||
self.MAX_CODES: int = 8192 | ||
self.tmp_decompression_buffer: List[int] = [0] * self.MAX_CODES | ||
self.lzw_lookup_table: List[LzwItem] = [LzwItem()] * self.MAX_CODES | ||
self.tmp_buffer_index: int = 0 | ||
self.current_byte_buffer_index: int = 0 | ||
|
||
# starts after 256, since 256 is the ASCII alphabet | ||
self.code_count: int = 257 | ||
self.output_stream = bytearray() | ||
|
||
def save_to_lookup_table(self, compressed_code: int): | ||
self.tmp_buffer_index = -1 | ||
while compressed_code >= 258: | ||
self.tmp_buffer_index += 1 | ||
self.tmp_decompression_buffer[self.tmp_buffer_index] = \ | ||
self.lzw_lookup_table[compressed_code].suffix | ||
compressed_code = self.lzw_lookup_table[compressed_code].prefix | ||
|
||
self.tmp_buffer_index += 1 | ||
self.tmp_decompression_buffer[self.tmp_buffer_index] = compressed_code | ||
|
||
for i in reversed(list(range(self.tmp_buffer_index + 1))): | ||
self.output_stream.append(self.tmp_decompression_buffer[i]) | ||
|
||
def decompress(self, input_stream=bytearray()): | ||
|
||
byte_buffer_index: int = 0 | ||
|
||
# used for bit shifts | ||
shift: int = 1 | ||
current_shift: int = 1 | ||
|
||
previous_code: int = 0 | ||
middle_code: int = 0 | ||
lookup_index: int = 0 | ||
|
||
skip_flag: bool = False | ||
|
||
first_code = input_stream[byte_buffer_index] | ||
|
||
while True: | ||
if current_shift >= 9: | ||
|
||
current_shift -= 8 | ||
|
||
if first_code != 0: | ||
byte_buffer_index += 1 | ||
middle_code = input_stream[byte_buffer_index] | ||
|
||
first_code = (first_code << current_shift + | ||
8) | (middle_code << current_shift) | ||
|
||
byte_buffer_index += 1 | ||
middle_code = input_stream[byte_buffer_index] | ||
|
||
tmp_code = middle_code >> (8 - current_shift) | ||
lookup_index = first_code | tmp_code | ||
|
||
skip_flag = True | ||
else: | ||
byte_buffer_index += 1 | ||
first_code = input_stream[byte_buffer_index] | ||
byte_buffer_index += 1 | ||
middle_code = input_stream[byte_buffer_index] | ||
else: | ||
byte_buffer_index += 1 | ||
middle_code = input_stream[byte_buffer_index] | ||
|
||
if not skip_flag: | ||
lookup_index = (first_code << current_shift) | ( | ||
middle_code >> 8 - current_shift) | ||
|
||
if lookup_index == 256: | ||
shift = 1 | ||
current_shift += 1 | ||
first_code = input_stream[byte_buffer_index] | ||
|
||
self.tmp_decompression_buffer = [0] * self.MAX_CODES | ||
self.tmp_buffer_index = 0 | ||
|
||
self.lzw_lookup_table = [LzwItem()] * self.MAX_CODES | ||
self.code_count = 257 | ||
continue | ||
|
||
elif lookup_index == 257: # EOF marker | ||
return self.output_stream | ||
|
||
skip_flag = False | ||
|
||
# skipit part | ||
if previous_code == 0: | ||
self.tmp_decompression_buffer[0] = lookup_index | ||
if lookup_index < self.code_count: | ||
self.save_to_lookup_table(lookup_index) | ||
if self.code_count < self.MAX_CODES: | ||
self.lzw_lookup_table[self.code_count] = LzwItem( | ||
previous_code, | ||
self.tmp_decompression_buffer[self.tmp_buffer_index]) | ||
self.code_count += 1 | ||
else: | ||
self.lzw_lookup_table[self.code_count] = LzwItem( | ||
previous_code, | ||
self.tmp_decompression_buffer[self.tmp_buffer_index]) | ||
self.code_count += 1 | ||
self.save_to_lookup_table(lookup_index) | ||
# end of skipit | ||
|
||
first_code = (middle_code & (0xff >> current_shift)) | ||
current_shift += shift | ||
|
||
if self.code_count in [511, 1023, 2047, 4095]: | ||
shift += 1 | ||
current_shift += 1 | ||
previous_code = lookup_index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule working_with_cogstack
updated
4 files
+1 −1 | .github/workflows/main.yml | |
+14 −35 | medcat/evaluate_mct_export/mct_analysis.py | |
+33 −1,802 | medcat/evaluate_mct_export/mct_export_summary.ipynb | |
+4 −4 | requirements.txt |