Ejemplo n.º 1
0
 def __init__(self, sep='', alphabet=None):
     self.encoded_dict = {}
     self.sep = sep
     if alphabet:
         self.alphabet = alphabet
     else:
         self.alphabet = MorseAlphabet()
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
def context():
    context = Context()
    context.add_sentence('......-...-..---')  # HELLO
    context.add_alphabet(MorseAlphabet())
    return context
Ejemplo n.º 8
0
def test_encoded_letters_first_line():
    morse = MorseAlphabet()
    assert '-..' in morse.get_encoded_letters()
    assert morse.get_encoded_letters_map()['D'] == '-..'
Ejemplo n.º 9
0
def test_encode():
    morse = MorseAlphabet()
    assert morse.encode('D') == '-..'
Ejemplo n.º 10
0
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'] == '.---'
Ejemplo n.º 11
0
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