示例#1
0
                def handle_projections(exclude_key: Optional[str] = None) -> bool:
                    if blank_idx is None or len(project_idx) == 0:
                        return False

                    projections = []
                    blank_pos = self.sentence_structures[sentence_idx][blank_idx].pos
                    for word_idx in project_idx:
                        projecting_word = self.sentence_generations[sentence_idx][word_idx]
                        projection = projecting_word.project(word_idx, sentence_length, blank_pos,
                                                             exclude_key=exclude_key)
                        projections.append(projection)

                    # Concatenate all projections and create p-value matrix
                    projection_collection = MarkovWordProjectionCollection(projections)
                    if len(projection_collection) == 0:
                        return False

                    all_p_values = projection_collection.probability_matrix()

                    # We just want the p-values for the blank word
                    p_values = all_p_values[:, blank_idx]

                    # Choose an index based on the probability
                    choices = np.arange(len(projection_collection))

                    word_choice_idx = temp(p_values, temperature=MARKOV_MODEL_TEMPERATURE)

                    # Select the word from the database and assign it to the blank space
                    select_word = projection_collection.keys[word_choice_idx]
                    word = GeneratedWord.from_markov_word(db.select(select_word),
                                                          self.sentence_structures[sentence_idx][blank_idx].mode)
                    self.sentence_generations[sentence_idx][blank_idx] = word
示例#2
0
    def predict(self, num_sentences: int) -> List[PoSCapitalizationMode]:
        from keras.preprocessing.sequence import pad_sequences

        predictions = []

        # Start the sequence with NONE / NONE
        sequence = [[0]]

        eos_count = 0

        while eos_count < num_sentences:
            padded_sequence = pad_sequences(sequence, maxlen=StructureModel.SEQUENCE_LENGTH, padding='post')

            prediction = self.model.predict(padded_sequence, batch_size=1)[0]

            index = temp(prediction, STRUCTURE_MODEL_TEMPERATURE)

            if PoSCapitalizationMode.from_embedding(index).pos == Pos.EOS:
                eos_count += 1

            predictions.append(index)
            sequence[0].append(index)
            sequence[0] = sequence[0][-StructureModel.SEQUENCE_LENGTH:]

        modes = []
        for embedding_idx, embedding in enumerate(predictions):
            mode = PoSCapitalizationMode.from_embedding(embedding)
            modes.append(mode)
        return modes
示例#3
0
    def predict(self, num_sentences: int) -> List[PoSCapitalizationMode]:
        from keras.preprocessing.sequence import pad_sequences

        predictions = []

        # Start the sequence with NONE / NONE
        sequence = [[0]]

        eos_count = 0

        while eos_count < num_sentences:
            padded_sequence = pad_sequences(sequence, maxlen=StructureModel.SEQUENCE_LENGTH, padding='post')

            prediction = self.model.predict(padded_sequence, batch_size=1)[0]

            index = temp(prediction, STRUCTURE_MODEL_TEMPERATURE)

            if PoSCapitalizationMode.from_embedding(index).pos == Pos.EOS:
                eos_count += 1

            predictions.append(index)
            sequence[0].append(index)
            sequence[0] = sequence[0][-StructureModel.SEQUENCE_LENGTH:]

        modes = []
        for embedding_idx, embedding in enumerate(predictions):
            mode = PoSCapitalizationMode.from_embedding(embedding)
            modes.append(mode)
        return modes
                def handle_projections(exclude_key: Optional[str] = None) -> bool:
                    if blank_idx is None or len(project_idx) == 0:
                        return False

                    projections = []
                    blank_pos = self.sentence_structures[sentence_idx][blank_idx].pos
                    for word_idx in project_idx:
                        projecting_word = self.sentence_generations[sentence_idx][word_idx]
                        projection = projecting_word.project(word_idx, sentence_length, blank_pos,
                                                             exclude_key=exclude_key)
                        projections.append(projection)

                    # Concatenate all projections and create p-value matrix
                    projection_collection = MarkovWordProjectionCollection(projections)
                    if len(projection_collection) == 0:
                        return False

                    all_p_values = projection_collection.probability_matrix()

                    # We just want the p-values for the blank word
                    p_values = all_p_values[:, blank_idx]

                    # Choose an index based on the probability
                    choices = np.arange(len(projection_collection))

                    word_choice_idx = temp(p_values, temperature=MARKOV_MODEL_TEMPERATURE)

                    # Select the word from the database and assign it to the blank space
                    select_word = projection_collection.keys[word_choice_idx]
                    word = GeneratedWord.from_markov_word(db.select(select_word),
                                                          self.sentence_structures[sentence_idx][blank_idx].mode)
                    self.sentence_generations[sentence_idx][blank_idx] = word