Exemple #1
0
def merge_dictionary(candidates):
    dictionary = Dictionary([])

    for candidate in candidates:
        dictionary = dictionary.merge(candidate.dictionary())

    return dictionary
    def execute(self, args):

        # Read Dictionary file
        if args.dictionary:
            dictionary_filename = args.dictionary
            Dictionary.read_dict(self.dictionary_mnger, dictionary_filename)

        # Read Original file
        if args.file:
            origin_filename = args.file
            Origin_file.read_file(self.origin_mnger, origin_filename, self.dictionary_mnger)

        # Create output directory
        out_dir = "name_change_output"
        if args.out:
            out_dir = args.out
        os.system('mkdir -p ' + out_dir)

        # Write output file
        fo = open(out_dir + '/output.txt', 'w')
        
        sys.stderr.write("Writing Output...\n")
        fo.write(self.origin_mnger.replaced_lines)

        fo.close()
Exemple #3
0
def main():
    if len(sys.argv) != 2:
        print('usage: python postagger-runner.py model-prefix < input > output', file=sys.stderr)
        return

    model_prefix = sys.argv[1]

    # load dictionary
    word_ids = Dictionary.load(model_prefix + '.wordid')
    pos_ids = Dictionary.load(model_prefix + '.posid')

    # load and test tagger
    tagger = POSTagger.load(model_prefix)
    
    # output queue
    qs = []
    wss = []

    # input iterator
    def iterate_words():
        for l in sys.stdin:
            ls = l.strip('\n').split(' ')
            wss.append(ls)
            for w in ls:
                yield word_ids[w]

    for w, p in tagger.iterate(iterate_words()):
        qs.append(pos_ids.get_name(p))
        if len(qs) >= len(wss[0]):
            print(' '.join('%s/%s' % wq for wq in zip(wss[0], qs)))
            sys.stdout.flush()
            qs = []
            wss.pop(0)
Exemple #4
0
def main():
    if len(sys.argv) != 3:
        print('usage: python postagger-test.py', file=sys.stderr)
        print('                <str: test prefix>', file=sys.stderr)
        print('                <str: model prefix>', file=sys.stderr)
        return

    test_prefix = sys.argv[1]
    model_prefix = sys.argv[2]

    print('loading data ...', file=sys.stderr)

    # load test data
    test_words = [w.lower() for w in utils.read_data(test_prefix + '.words')]
    test_pos = utils.read_data(test_prefix + '.pos')

    # load dictionary
    word_ids = Dictionary.load(model_prefix + '.wordid')
    pos_ids = Dictionary.load(model_prefix + '.posid')

    # make word/POS IDs
    test_wids = [word_ids[w] for w in test_words]
    test_pids = [pos_ids[w] for w in test_pos]

    # load and test tagger
    tagger = POSTagger.load(model_prefix)
    tagger.test(test_wids, test_pids)
    def test_get_part_of_speech(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))

        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        pos = dictionary.get_part_of_speech("torch")
        self.assertEqual(pos, "noun")
    def test_words(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))

        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        words = dictionary.words

        self.assertTrue(len(words), 16)
