H10: Object oriented programming
Code van Hippocrates
import random
class Hippocrates:
"""
>>> codec = Hippocrates('ICD.txt')
>>> codec.beschrijving('A00')
'Cholera'
>>> codec.beschrijving('A00.0')
'Cholera due to Vibrio cholerae 01, biovar cholerae'
>>> codec.beschrijving('A00.2')
Traceback (most recent call last):
ValueError: ongeldige ICD-code
>>> codec.karakter('A00.0')
'C'
>>> codec.karakter('A00.2')
'o'
>>> codec.karakter('A00.0.33')
','
>>> codec.karakter('A00.2.33')
Traceback (most recent call last):
ValueError: ongeldige Hippocrates-code
>>> codec.codes('V')
{'O22.0', 'A00.1.15', 'I83.218.0', 'A00.0.15'}
>>> codec.codes('v')
{'I67.83.12', 'V59.9.50', 'V64.0.76', 'H59.121.12', 'M50.022.3', 'S44.8X9.19', 'T37.92.46',
'S44.8X9.51', 'V42.1.68', 'S66.911.68', 'T37.3.16', 'T37.1X.16', 'V04.92.67', 'M67.971.28',
'B08.04.4', 'Q95.3.53', 'V49.60.69', 'V64.0.3', 'T43.2.16', 'V64.0.26', 'E10.32.49', 'B57.30.40',
'A00.0.38', 'V64.0.13', 'I89.9.10', 'I83.218.9', 'V04.92.54', 'F44.6.3', 'I89.9.35', 'W24.0.23',
'M67.232.4', 'B57.30.43', 'S90.464.16', 'N13.9', 'V52.1.88', 'S14.9.25', 'V52.1.30', 'S78.922.54',
'V92.16.45', 'V14.1.51', 'S00.261.16', 'B57.30.28', 'V59.9.13', 'W93.19', 'W92.XXXD.19',
'V64.0.63', 'M50.022.34', 'J10.89.44', 'A00.1.38', 'A75.0.30', 'V14.1.64', 'T37.1X5.2'}
>>> codec.codes('.')
{'Z68.35.29', 'Z68.35.24', 'Z68.42.29', 'Z68.36.29', 'Z68.42.24', 'Z68.36.24', 'Z68.43.27'}
>>> codec.codes('xxx')
set()
>>> codec.codes('!')
set()
>>> codec.codeer('Eat wise, drop a size')
'A75.0.0 K50.119.59 T43.2.75 V14.1.21 V42.1.54 E10.32.53 T21.70.5 T21.06.12 S59.239.62
M85.812.56 M85.812.34 Q95.3.27 V64.0.22 T37.3.61 S00.261.34 T49.4X6.76 F44.6.32 A02.24.23
V59.9.62 J10.89.41 O91.1.13'
>>> codec.codeer('Eat wise, drop a size')
'W92.XXXD.0 I97.611.84 I97.611.38 S59.099.31 M90.832.38 V42.1.93 M61.25.29 I67.83.43
T37.92.67 W93.38 O22.23 V59.9.48 F04.23 C34.2.13 F44.6.43 A00.0.28 M85.812.5 A02.24.23
Z96.621.21 J10.89.41 N07.9.31'
>>> codec.codeer('Eat wise, drop a size!')
Traceback (most recent call last):
ValueError: ongeldige platte tekst
>>> codec.decodeer('A75.0.0 K50.119.59 T43.2.75 V14.1.21 V42.1.54 E10.32.53 T21.70.5
T21.06.12 S59.239.62 M85.812.56 M85.812.34 Q95.3.27 V64.0.22 T37.3.61 S00.261.34
T49.4X6.76 F44.6.32 A02.24.23 V59.9.62 J10.89.41 O91.1.13')
'Eat wise, drop a size'
>>> codec.decodeer('W92.XXXD.0 I97.611.84 I97.611.38 S59.099.31 M90.832.38 V42.1.93
M61.25.29 I67.83.43 T37.92.67 W93.38 O22.23 V59.9.48 F04.23 C34.2.13 F44.6.43 A00.0.28
M85.812.5 A02.24.23 Z96.621.21 J10.89.41 N07.9.31')
'Eat wise, drop a size'
, >>> codec.decodeer('X66.6 Q99.99 Z12.34')
Traceback (most recent call last):
ValueError: ongeldige gecodeerde tekst
"""
def __init__(self, locatie):
self.c2k = {}
self.k2c = {}
self.c2l = {}
for icd in open(locatie, 'r', encoding='utf-8'):
icd = icd.rstrip('\n')
code = icd.split(' ', 1)[0]
self.c2k[code] = icd.split(' ', 1)[1]
omschrijving = icd.split(' ', 1)[1]
i=0
for letter in omschrijving:
self.c2l[code + "." + str(i)] = letter
set_van_codes = []
if letter in self.k2c:
set_van_codes = list(self.k2c[letter])
set_van_codes.append(code + '.' + str(i))
self.k2c[letter] = set(set_van_codes)
else:
set_van_codes.append(code + '.' + str(i))
self.k2c[letter] = set(set_van_codes)
i += 1
def beschrijving(self, code):
if code not in self.c2k:
raise ValueError('ongeldige ICD-code')
return self.c2k.get(code)
def karakter(self, hippocode):
if hippocode not in self.c2l:
raise ValueError('ongeldige Hippocrates-code')
return self.c2l.get(hippocode)
def codes(self, karakter):
if karakter not in self.k2c:
return set()
return self.k2c.get(karakter)
def codeer(self, tekst):
code = ''
for letter in tekst:
if letter not in self.k2c:
raise ValueError('ongeldige platte tekst')
lcd_set = self.codes(letter)
lcd_set = list(lcd_set)
i = random.randint(0, len(lcd_set) - 1)
code += lcd_set[i] + " "
return code.rstrip(" ")
def decodeer(self, code_tekst):
zin = ''
lijst_codes = code_tekst.split(" ")
for code in lijst_codes:
if code not in self.c2l:
raise ValueError('ongeldige gecodeerde tekst')
zin += self.karakter(code)
return zin