Пример #1
0
 def predictKTactics(self, in_data : TacticContext, k : int) -> List[Prediction]:
     if len(in_data.hypotheses) == 0:
         return [Prediction("eauto", 0)]
     k = min(k, len(in_data.hypotheses))
     best_hyps = sorted(in_data.hypotheses, key=len, reverse=True)[:k]
     return [Prediction("apply " + serapi_instance.get_first_var_in_hyp(hyp) + ".",
                        .5 ** idx) for idx, hyp in enumerate(best_hyps)]
Пример #2
0
 def add_arg(self, tactic_stem : str, goal : str, hyps : List[str], max_length : int):
     if serapi_instance.tacticTakesHypArgs(tactic_stem):
         return tactic_stem + " " + \
             serapi_instance.get_first_var_in_hyp(get_closest_hyp(hyps, goal,
                                                                  max_length)) \
             + "."
     else:
         return tactic_stem + "."
Пример #3
0
 def predictKTactics(self, in_data : TacticContext, k : int) -> List[Prediction]:
     if len(in_data.hypotheses) == 0:
         return [Prediction("eauto", 0)]
     k = min(k, len(in_data.hypotheses))
     best_hyps = \
         sorted(in_data.hypotheses,
                reverse=True,
                key=lambda hyp:
                SequenceMatcher(None, serapi_instance.get_hyp_type(hyp),
                                in_data.goal).ratio()
         )[:k]
     return [Prediction("apply " + serapi_instance.get_first_var_in_hyp(hyp) + ".",
                        .5 ** idx) for idx, hyp in enumerate(best_hyps)]
Пример #4
0
 def add_args(self, stem_predictions : List[Prediction],
              goal : str, hyps : List[str], max_length : int):
     possibilities : List[Prediction] = []
     for stem, stem_score in stem_predictions:
         if serapi_instance.tacticTakesHypArgs(stem):
             for hyp, hyp_score in get_closest_hyps(hyps, goal,
                                                    len(stem_predictions),
                                                    max_length):
                 possibilities.append(
                     Prediction(stem + " " +
                                serapi_instance.get_first_var_in_hyp(hyp) + ".",
                                hyp_score * stem_score))
         else:
             possibilities.append(Prediction(stem + ".", stem_score * 0.5))
     return list(sorted(possibilities, key=lambda pred: pred.certainty,
                        reverse=True)[:len(stem_predictions)])
Пример #5
0
 def _predictDistribution(self, in_data : TacticContext) -> \
     Tuple[torch.FloatTensor, str]:
     if len(in_data.hypotheses) > 0:
         relevant_hyp, relevance = \
             max([(hyp,
                   term_relevance(in_data.goal,
                                        serapi_instance.get_hyp_type(hyp)))
                  for hyp in in_data.hypotheses], key=lambda x: x[1])
     else:
         relevant_hyp = ":"
         relevance = 0
     encoded_hyp = self._encode_term(serapi_instance.get_hyp_type(relevant_hyp))
     encoded_relevance = [relevance]
     encoded_goal = self._encode_term(in_data.goal)
     stem_distribution = self._run_model(encoded_hyp, encoded_relevance, encoded_goal)
     return FloatTensor(stem_distribution), \
         serapi_instance.get_first_var_in_hyp(relevant_hyp)
Пример #6
0
 def predictKTactics(self, in_data: TacticContext,
                     k: int) -> List[Prediction]:
     if len(in_data.hypotheses) == 0:
         return [Prediction("eauto", 0)]
     with self._lock:
         distribution = self._predictDistribution(in_data)
         if k > len(in_data.hypotheses):
             k = len(in_data.hypotheses)
         probs, indices = distribution.squeeze().topk(k)
         if k == 1:
             probs = FloatTensor([probs])
             indices = LongTensor([indices])
     return [
         Prediction(
             "apply " + serapi_instance.get_first_var_in_hyp(
                 in_data.hypotheses[idx.item()]) + ".",
             math.exp(certainty.item()))
         for certainty, idx in zip(probs, indices)
     ]
Пример #7
0
def decode_tactic_structure(stem_embedding : SimpleEmbedding,
                            struct : TacticStructure, hyps : List[str]) -> str:
    stem_idx, arg_hyp_idxs = struct
    return " ".join([stem_embedding.decode_token(stem_idx)] +
                    [serapi_instance.get_first_var_in_hyp(hyps[hyp_idx-TOKEN_START])
                     for hyp_idx in arg_hyp_idxs])
Пример #8
0
 def get_var(idx: int) -> str:
     if idx == 0:
         return "UNKNOWN"
     else:
         return serapi_instance.get_first_var_in_hyp(hyps[idx - 1])