def __init__(self, sep='', alphabet=None): self.encoded_dict = {} self.sep = sep if alphabet: self.alphabet = alphabet else: self.alphabet = MorseAlphabet()
class WordEncoder(): def __init__(self, sep='', alphabet=None): self.encoded_dict = {} self.sep = sep if alphabet: self.alphabet = alphabet else: self.alphabet = MorseAlphabet() def get_alphabet(self): return self.alphabet def get_list(self): return list(self.encoded_dict.keys()) def get_dict(self): return self.encoded_dict def add_word_list(self, words): [self.add_word(w) for w in words] def add_word(self, word): alphabet_map = self.alphabet.get_encoded_letters_map() letters = [] for letter in word: letters.append(alphabet_map[letter]) encoded_word = self.sep.join(letters) if encoded_word in self.encoded_dict: self.encoded_dict[encoded_word] += 1 else: self.encoded_dict[encoded_word] = 1
def context2(): context = Context() context.add_sentence('--') # TT M context.add_alphabet(MorseAlphabet()) encoder = WordEncoder(context) context.add_word_encoder(encoder) encoder.add_word('T') return context
def context1(): context = Context() context.add_sentence('......-...-..---') # HELLO context.add_alphabet(MorseAlphabet()) encoder = WordEncoder(context) context.add_word_encoder(encoder) encoder.add_word('HELLO') encoder.add_word('HELL') encoder.add_word('TEST') return context
def context4(): context = Context() context.add_sentence('......-...-..---') # HELLO context.add_alphabet(MorseAlphabet()) encoder = WordEncoder(context) context.add_word_encoder(encoder) encoder.add_word('HELLO') encoder.add_word('EEEEELLO') # same encoding context.add_word_finder(WordFinder(context)) return context
def context2(): context = Context() context.add_sentence('......-...-..---') # HELLO context.add_alphabet(MorseAlphabet()) encoder = WordEncoder(context) context.add_word_encoder(encoder) encoder.add_word('HELL') encoder.add_word('O') # invalid in pos 0 context.add_word_finder(WordFinder(context)) return context
def context(): context = Context() context.add_sentence('......-...-..---') # HELLO context.add_alphabet(MorseAlphabet()) return context
def test_encoded_letters_first_line(): morse = MorseAlphabet() assert '-..' in morse.get_encoded_letters() assert morse.get_encoded_letters_map()['D'] == '-..'
def test_encode(): morse = MorseAlphabet() assert morse.encode('D') == '-..'
def test_encoded_letters_ident_second_line(): morse = MorseAlphabet() assert morse.encode('J') == '.---' assert '.---' in morse.get_encoded_letters() assert morse.get_encoded_letters_map()['J'] == '.---'
def test_add_one_word_with_alphabet(): morse = MorseAlphabet() encoder = WordEncoder(sep='|', alphabet=morse) encoder.add_word('HTE') encoded_dict = encoder.get_dict() assert encoded_dict['....|-|.'] == 1