Esempio n. 1
0
def find_mention_head(mention: CementSpan,
                      predictor: Predictor) -> Optional[int]:
    tokens = mention.get_tokens()
    predicted_results = predictor.predict(sentence=tokens)
    # sanity check
    if (
            len(tokens) != len(predicted_results['words'])
            or not all([a == b for a, b in zip(tokens, predicted_results['words'])])
    ):
        logger.warning(f'Tokenizations do not match: {tokens} - {predicted_results["words"]}')
        return None

    for i in range(len(tokens)):
        if predicted_results['predicted_dependencies'][i] == 'root':
            logger.info(f'Mention: {mention} - has head: {predicted_results["words"][i]}')
            return i
    return 0
Esempio n. 2
0
def predict_outputs(model: Model,
                    test_data: List[Dict[str, str]],
                    predictor: Predictor,
                    tgt_attr: str) -> List[Dict[str, str]]:
    assert tgt_attr in ["positive", "negative"]
    outputs = []
    for datum in test_data:
        predictions = predictor.predict(datum["source"], tgt_attr)['predictions']
        final_predictions = predictions
        if any(isinstance(el, list) for el in predictions):
            final_predictions = predictions[0]
        tokens = [model.vocab.get_token_from_index(i, 'tokens') for i in final_predictions]
        predicted_sentence = " ".join(tokens)
        evaluation = {
            "input": datum["source"],
            "prediction": predicted_sentence,
            "gold": datum["target"]
        }
        outputs.append(evaluation)

    log_predictions(outputs)
    return outputs
Esempio n. 3
0
    def predict(
        self,
        premise='',
        hypothesis='',
    ):
        cache_key = '{}->{}'.format(premise, hypothesis)
        if cache_key in self._cache:
            return self._cache[cache_key]['label_probs']

        result = self.predictor.predict(
            premise=premise,
            hypothesis=hypothesis,
        )
        # [entailment, contradiction, neutral]
        self._cache[cache_key] = result
        self.save()
        return result['label_probs']

    def save(self):
        pickle.dump(self._cache, open(self.cache_path, "wb"))


if __name__ == '__main__':
    predictor = Predictor()
    print(
        predictor.predict(
            hypothesis=
            'The information is straight-forward, and probably easy to verify.',
            premise='The sentence provides coherent, verifiable information.'))