Exemple #7
0
 def init_systems(self):
     self.dictionary = Dictionary()
     self.input_handler = InputHandler(self.dictionary)
     self.mob_factory = MobFactory()
     self.renderer = Renderer()
     self.event_queue = EventQueue()
     self.event_queue.register_system(self)
     self.message_log = MessageLog(self.event_queue)
     self.combat_system = CombatSystem(self.event_queue)
     self.init_crafting_systems()
    def test_lexer(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "take the torch from the table"
        tokens = parser.lexer(src)
        self.assertEqual(len(tokens), 6)
    def test_parser_with_adjectives_wrong(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "take the key rusty from the table"
        tree = parser.parse(src)

        self.assertFalse(tree)
    def test_verb(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "move"
        tree = parser.parse(src)

        self.assertTrue(tree)
        self.assertEqual(tree.verb.verb.word, "move")
    def test_parser_phrasal_verb(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "pick up the torch"
        tree = parser.parse(src)

        self.assertTrue(tree)
        self.assertEqual(tree.verb.verb.word, "pick up")
        self.assertEqual(tree.noun_phrase.noun.word, "torch")
    def test_parser_with_adjectives(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "take the rusty key from the table"
        tree = parser.parse(src)

        self.assertTrue(tree)
        self.assertTrue(tree.noun_phrase)
        self.assertEqual(tree.noun_phrase.modifier.adjective.word, "rusty")
    def test_visitor_simple(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "move"
        tree = parser.parse(src)

        visitor = Visitor()
        command = tree.accept(visitor)
        self.assertTrue(command)
        self.assertEqual(command["verb"], "move")
    def test_parser_complex(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "take the torch from the table"
        tree = parser.parse(src)

        self.assertTrue(tree)
        self.assertTrue(tree.prep_phrase)
        self.assertEqual(tree.prep_phrase.prep.prep.word, "from")
        self.assertEqual(tree.prep_phrase.noun_phrase.noun.word, "table")
    def test_parser_simple(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "take the torch"
        tree = parser.parse(src)

        self.assertTrue(tree)
        self.assertTrue(isinstance(tree, VerbPhrase))
        self.assertTrue(isinstance(tree.verb.verb.word, str))
        self.assertTrue(tree.verb.verb.word, "take")
        self.assertTrue(tree.noun_phrase.noun.word, "torch")
    def test_parser_with_adverb(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "move north"
        tree = parser.parse(src)

        self.assertTrue(tree)
        self.assertTrue(isinstance(tree, VerbPhrase))
        self.assertTrue(isinstance(tree.verb.verb.word, str))
        self.assertTrue(tree.verb.verb.word, "move")
        self.assertTrue(tree.adverb_phrase.adverb.word, "north")
    def test_load_words(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))

        filename = os.path.join(filedir, "tests/dictionary.json")
        try:
            dictionary.load_words(filename)
        except Exception as e:
            self.assertTrue(False)

        self.assertTrue(len(dictionary.verbs), 3)
        self.assertTrue(len(dictionary.nouns), 3)
        self.assertTrue(len(dictionary.adverbs), 4)
        self.assertTrue(len(dictionary.prepositions), 3)
        self.assertTrue(len(dictionary.adjectives), 1)
        self.assertTrue(len(dictionary.articles), 3)
    def test_parser_phrasal_verb_complex(self):
        dictionary = Dictionary()
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "tests/dictionary.json")
        dictionary.load_words(filename)

        parser = Parser(dictionary)
        src = "pick up the rusty key from the table"
        tree = parser.parse(src)

        self.assertTrue(tree)
        self.assertEqual(tree.verb.verb.word, "pick up")
        self.assertEqual(tree.noun_phrase.modifier.adjective.word, "rusty")
        self.assertEqual(tree.noun_phrase.noun_phrase.noun.word, "key")
        self.assertEqual(tree.prep_phrase.prep.prep.word, "from")
        self.assertEqual(tree.prep_phrase.noun_phrase.noun.word, "table")
Exemple #19
0
def read_txt_embeddings(lang,full_vocab=False):
    """
    Reload pretrained embeddings from a text file.
    """
    word2id = {}
    vectors = []

    # load pretrained embeddings
    emb_path = 'vectors/wiki.{}.vec'.format(lang)
    _emb_dim_file = 300
    max_vocab = 200000

    with io.open(emb_path, 'r', encoding='utf-8', newline='\n', errors='ignore') as f:
        for i, line in enumerate(f):
            if i == 0:
                split = line.split()
                assert len(split) == 2
                assert _emb_dim_file == int(split[1])
            else:
                word, vect = line.rstrip().split(' ', 1)
                if not full_vocab:
                    word = word.lower()
                vect = np.fromstring(vect, sep=' ')
                if np.linalg.norm(vect) == 0:  # avoid to have null embeddings
                    vect[0] = 0.01
                if word in word2id:
                    if full_vocab:
                        print("Word '%s' found twice in embedding file" % (word))
                        pass
                else:
                    if not vect.shape == (_emb_dim_file,):
                        print("Invalid dimension (%i) for word '%s' in line %i." % (vect.shape[0], word, i))
                        continue
                assert vect.shape == (_emb_dim_file,), i
                word2id[word] = len(word2id)
                vectors.append(vect[None])
            if max_vocab > 0 and len(word2id) >= max_vocab and not full_vocab:
                break

    assert len(word2id) == len(vectors)
    # logger.info("Loaded %i pre-trained word embeddings." % len(vectors))

    # compute new vocabulary / embeddings
    id2word = {v: k for k, v in word2id.items()}
    dico = Dictionary(id2word, word2id, lang)
    embeddings = np.concatenate(vectors, 0)

    assert embeddings.shape == (len(dico), _emb_dim_file)
    return dico, embeddings
Exemple #20
0
def main():
    if len(sys.argv) != 9:
        print('usage: python postagger-train.py', file=sys.stderr)
        print('                <str: train prefix>', file=sys.stderr)
        print('                <str: dev prefix>', file=sys.stderr)
        print('                <str: model prefix>', file=sys.stderr)
        print('                <int: word n-gram size>', file=sys.stderr)
        print('                <int: POS n-gram size>', file=sys.stderr)
        print('                <int: word window size>', file=sys.stderr)
        print('                <int: POS history size>', file=sys.stderr)
        print('                <int: max iteration>', file=sys.stderr)
        return

    train_prefix = sys.argv[1]
    dev_prefix = sys.argv[2]
    model_prefix = sys.argv[3]
    word_ngram_size = int(sys.argv[4])
    pos_ngram_size = int(sys.argv[5])
    word_window_size = int(sys.argv[6])
    pos_history_size = int(sys.argv[7])
    max_iteration = int(sys.argv[8])

    print('loading data ...', file=sys.stderr)

    # load train/dev data
    train_words = [w.lower() for w in utils.read_data(train_prefix + '.words')]
    train_pos = utils.read_data(train_prefix + '.pos')
    dev_words = [w.lower() for w in utils.read_data(dev_prefix + '.words')]
    dev_pos = utils.read_data(dev_prefix + '.pos')

    # make dictionary
    word_ids = Dictionary(train_words, frozen=True)
    pos_ids = Dictionary(train_pos, frozen=True)
    word_ids.save(model_prefix + '.wordid')
    pos_ids.save(model_prefix + '.posid')

    # make word/POS IDs
    train_wids = [word_ids[w] for w in train_words]
    train_pids = [pos_ids[w] for w in train_pos]
    dev_wids = [word_ids[w] for w in dev_words]
    dev_pids = [pos_ids[w] for w in dev_pos]

    # train
    tagger = POSTagger(word_ngram_size, pos_ngram_size, word_window_size, pos_history_size)
    tagger.train(len(pos_ids), train_wids, train_pids, dev_wids, dev_pids, max_iteration, model_prefix)
Exemple #21
0
 def dictionary(self):
     return Dictionary(self.words)
 def test_contains_when_dictionary_is_empty(self):
     self.assertFalse("baz" in Dictionary([]))
 def test_contains_when_dictionary_is_none(self):
     with self.assertRaises(TypeError, msg="Words must be a list."):
         "baz" in Dictionary(None)
 def test_merge_when_other_is_empty(self):
     self.assertEqual(set(self.dictionary.merge(Dictionary([])).words),
                      set(self.dictionary.words))
 def test_merge_when_this_is_empty(self):
     self.assertEqual(set(Dictionary([]).merge(self.dictionary)),
                      set(self.dictionary.words))
class TestDictionary(TestCase):
    def setUp(self):
        self.dictionary = Dictionary(["foo", "bar"])

    def test_letters(self):
        self.assertEqual(set("foobar"), self.dictionary.letters())

    def test_letter_frequencies(self):
        self.assertEqual({
            "f": 0.5,
            "o": 1,
            "b": 0.5,
            "a": 0.5,
            "r": 0.5
        }, self.dictionary.letter_frequencies())

    def test_generate(self):
        text = self.dictionary.generate(10, indexer)

        self.assertEqual("foo foo fo", text)

    def test_shuffle(self):
        shuffled = self.dictionary.shuffle(shuffle)

        self.assertTrue("foo" in shuffled)
        self.assertNotEqual(shuffled, self.dictionary)

    def test_contains_when_in_dictionary(self):
        self.assertTrue("foo" in self.dictionary)

    def test_contains_when_not_in_dictionary(self):
        self.assertFalse("baz" in self.dictionary)

    def test_contains_when_dictionary_is_empty(self):
        self.assertFalse("baz" in Dictionary([]))

    def test_contains_when_dictionary_is_none(self):
        with self.assertRaises(TypeError, msg="Words must be a list."):
            "baz" in Dictionary(None)

    def test_contains_when_word_is_none(self):
        self.assertFalse(None in self.dictionary)

    def test_contains_when_word_is_not_a_string(self):
        self.assertFalse(1 in self.dictionary)

    def test_merge_when_other_is_empty(self):
        self.assertEqual(set(self.dictionary.merge(Dictionary([])).words),
                         set(self.dictionary.words))

    def test_merge_when_this_is_empty(self):
        self.assertEqual(set(Dictionary([]).merge(self.dictionary)),
                         set(self.dictionary.words))

    def test_merge_when_this_is_none(self):
        with self.assertRaises(
                AttributeError,
                msg="'NoneType' object has no attribute 'words'"):
            self.dictionary.merge(None)

    def test_read_from_file(self):
        self.dictionary = src.dictionary.read_from_file(
            "test2_candidate_70_english_words.txt")

        self.assertEqual(len(self.dictionary), 70)
        self.assertTrue("vessel" in self.dictionary)
 def setUp(self):
     self.dictionary = Dictionary(["foo", "bar"])
Exemple #28
0
class Test:
    def __init__(
            self,
            checkpoint_path=r"C:\Workplaces\NLP\Project\test\MachineTranslation\outputs\train1",
            dictionary_path='datasets/vi_zh') -> None:
        loader = DataLoader()
        content_cn = loader.np_load('lst_cn_all_with6k_except_1001')
        content_vn = loader.np_load('lst_vi_all_with6k_except_1001')
        for i in range(len(content_vn)):
            content_vn[i] = content_vn[i].lower()
        for i in range(len(content_cn)):
            content_cn[i] = self.preproces_cn(content_cn[i])

        from sklearn.model_selection import train_test_split
        X_train, X_test, y_train, y_test = train_test_split(content_cn,
                                                            content_vn,
                                                            test_size=0.2,
                                                            random_state=1)
        X_val, y_val = [X_train[0]], [y_train[0]]

        full_dataset = self.create_dataset(content_cn, content_vn)
        train_examples = self.create_dataset(X_train, y_train)
        test_dataset = self.create_dataset(X_test, y_test)
        val_dataset = self.create_dataset(X_val, y_val)

        self.tokenizer_cn = tfds.deprecated.text.SubwordTextEncoder.build_from_corpus(
            (en.numpy() for en, _ in full_dataset), target_vocab_size=2**13)

        self.tokenizer_vn = tfds.deprecated.text.SubwordTextEncoder.build_from_corpus(
            (vn.numpy() for _, vn in full_dataset), target_vocab_size=2**13)

        BUFFER_SIZE = 2000
        BATCH_SIZE = 64
        train_dataset = train_examples.map(self.tf_encode)
        train_dataset = train_dataset.filter(self.filter_max_length)
        train_dataset = train_dataset.cache()
        train_dataset = train_dataset.shuffle(BUFFER_SIZE)
        train_dataset = train_dataset.prefetch(tf.data.experimental.AUTOTUNE)
        val_dataset = val_dataset.map(self.tf_encode)
        val_dataset = val_dataset.filter(self.filter_max_length)
        test_dataset = test_dataset.map(self.tf_encode)
        test_dataset = test_dataset.filter(self.filter_max_length)

        num_layers = 6
        d_model = 256  # model dim
        dff = 512  # feed forward dim
        num_heads = 8  # number of multi head attention d_model%num_heads == 0

        input_vocab_size = self.tokenizer_cn.vocab_size + 2
        target_vocab_size = self.tokenizer_vn.vocab_size + 2
        dropout_rate = 0.1
        learning_rate = CustomSchedule(d_model)
        optimizer = tf.keras.optimizers.Adam(learning_rate,
                                             beta_1=0.9,
                                             beta_2=0.98,
                                             epsilon=1e-9)
        self.dic = Dictionary().create_dict(dictionary_path)
        self.transformer = Transformer(num_layers,
                                       d_model,
                                       num_heads,
                                       dff,
                                       input_vocab_size,
                                       target_vocab_size,
                                       pe_input=input_vocab_size,
                                       pe_target=target_vocab_size,
                                       rate=dropout_rate)

        ckpt = tf.train.Checkpoint(transformer=self.transformer,
                                   optimizer=optimizer)

        ckpt_manager = tf.train.CheckpointManager(ckpt,
                                                  checkpoint_path,
                                                  max_to_keep=5)
        if ckpt_manager.latest_checkpoint:
            ckpt.restore(ckpt_manager.latest_checkpoint).expect_partial()
            print('Latest checkpoint restored!!')
        else:
            raise 'Checkpoint not found!'

    def preproces_cn(self, s):
        return re.sub('\s+', ' ', ' '.join(s))
        seg_list = jieba.cut(s)
        return " ".join(seg_list)

    def create_dataset(self, x, y):
        a = tf.data.Dataset.from_tensor_slices(x)  # ==> [ 1, 2, 3 ]
        b = tf.data.Dataset.from_tensor_slices(y)
        ds = tf.data.Dataset.zip((a, b))
        ds = ds.shuffle(buffer_size=1000)
        return ds

    def encode(self, lang1, lang2):
        lang1 = [self.tokenizer_cn.vocab_size] + self.tokenizer_cn.encode(
            lang1.numpy()) + [self.tokenizer_cn.vocab_size + 1]

        lang2 = [self.tokenizer_vn.vocab_size] + self.tokenizer_vn.encode(
            lang2.numpy()) + [self.tokenizer_vn.vocab_size + 1]

        return lang1, lang2

    def tf_encode(self, en, vn):
        result_en, result_vn = tf.py_function(self.encode, [en, vn],
                                              [tf.int64, tf.int64])
        result_en.set_shape([None])
        result_vn.set_shape([None])

        return result_en, result_vn

    def filter_max_length(self, x, y, max_length=MAX_LENGTH):
        return tf.logical_and(
            tf.size(x) <= max_length,
            tf.size(y) <= max_length)

    def evaluate(self, inp_sentence):
        start_token = [self.tokenizer_cn.vocab_size]
        end_token = [self.tokenizer_cn.vocab_size + 1]

        # inp sentence is eng, hence adding the start and end token
        inp_sentence = start_token + self.tokenizer_cn.encode(
            inp_sentence) + end_token
        encoder_input = tf.expand_dims(inp_sentence, 0)

        # as the target is vn, the first word to the transformer should be the
        # english start token.
        decoder_input = [self.tokenizer_vn.vocab_size]
        output = tf.expand_dims(decoder_input, 0)

        enc_padding_mask, combined_mask, dec_padding_mask = create_masks(
            encoder_input, output)
        enc_output = self.transformer.encoder(encoder_input, False,
                                              enc_padding_mask)

        for i in range(MAX_LENGTH):
            enc_padding_mask, combined_mask, dec_padding_mask = create_masks(
                encoder_input, output)
            dec_output, attention_weights = self.transformer.decoder(
                output, enc_output, False, combined_mask, dec_padding_mask)
            predictions = self.transformer.final_layer(dec_output)
            predictions = predictions[:, -1:, :]  # (batch_size, 1, vocab_size)
            predicted_id = tf.cast(tf.argmax(predictions, axis=-1), tf.int32)
            if predicted_id == self.tokenizer_vn.vocab_size + 1:
                return tf.squeeze(output, axis=0), attention_weights
            output = tf.concat([output, predicted_id], axis=-1)
        return tf.squeeze(output, axis=0), attention_weights

    def translate(self, sentence, plot=''):
        if self.dic.get(sentence):
            return self.dic[sentence]
        else:
            sentence = self.preproces_cn(sentence)
            result, attention_weights = self.evaluate(sentence)
            predicted_sentence = self.tokenizer_vn.decode(
                [i for i in result if i < self.tokenizer_vn.vocab_size])
            return predicted_sentence
Exemple #29
0
from src.dictionary import Dictionary
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

d = Dictionary()

message = "e18d1c933da3f3bf1517ec5033fff8f7998d8108d88d04688510f94dde36d5d69f3a3fb92e4515d72204fd420d079156e0434caeee957c9784390c85e4354acb"

for i in range(0, len(d.wordarray)):
    length = len(d.wordarray[i])
    key = bytearray.fromhex(d.wordarray[i].encode("utf-8").hex())
    if length < 16:
        key = pad(key, 16)
    elif 16 < length < 24:
        key = pad(key, 24)
    elif 24 < length < 32:
        key = pad(key, 32)

    if not len(key) % 8 == 0:
        continue

    cipher = AES.new(key, AES.MODE_ECB)

    plain_text = cipher.decrypt(pad(bytearray.fromhex(message), 16))
    try:
        print(plain_text.decode('utf-8'))
    except:
        continue
Exemple #30
0
    def __init__(
            self,
            checkpoint_path=r"C:\Workplaces\NLP\Project\test\MachineTranslation\outputs\train1",
            dictionary_path='datasets/vi_zh') -> None:
        loader = DataLoader()
        content_cn = loader.np_load('lst_cn_all_with6k_except_1001')
        content_vn = loader.np_load('lst_vi_all_with6k_except_1001')
        for i in range(len(content_vn)):
            content_vn[i] = content_vn[i].lower()
        for i in range(len(content_cn)):
            content_cn[i] = self.preproces_cn(content_cn[i])

        from sklearn.model_selection import train_test_split
        X_train, X_test, y_train, y_test = train_test_split(content_cn,
                                                            content_vn,
                                                            test_size=0.2,
                                                            random_state=1)
        X_val, y_val = [X_train[0]], [y_train[0]]

        full_dataset = self.create_dataset(content_cn, content_vn)
        train_examples = self.create_dataset(X_train, y_train)
        test_dataset = self.create_dataset(X_test, y_test)
        val_dataset = self.create_dataset(X_val, y_val)

        self.tokenizer_cn = tfds.deprecated.text.SubwordTextEncoder.build_from_corpus(
            (en.numpy() for en, _ in full_dataset), target_vocab_size=2**13)

        self.tokenizer_vn = tfds.deprecated.text.SubwordTextEncoder.build_from_corpus(
            (vn.numpy() for _, vn in full_dataset), target_vocab_size=2**13)

        BUFFER_SIZE = 2000
        BATCH_SIZE = 64
        train_dataset = train_examples.map(self.tf_encode)
        train_dataset = train_dataset.filter(self.filter_max_length)
        train_dataset = train_dataset.cache()
        train_dataset = train_dataset.shuffle(BUFFER_SIZE)
        train_dataset = train_dataset.prefetch(tf.data.experimental.AUTOTUNE)
        val_dataset = val_dataset.map(self.tf_encode)
        val_dataset = val_dataset.filter(self.filter_max_length)
        test_dataset = test_dataset.map(self.tf_encode)
        test_dataset = test_dataset.filter(self.filter_max_length)

        num_layers = 6
        d_model = 256  # model dim
        dff = 512  # feed forward dim
        num_heads = 8  # number of multi head attention d_model%num_heads == 0

        input_vocab_size = self.tokenizer_cn.vocab_size + 2
        target_vocab_size = self.tokenizer_vn.vocab_size + 2
        dropout_rate = 0.1
        learning_rate = CustomSchedule(d_model)
        optimizer = tf.keras.optimizers.Adam(learning_rate,
                                             beta_1=0.9,
                                             beta_2=0.98,
                                             epsilon=1e-9)
        self.dic = Dictionary().create_dict(dictionary_path)
        self.transformer = Transformer(num_layers,
                                       d_model,
                                       num_heads,
                                       dff,
                                       input_vocab_size,
                                       target_vocab_size,
                                       pe_input=input_vocab_size,
                                       pe_target=target_vocab_size,
                                       rate=dropout_rate)

        ckpt = tf.train.Checkpoint(transformer=self.transformer,
                                   optimizer=optimizer)

        ckpt_manager = tf.train.CheckpointManager(ckpt,
                                                  checkpoint_path,
                                                  max_to_keep=5)
        if ckpt_manager.latest_checkpoint:
            ckpt.restore(ckpt_manager.latest_checkpoint).expect_partial()
            print('Latest checkpoint restored!!')
        else:
            raise 'Checkpoint not found!'
Exemple #31
0
class Engine:
    def __init__(self):
        self._id = 0
        self.playing = False
        self.moved = True
        self.enemy = None
        self.npc = None
        self.combatants = None
        self.explore_actions = {
            "move": self.move,
            "take": self.take,
            "look": self.look,
            "search": self.search,
            "unlock": self.unlock,
            "throw": self.throw,
            "drop": self.drop,
            "examine": self.examine,
            "trade": self.trade,
            "sleep": self.sleep,
            "speak": self.speak,
            "use": self.use,
            "consume": self.use,
            "equip": self.equip,
            "unequip": self.unequip,
            "mine": self.use
        }

    def init_crafting_systems(self):
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/crafting.json")
        systems = []

        with open(filename) as f:
            data = json.load(f)
            for system in data:
                systems.append(CraftingSystem(self.event_queue, **system))

        self.crafting_systems = systems

    def init_systems(self):
        self.dictionary = Dictionary()
        self.input_handler = InputHandler(self.dictionary)
        self.mob_factory = MobFactory()
        self.renderer = Renderer()
        self.event_queue = EventQueue()
        self.event_queue.register_system(self)
        self.message_log = MessageLog(self.event_queue)
        self.combat_system = CombatSystem(self.event_queue)
        self.init_crafting_systems()

    def init_game_data(self):
        self.world_map = WorldMap()

        # load rooms
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/rooms.json")
        self.world_map.load_rooms(filename)

        # load cutscenes
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/cutscenes.json")
        self.world_map.load_cutscenes(filename)

        # load items
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/items.json")
        self.world_map.load_items(filename)

        #load mobs
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/enemies.json")
        self.world_map.load_mobs(filename)

        # load npcs
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/npcs.json")
        self.world_map.load_npcs(filename)

        # load quests
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/quests.json")
        self.world_map.load_quests(filename)

        # load dictionary
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/dictionary.json")
        self.dictionary.load_words(filename)

    def create_player(self):
        filedir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(filedir, "resources/player.json")
        with open(filename) as f:
            data = json.load(f)
            self.player = Player(**data)

    def get_crafting_system(self, name):
        """Retreive the specified crafting system."""

        for system in self.crafting_systems:
            if system.name == name:
                return system

    def list_items(self, container):
        """Builds a string containing all items in a container."""

        str = "{}{}{}"
        for i, _id in enumerate(container):
            item = self.world_map.items[_id]
            if i == 0:
                str = str.format(c.yellow, item.name, c.end)
                continue
            if i == len(container) - 1:
                str = "{} and {}{}{}".format(str, c.yellow, item.name, c.end)
                break
            str = "{}, {}{}{}".format(str, c.yellow, item.name, c.end)

        if str != "":
            if len(container) > 1:
                str = "You see the following items: {}".format(str)
            else:
                if str[0] == "a":
                    str = "You see an {}{}{}.".format(c.yellow, str, c.end)
                else:
                    str = "You see a {}{}{}.".format(c.yellow, str, c.end)
            self.event_queue.add_event({"message": str})

    def check_object(self, object, container, flag=0):
        """Checks if the specified object is in the passed container.

        flag = 0 represents an inanimate container (room or item), whereas
        flag = 1 represents an animate container (NPC).
        """

        if flag == 0:
            for _id in container:
                item = self.world_map.items[_id]
                if item.name.lower() == object:
                    return item

        elif flag == 1:
            names = [
                self.world_map.npcs[npc].name.lower() for npc in container
            ]
            if object in ["man", "woman"]:
                object = names[0]

            if object in names:
                for _id in container:
                    npc = self.world_map.npcs[_id]
                    if npc.name.lower() == object:
                        return npc
        else:
            return False

    def transition(self, from_id, to_id):
        """Represents the transition between moving from room A to room B."""

        from_room = self.world_map.get_room_at(from_id)
        to_room = self.world_map.get_room_at(to_id)
        self.moved = True

        self.world_map._id = to_id

        if not to_room.safe:
            if randrange(1, 100) < to_room.fight_chance:
                self.state = State.PRE_BATTLE
                mob_index = choice(to_room.mobs)
                mob_data = self.world_map.mobs[mob_index]
                mob = self.mob_factory.spawn(**mob_data)
                self.enemy = mob
                self.combatants = sorted([self.player, self.enemy],
                                         key=lambda entity: entity.speed)[::-1]
                return

        if to_room.tryattr("cutscene"):
            if to_room.cutscene == self.world_map.cutscene_id:
                for line in self.world_map.cutscenes[to_room.cutscene]["text"]:
                    self.event_queue.add_event({"message": line})
                self.world_map.cutscene_id += 1
                self.state = State.CUTSCENE
                return

    def move_player(self, action):
        """Moves the player in the direction specified if an exit exists."""

        exits = self.world_map.get_current_exits()
        index = self.world_map.directions.index(action["adverb"])
        if exits[index] != -1:
            self.transition(self.world_map._id, exits[index])
        else:
            msg = "There's no way out that way."
            self.event_queue.add_event({"message": msg})

    def move_item(self, action):
        """Passes to the 'use' method to call the 'move' script on an item."""
        self.use(action)

    def move(self, action):
        """Handles the two 'move' commands."""
        if (action["adverb"] in self.world_map.directions
                and not action["noun_phrase"]):
            self.move_player(action)
        else:
            self.move_item(action)

    def take(self, action):
        """Moves the specified item into the player's inventory."""

        items = [self.world_map.get_items_in_room()]
        object = action["noun_phrase"]
        prep = None
        indir_obj = None

        if action["prep_phrase"]:
            prep = action["prep_phrase"]["prep"]
            indir_obj = action["prep_phrase"]["noun_phrase"]

        if not object:
            self.event_queue.add_event({"message": "I don't understand that!"})
            return

        if indir_obj:
            switched = False
            item = self.check_object(indir_obj, items[0], flag=0)
            if item:
                if not item.locked:
                    items[0] = item.container
                    switched = True
                if item.locked:
                    self.event_queue.add_event({"message": "That's locked!"})
                    return

            if not switched:
                msg = "{}{}{} isn't here.".format(c.yellow, indir_obj, c.end)
                self.event_queue.add_event({"message": msg})
        else:
            for _id in items[0]:
                if self.world_map.items[_id].tryattr("container"):
                    if (self.world_map.items[_id].container
                            and self.world_map.items[_id].searched):
                        items.append(self.world_map.items[_id].container)

        taken = False
        attempted = False
        for container in items:
            item = self.check_object(object, container, flag=0)
            if item:
                if item.takeable:
                    if indir_obj:
                        msg = ("You take the {}{}{} from the {}{}{}.".format(
                            c.yellow, item.name, c.end, c.yellow, indir_obj,
                            c.end))
                        self.event_queue.add_event({"message": msg})
                    else:
                        msg = ("You take the {}{}{}.".format(
                            c.yellow, item.name, c.end))
                        self.event_queue.add_event({"message": msg})
                    self.player.container.append(item._id)
                    container.remove(item._id)
                    taken = True
                elif not item.takeable:
                    attempted = True
                    msg = "You're quite strong, but even you can't lift that..."
                    self.event_queue.add_event({"message": msg})

        if not taken and not attempted:
            msg = "{}{}{} isn't here.".format(c.yellow, object, c.end)
            self.event_queue.add_event({"message": msg})

        if taken:
            self.event_queue.add_event({"taken": item})

    def look_direction(self, action):
        """Reveals the description of the room in the specified direction."""

        exits = self.world_map.get_current_exits()
        index = self.world_map.directions.index(action["adverb"])

        if exits[index] == -1:
            msg = "You can't see anything of interest in that direction."
            self.event_queue.add_event({"message": msg})
        else:
            look_room = self.world_map.rooms[exits[index]]
            direction = action["adverb"]
            msg = ("You look {}{}{} and see the {}{}{}.".format(
                c.blue, direction, c.end, c.blue, look_room.name.title(),
                c.end))
            self.event_queue.add_event({"message": msg})

    def look_room(self, action):
        """Reveals items in, and extra info about, the current room."""

        ids = self.world_map.get_items_in_room()
        room = self.world_map.get_current_room()
        self.event_queue.add_event({"message": room.long_desc})

        if not ids:
            msg = "There's nothing of interest here..."
            self.event_queue.add_event({"message": msg})
        else:
            self.list_items(ids)

    def look(self, action):
        """Handles the two 'look' commands."""

        if action["adverb"] is not None:
            self.look_direction(action)
        else:
            self.look_room(action)

    def search(self, action):
        """Reveals any items stored within another object."""

        ids = self.world_map.get_items_in_room()
        object = action["noun_phrase"]

        item = self.check_object(object, ids, flag=0)

        if not item:
            msg = "You can't search something that's not here..."
            self.event_queue.add_event({"message": msg})
            return

        if item.tryattr("in_sight"):
            if item.in_sight:
                if item.container:
                    self.list_items(item.container)
                    item.searched = True
                else:
                    msg = "There's nothing here..."
                    self.event_queue.add_event({"message": msg})
            else:
                if item.tryattr("locked"):
                    if item.locked:
                        msg = "You can't see inside that."
                        self.event_queue.add_event({"message": msg})
                    else:
                        if item.container:
                            self.list_items(item.container)
                            item.searched = True
                        else:
                            msg = "There's nothing here..."
                            self.event_queue.add_event({"message": msg})

    def unlock(self, action):
        """Unlocks a locked object if the player has the right key."""

        object = action["noun_phrase"]
        unlocked = False
        items = self.world_map.get_items_in_room()
        item = self.check_object(object, items, flag=0)

        if item.tryattr("locked"):
            for _item in self.player.container:
                if item.locked_id == _item:
                    item.locked = False
                    self.player.container.remove(_item)
                    msg = "You unlocked the {}.".format(item.name)
                    self.event_queue.add_event({"message": msg})
                    unlocked = True

        if not unlocked:
            msg = "You don't have the right key for that..."
            self.event_queue.add_event({"message": msg})

    def collide(self, item_1, item_2, flag=0):
        """Represents the collision between two objects.

        flag = 0 means the target object is inanimate, flag = 1 means the player
        has thrown something at an NPC.
        """

        hit = randrange(1, 100) < 70

        if not hit:
            self.event_queue.add_event({
                "message":
                "You throw the {}{}{} but it misses!".format(
                    c.yellow, item_1.name, c.end)
            })
        else:
            if flag == 0:
                msg = ("You throw the {}{}{} at the {}{}{}.".format(
                    c.yellow, item_1.name, c.end, c.yellow, item_2.name,
                    c.end))
                self.event_queue.add_event({"message": msg})
                if item_2.breakable:
                    msg = (
                        "The {}{}{} strikes the {}{}{} and breaks it!".format(
                            c.yellow, item_1.name, c.end, c.yellow,
                            item_2.name, c.end))
                    self.event_queue.add_event({"message": msg})
                    if item_2.tryattr("container"):
                        room = self.world_map.get_current_room()
                        room.container += item_2.container
                        msg = ("The {}{}{} spills its contents onto the floor."
                               .format(c.yellow, item_2.name, c.end))
                        self.event_queue.add_event({"message": msg})
                        room.container.remove(item_2._id)
            elif flag == 1:
                msg = "Ow! Why'd you throw the {} at me???".format(item_1.name)
                self.event_queue.add_event({"message": msg})
                self.state = State.DIALOG
                self.npc = item_2

        self.player.container.remove(item_1._id)
        self.world_map.get_current_room().container.append(item_1._id)

    def throw(self, action):
        """Throw the specified item at another object."""

        object = action["noun_phrase"]
        prep = None
        indir_obj = None
        items = self.world_map.get_items_in_room()
        npcs = self.world_map.get_npcs_in_room()

        if action["prep_phrase"]:
            prep = action["prep_phrase"]["prep"]
            indir_obj = action["prep_phrase"]["noun_phrase"]

        if not object:
            msg = "I don't understand that..."
            self.event_queue.add_event({"message": msg})
            return

        if prep not in ["at"]:
            msg = "You have to throw something AT something else."
            self.event_queue.add_event({"message": msg})
            return

        if not indir_obj:
            msg = "You have to throw an object at something."
            self.event_queue.add_event({"message": msg})
            return

        item_2 = self.check_object(indir_obj, items)
        item_1 = self.check_object(object, self.player.container)

        if not item_1:
            msg = "You don't have one of those to throw."
            self.event_queue.add_event({"message": msg})
            return

        if not item_2:
            item_2 = self.check_object(indir_obj, npcs, flag=1)

            if not item_2:
                msg = "You can't throw an object at something that isn't here!"
                self.event_queue.add_event({"message": msg})
                return

        if item_2.tryattr("takeable"):
            self.collide(item_1, item_2)
        else:
            self.collide(item_1, item_2, flag=1)

    def drop(self, action):
        """Drop the specified item."""

        items = self.world_map.get_items_in_room()
        object = action["noun_phrase"]
        prep = None
        indir_obj = None

        item = self.check_object(object, self.player.container)

        if not item:
            if object in self.dictionary.words:
                msg = "You don't have one of those..."
                self.event_queue.add_event({"message": msg})
                return
            else:
                msg = "I don't understand that..."
                self.event_queue.add_event({"message": msg})
                return

        put = False
        if action["prep_phrase"]:
            prep = action["prep_phrase"]["prep"]
            indir_obj = action["prep_phrase"]["noun_phrase"]

        if prep:
            if prep in ["in", "on"]:
                indir_obj = self.check_object(indir_obj, items)
                indir_obj.container.append(item._id)
                self.player.container.remove(item._id)
                msg = ("You put the {}{}{} {} the {}{}{}".format(
                    c.yellow, item.name, c.end, prep, c.yellow, indir_obj.name,
                    c.end))
                self.event_queue.add_event({"message": msg})
            else:
                msg = "That doesn't make sense..."
                self.event_queue.add_event({"message": msg})
        else:
            room = self.world_map.get_current_room()
            room.container.append(item._id)
            self.player.container.remove(item._id)
            msg = ("You drop the {}{}{}.".format(c.yellow, item.name, c.end))
            self.event_queue.add_event({"message": msg})

    def examine(self, action):
        """Retreive the specified item's long description."""

        ids = self.world_map.get_items_in_room()
        object = action["noun_phrase"]
        item = None

        # make sure there is an object to examine
        if not object:
            msg = "You need to examine SOMETHING."
            self.event_queue.add_event({"message": msg})
            return

        # get item from room
        if ids:
            item = self.check_object(object, ids, flag=0)

        # get item from the player
        if not item:
            item = self.check_object(object, self.player.container, flag=0)

        # check player's equipped items
        if not item:
            all_slots = list(self.player.slots.values())
            equipped_items = [i for i in all_slots if i is not None]
            if equipped_items is not None:
                for id in equipped_items:
                    item = self.world_map.items[id]
                    if item.name == object:
                        break

        # get item from containers in the room
        if not item:
            room = self.world_map.get_current_room()
            for _id in ids:
                container = self.world_map.items[_id].container
                if container:
                    item = self.check_object(object, container, flag=0)
                    if item:
                        break

        if item:
            self.event_queue.add_event({"message": item.long_desc})
        else:
            msg = "You can't examine the {} if it's not here...".format(object)
            self.event_queue.add_event({"message": msg})

    def trade(self, action):

        pass

    def rest(self, on_floor=True):
        """Simulates resting and heals the player based on the type of bed."""
        for i in range(2):
            self.event_queue.add_event({"message": "ZZZzzzz"})

        if on_floor:
            self.player.hp += int(self.player.max_hp / 10)
            self.player.hp = min(self.player.hp, self.player.max_hp)
            self.event_queue.add_event(
                {"message": "You wake up feeling sore."})
        elif not on_floor:
            self.player.hp = self.player.max_hp
            self.event_queue.add_event({"message": "You feel well rested."})
        self.state = State.SLEEP

    def sleep(self, action):
        """Allows the player to 'sleep', providing there is a suitable bed."""

        prep_phrase = action.get("prep_phrase")
        prep = None
        object = None
        room = self.world_map.get_current_room()

        if prep_phrase:
            prep = action.get("prep_phrase")["prep"]
            object = action.get("prep_phrase")["noun_phrase"]

            if prep != "on":
                msg = "I don't understand that..."
                self.event_queue.add_event({"message": msg})
                return

            item = self.check_object(object, room.container)
            if item:
                if item.tryattr("sleep"):
                    # can sleep on it
                    self.rest(on_floor=False)
                else:
                    msg = "You can't sleep on that."
                    self.event_queue.add_event({"message": msg})
                    return
            else:
                if object == "floor":
                    self.rest(on_floor=True)
        else:
            msg = "You need to sleep ON something..."
            self.event_queue.add_event({"message": msg})

    def handle_quest(self, npc):
        """Starts or ends a quest with an NPC."""

        quest = self.world_map.quests[npc.quest_id]

        if quest.check_requirements(self.player):
            if npc.quest_id in self.player.quests:
                if quest.check_objective(self.player, self.event_queue):
                    for line in quest.outro:
                        self.event_queue.add_event({"message": line})
                    quest.on_completion(self.player, self.event_queue)
                    self.state = State.REWARD
            else:
                for line in quest.intro:
                    self.event_queue.add_event({"message": line})
                self.event_queue.add_event({"message": "y/n?"})

    def identify_npc(self, indir_obj, npcs):
        """Attempts to determine the NPC name when referred to indirectly."""

        if indir_obj in ["man", "gentleman"]:
            if self.world_map.npcs[npcs[0]].sex == 0:
                return self.world_map.npcs[npcs[0]].name.lower()
            else:
                msg = "There isn't a man here..."
                self.event_queue.add_event({"message": msg})
                return
        elif indir_obj in ["woman", "lady"]:
            if self.world_map.npcs[npcs[0]].sex == 1:
                return self.world_map.npcs[npcs[0]].name.lower()
            else:
                msg = "There isn't a woman here..."
                self.event_queue.add_event({"message": msg})
                return

    def introduce_npc(self, npc):
        """Introduces an NPC for the first time."""

        npc.known = True
        msg = ("My name's {}. Nice to meet you, friend.".format(
            npc.name.capitalize()))
        self.event_queue.add_event({"message": msg})
        self.event_queue.add_event({"meeting": npc})

    def speak(self, action):
        """Begins dialog with an NPC, leading potentially to quest giving."""

        npcs = self.world_map.get_npcs_in_room()
        prep = None
        indir_obj = None

        if action["prep_phrase"]:
            prep = action["prep_phrase"]["prep"]
            indir_obj = action["prep_phrase"]["noun_phrase"]

        if prep not in ["to", "with"]:
            self.event_queue.add_event(
                {"message": "That doesn't make sense..."})
            msg = ("Do you mean \"{} to\" or \"{} with\"?".format(
                action["verb"], action["verb"]))
            self.event_queue.add_event({"message": msg})
            return

        indir_obj = self.identify_npc(indir_obj, npcs)

        npc = self.check_object(indir_obj, npcs, flag=1)

        if not npc:
            names = [self.world_map.npcs[npc].name.lower() for npc in npcs]
            if indir_obj in names:
                msg = ("I'm sorry, but {}{}{} isn't here...".format(
                    c.green, indir_obj.capitalize(), c.end))
                self.event_queue.add_event({"message": msg})
                return
            else:
                if indir_obj[0] == "a":
                    msg = ("If you try talking to an {}{}{}, \
                                people will think you're mad.".format(
                        c.yellow, indir_obj, c.end))
                    self.event_queue.add_event({"message": msg})
                    return
                else:
                    msg = ("If you try talking to a {}{}{}, \
                                people will think you're mad.".format(
                        c.yellow, indir_obj, c.end))
                    self.event_queue.add_event({"message": msg})
                    return

        self.npc = npc

        self.state = State.DIALOG

        if not npc.known:
            self.introduce_npc(npc)
        else:
            self.event_queue.add_event({"message": choice(s.GREETINGS)})

        if npc.quest_id >= 0:
            self.handle_quest(npc)

    def use(self, action):
        """Activates an item's attached script."""

        object = action.get("noun_phrase")
        item = self.check_object(object, self.player.container)
        room = self.world_map.get_current_room()

        if not item:
            item = self.check_object(object, room.container)
            if not item:
                msg = "That doesn't make sense..."
                self.event_queue.add_event({"message": msg})
                return
            if item.takeable:
                item = None

        if item:
            if item.tryattr("script"):
                method_to_call = getattr(src.systems.scripts, item.script)
                kwargs = {
                    "world_map": self.world_map,
                    "event_queue": self.event_queue,
                    "player": self.player,
                    "item": item
                }
                method_to_call(**kwargs)
            else:
                self.event_queue.add_event({"message": "You can't do that..."})
        else:
            self.event_queue.add_event({"message": "You can't do that..."})

    def equip(self, action):
        """Equips the specified item into the relevant slot.

        If there is already an item in the relevant slot, this will be
        unequipped and placed into the player's inventory and the specified item
        will be equipped.
        """

        object = action.get("noun_phrase")

        item = self.check_object(object, self.player.container)

        if item:
            if item.tryattr("slot"):
                if not self.player.slots[item.slot]:
                    self.player.slots[item.slot] = item._id
                elif self.player.slots[item.slot]:
                    self.player.container.append(self.player.slot[item.slot])
                    self.player.slots[item.slot] = item._id
                msg = "You equip {}{}{}.".format(c.yellow, item.name, c.end)
                self.event_queue.add_event({"message": msg})
                self.player.container.remove(item._id)
            else:
                msg = "You can't equip that..."
                self.event_queue.add_event({"message": msg})
        else:
            msg = "That doesn't make sense..."
            self.event_queue.add_event({"message": msg})

    def unequip(self, action):
        """Unequips the specified item from the relevant slot."""

        object = action.get("noun_phrase")

        items = [i for i in list(self.player.slots.values()) if i]
        item = self.check_object(object, items)

        if item:
            if item.tryattr("slot"):
                self.player.slots[item.slot] = None
                self.player.container.append(item._id)
                msg = ("You unequip the {}{}{}".format(c.yellow, item.name,
                                                       c.end))
                self.event_queue.add_event({"message": msg})
        else:
            msg = "You haven't got that equipped."
            self.event_queue.add_event({"message": msg})

    def add_quest(self):
        """Adds a quest to the player's quest log."""

        quest = self.world_map.quests[self.npc.quest_id]
        self.player.quests.append(quest.id)
        msg = ("You accepted {}{}{} quest.".format(c.green, quest.name.title(),
                                                   c.end))
        self.event_queue.add_event({"message": msg})

    def choose(self, action):
        """Presents the player the choice to accept or deny a quest."""

        choice = action.get("choice")
        if choice:
            if choice == "yes":
                self.add_quest()
                self.progress_quests()
                self.npc = None
                self.state = State.EXPLORE
                return
            elif choice == "no":
                self.state = State.EXPLORE
                return

        quest = self.world_map.quests[self.npc.quest_id]
        if not quest.check_requirements(self.player):
            self.npc = None
            self.state = State.EXPLORE

    def loot(self):
        """Moves from the loot state back to the explore state after a fight."""

        self.moved = True
        self.state = State.EXPLORE

    def pre_fight(self):
        """Moves from the pre-fight state to the battle state."""

        self.state = State.BATTLE

    def game_over(self):
        """The player has died."""
        self.playing = False

    def save_data(self):
        """Saves the player, world and game state data."""

        try:
            with open("savegame.txt", "wb") as save_game:
                data = (self.player, self.world_map, self.state)
                pickle.dump(data, save_game)
                self.event_queue.add_event({"message": "GAME SAVED."})
        except Exception as e:
            raise e

    def load_data(self):
        """Loads the player, world and game state data."""

        try:
            with open("savegame.txt", "rb") as save_game:
                data = pickle.load(save_game)
                self.player = data[0]
                self.world_map = data[1]
                self.state = data[2]
        except Exception as e:
            raise e

    def explore(self):
        """Reverts back to the explore state."""

        self.state = State.EXPLORE

    def do_action(self, action):
        """Executes the player's command when in the explore state."""

        command = list(action.keys())[0]

        if command == "verb":
            verb = action.get("verb")
            if verb:
                self.explore_actions[verb](action)
        elif command == "quit":
            self.playing = False
        elif command in ["stats", "stat"]:
            self.state = State.CHAR_SCREEN
        elif command in ["j", "log", "journal"]:
            self.state = State.JOURNAL
        elif command in ["inventory"]:
            self.state = State.INVENTORY
        elif command in ["save"]:
            self.save_data()

    def validate_move(self, move, known_moves):
        """Checks whether the specified move is in known_moves.

        'move' is single character.
        """

        if len(move) > 1:
            return False

        if ord(move) - 97 in known_moves:
            return True
        else:
            return False

    def fight(self, action):
        """Execute combatant moves for this round of combat."""

        if action.get("command"):
            move = action.get("command")
            if self.validate_move(move, self.player.learnt_moves):
                enemy_move = self.enemy.choose_move()
                self.combatants = sorted([self.player, self.enemy],
                                         key=lambda entity: entity.speed)[::-1]
                # loop through combatants and conduct moves.
                for combatant in self.combatants:
                    if isinstance(combatant, Player):
                        move_index = self.player.moves[ord(move) - 97]
                        event_params = (self.player, self.enemy, move_index)
                        self.event_queue.add_event({"attack": event_params})
                        continue
                    if isinstance(combatant, Mob):
                        event_params = (self.enemy, self.player, enemy_move)
                        self.event_queue.add_event({"attack": event_params})
                        continue

    def level_up(self):
        """Level up the player."""

        lvl = self.player.level
        while self.player.xp >= self.player.next_xp:
            self.player.xp -= self.player.next_xp
            self.player.level += 1
            self.player.next_xp = 110 * lvl + 50 * (lvl - 1)
        msg = ("Congratulations! You advanced to {}level {}{}".format(
            c.yellow, self.player.level, c.end))
        self.event_queue.add_event({"message": msg})

    def check_level_up(self):
        """Check whether the player has reached the next level."""

        if self.player.xp >= self.player.next_xp:
            return True

    def xp_gain(self, event):
        """Gives xp to the player."""

        xp = event.get("xp")
        self.player.xp += xp
        msg = "You gained {}{}{} xp.".format(c.yellow, xp, c.end)
        self.event_queue.add_event({"message": msg})
        if self.check_level_up():
            return self.level_up()

    def money_gain(self, event):
        """Gives money to the player."""

        money = event.get("money")
        self.player.wallet += money
        msg = ("You received {}{}{} coins as a reward.".format(
            c.yellow, money, c.end))
        self.event_queue.add_event({"message": msg})

    def on_enemy_death(self):
        """Handles enemy death. Gives xp and money, and drops items."""
        room = self.world_map.get_current_room()
        room.container += self.enemy.container

        self.event_queue.inject({"xp": self.enemy.xp})

        money = randrange(self.enemy.money[0], self.enemy.money[1])
        self.player.wallet += money

        if money == -1:
            msg = (
                "I'm not sure the {}{}{} is going to have any money on it...".
                format(c.red, self.enemy.name, c.end))
            self.event_queue.add_event({"message": msg})
        elif money == 0:
            msg = ("You loot the {}{}{} and find nothing!".format(
                c.red, self.enemy.name, c.end))
            self.event_queue.add_event({"message": msg})
        elif money == 1:
            msg = ("You loot the {}{}{} and find {}{}{} coin.".format(
                c.red, self.enemy.name, c.end, c.yellow, money, c.end))
            self.event_queue.add_event({"message": msg})
        elif money > 1:
            msg = ("You loot the {}{}{} and find {}{}{} coins.".format(
                c.red, self.enemy.name, c.end, c.yellow, money, c.end))
            self.event_queue.add_event({"message": msg})

    def update_quests(self, message):
        """Moves a completed quest from current to completed list."""

        id = message.get("quest")
        self.player.quests.remove(id)
        self.player.completed_quests.append(id)

    def progress_quests(self, message):
        """Tracks the progress of active quests."""

        entity = message.get("dead")
        npc = message.get("meeting")
        item = message.get("taken")
        for id in self.player.quests:
            quest = self.world_map.quests[id]
            for objective in quest.objectives:
                if objective["quest_type"] == "KILL" and entity:
                    if objective["target"] == entity._id:
                        objective["progress"] += 1
                if objective["quest_type"] == "FIND" and npc:
                    if npc._id == objective["target"]:
                        objective["progress"] += 1
                if objective["quest_type"] == "COLLECT" and item:
                    if item._id == objective["target"]:
                        objective["progress"] += 1

    def reveal_exit(self, message):
        """Reveals hidden exits."""

        exit_update = message.get("exit")
        room = self.world_map.rooms[exit_update[0]]
        room.exits[exit_update[1]] = exit_update[2]
        msg = ("A new exit has been revealed in the {}{}{}.".format(
            c.blue, room.name, c.end))
        self.event_queue.add_event({"message": msg})

    def learn_skill(self, message):
        """Learns a new skill."""

        skill_info = message.get("skill")

        player_skill = getattr(self.player, skill_info[0])
        player_skill.append(skill_info[1])

        system = self.get_crafting_system(skill_info[0])
        skill_learnt = system.recipe_names[skill_info[1]]

        msg = ("You have learnt how to make {}{}{}.".format(
            c.yellow, skill_learnt, c.end))
        self.event_queue.add_event({"message": msg})

    def change_state(self, new_state):
        """Changes state as requested by an external object."""
        self.state = new_state.get("state")

    def start_game(self, action):
        """Starts up the game and determines whether to start anew or load."""
        if action.get("command") == "new_game":
            self.playing = True
        elif action.get("command") == "load_game":
            self.playing = True
            self.load_data()
        else:
            pass

    def smelt(self, action):
        """Begins the smelting crafting system."""

        if action.get("command"):
            if ord(action.get("command")) - 97 in self.player.smelting:
                event_params = (action.get("command"), self.player)
                self.event_queue.inject({"smelt": event_params})
            else:
                msg = "You haven't learnt that yet..."
                self.event_queue.add_event({"message": msg})
        else:
            self.state = State.EXPLORE

    def smith(self, action):
        """Begins the blacksmithing crafting system."""

        if action.get("command"):
            if ord(action.get("command")) - 97 in self.player.blacksmithing:
                event_params = (action.get("command"), self.player)
                self.event_queue.inject({"smith": event_params})
        else:
            self.state = State.EXPLORE

    def brew(self, action):
        """Begins the brewing crafting system."""

        if action.get("command"):
            if ord(action.get("command")) - 97 in self.player.brewing:
                event_params = (action.get("command"), self.player)
                self.event_queue.inject({"brew": event_params})
        else:
            self.state = State.EXPLORE

    def handle_input(self):
        """Handles input across all states."""

        action = self.input_handler.prompt(self.state)

        if isinstance(action, Exception):
            self.event_queue.add_event({"message": str(action)})
            return

        if self.state == State.EXPLORE:
            self.do_action(action)
        elif self.state == State.BATTLE:
            self.fight(action)
        elif self.state == State.LOOT:
            self.loot()
        elif self.state == State.PRE_BATTLE:
            self.pre_fight()
        elif self.state == State.GAME_OVER:
            self.game_over()
        elif self.state == State.CHAR_SCREEN:
            self.explore()
        elif self.state == State.DIALOG:
            self.choose(action)
        elif self.state == State.REWARD:
            self.explore()
        elif self.state == State.JOURNAL:
            self.explore()
        elif self.state == State.INVENTORY:
            self.explore()
        elif self.state == State.SLEEP:
            self.explore()
        elif self.state == State.START:
            self.start_game(action)
        elif self.state == State.CUTSCENE:
            self.explore()
        elif self.state == State.FURNACE:
            self.smelt(action)
        elif self.state == State.ANVIL:
            self.smith(action)

    def update(self):
        """Updates the game engine and other systems each turn."""

        self.combat_system.update(self.combatants)
        self.world_map.update()
        self.event_queue.processs_queue()

    def draw(self):
        """Passes all relevant information to the renderer to be 'drawn'."""

        self.renderer.draw(self.state,
                           player=self.player,
                           world=self.world_map,
                           messages=self.message_log.messages,
                           moved=self.moved,
                           enemy=self.enemy,
                           combat_system=self.combat_system,
                           npc=self.npc,
                           crafting_systems=self.crafting_systems)
        self.message_log.clear()

    def receive(self, message):
        """Handles the actioning of received events."""

        if message.get("xp"):
            self.xp_gain(message)
        elif message.get("dead"):
            if not isinstance(message.get("dead"), Player):
                self.on_enemy_death()
                self.progress_quests(message)
                self.enemy = None
                self.state = State.LOOT
        elif message.get("meeting"):
            self.progress_quests(message)
        elif message.get("taken"):
            self.progress_quests(message)
        elif message.get("game over"):
            self.state = State.GAME_OVER
        elif message.get("money"):
            self.money_gain(message)
        elif list(message.keys())[0] == "quest":
            self.update_quests(message)
        elif message.get("exit"):
            self.reveal_exit(message)
        elif message.get("skill"):
            self.learn_skill(message)
        elif message.get("state"):
            self.change_state(message)

    def begin(self):
        """Begins the game and initalises/loads all systems and data."""

        self.state = State.START
        self.init_systems()
        self.init_game_data()
        self.create_player()
        self.renderer.clear()
        self.draw()
        while (not self.playing):
            self.handle_input()
            if self.playing:
                break
            self.draw()
        # check whether a save file has been loaded
        if self.state == State.START:
            self.transition(0, 0)  # move into the game world.

    def run(self):
        """The main game loop."""

        self.begin()
        self.update()
        self.draw()

        while (self.playing):
            self.moved = False
            self.handle_input()
            self.update()
            self.draw()
Exemple #32
0
 def test_attack_when_one_repeated(self):
     self.attacker = DictionaryAttacker(FREQUENCIES, Dictionary(["foo"]))
     self.assertEqual(self.attacker.attack(self.cipher.encrypt("foo foo fo")), "foo foo fo")
Exemple #33
0
def convert_dic(dic, lang):
    id2word, word2id = {}, {}
    for i in range(len(dic)):
        id2word[i] = dic[i][0]
        word2id[dic[i][0]] = i
    return Dictionary(id2word, word2id, lang)
Exemple #34
0
from Crypto.Cipher import AES
from src.dictionary import Dictionary
from time import time

current_time_ms = lambda: int(round(time() * 1000))

d = Dictionary()
mode = input(
    "select mode e (encrypt), r (encrypt with random key), d (decrypt) or c (crack)?: \n"
)

key_string = ""
if mode == "e" or mode == "d":
    key_string = input("enter key: \n")
elif mode == "r":
    key_string = d.random_word()
    print(f"{key_string} was chosen as the random key")
    numbers = input("salt key (y / n)?\n")
    if numbers == "y":
        key_string = key_string.replace("o", "0")
        key_string = key_string.replace("i", "1")
        key_string = key_string.replace("e", "3")
        key_string = key_string.replace("s", "5")
        print(f"key looks now like this: {key_string}")
    mode = "e"
elif mode == "c":
    print("craking mode selected, no key input is required")

while len(key_string) % 16 != 0:
    key_string += " "