Example #1
0
 def _test(self):
     y_cur = deepcopy(self.y)
     for i, yi in zip(y_indexes, y_pred):
         y_cur[i] = yi
     code_generator = CodeGenerator(self.feature_extractor)
     pred_vnodes = code_generator.apply_predicted_y(
         self.vnodes, self.vnodes_y, list(range(len(self.vnodes_y))),
         FakeRules(y_cur))
     generated_file = code_generator.generate(pred_vnodes, "local")
     self.assertEqual(generated_file, result_local)
Example #2
0
 def _generate_token_fixes(
     self,
     file: File,
     fe: FeatureExtractor,
     feature_extractor_output,
     bblfsh_stub: "bblfsh.aliases.ProtocolServiceStub",
     rules: Rules,
 ) -> Tuple[List[LineFix], List[VirtualNode], numpy.ndarray, numpy.ndarray]:
     X, y, (vnodes_y, vnodes, vnode_parents,
            node_parents) = feature_extractor_output
     y_pred_pure, rule_winners, new_rules, grouped_quote_predictions = rules.predict(
         X=X, vnodes_y=vnodes_y, vnodes=vnodes, feature_extractor=fe)
     y_pred = rules.fill_missing_predictions(y_pred_pure, y)
     if self.config["uast_break_check"]:
         y, y_pred, vnodes_y, rule_winners, safe_preds = filter_uast_breaking_preds(
             y=y,
             y_pred=y_pred,
             vnodes_y=vnodes_y,
             vnodes=vnodes,
             files={file.path: file},
             feature_extractor=fe,
             stub=bblfsh_stub,
             vnode_parents=vnode_parents,
             node_parents=node_parents,
             rule_winners=rule_winners,
             grouped_quote_predictions=grouped_quote_predictions)
         y_pred_pure = y_pred_pure[safe_preds]
     assert len(y) == len(y_pred)
     assert len(y) == len(rule_winners)
     code_generator = CodeGenerator(fe, skip_errors=True)
     new_vnodes = code_generator.apply_predicted_y(vnodes, vnodes_y,
                                                   rule_winners, new_rules)
     token_fixes = []
     for line_number, line in self._group_line_nodes(
             y, y_pred, vnodes_y, new_vnodes, rule_winners):
         line_ys, line_ys_pred, line_vnodes_y, new_line_vnodes, line_winners = line
         new_code_line = code_generator.generate(
             new_line_vnodes, "local").lstrip("\n").splitlines()[0]
         confidence = self._get_comment_confidence(line_ys, line_ys_pred,
                                                   line_winners, new_rules)
         fixed_vnodes = [
             vnode for vnode in new_line_vnodes
             if hasattr(vnode, "y_old") and vnode.y_old != vnode.y
         ]
         token_fixes.append(
             LineFix(
                 line_number=line_number,  # line number for the comment
                 suggested_code=
                 new_code_line,  # code line suggested by our model
                 fixed_vnodes=fixed_vnodes,  # VirtualNode-s with changed y
                 confidence=
                 confidence,  # overall confidence in the prediction, 0-100
             ))
     return token_fixes, new_vnodes, y_pred_pure, y
Example #3
0
 def _test(self):
     y_cur = deepcopy(self.y)
     for offset, yi in zip(offsets, y_pred):
         i = None
         for i, vnode in enumerate(vnodes_y):  # noqa: B007
             if offset == vnode.start.offset:
                 break
         y_cur[i] = yi
     code_generator = CodeGenerator(self.feature_extractor)
     pred_vnodes = code_generator.apply_predicted_y(
         self.vnodes, self.vnodes_y, list(range(len(self.vnodes_y))),
         FakeRules(y_cur))
     generated_file = code_generator.generate(pred_vnodes)
     self.assertEqual(generated_file, result)
Example #4
0
 def test_reproduction(self):
     for indent in ("local", "global"):
         code_generator = CodeGenerator(self.feature_extractor)
         generated_file = code_generator.generate(self.vnodes, indent)
         self.assertEqual(generated_file, self.file.content.decode("utf-8"))
Example #5
0
 def test_reproduction(self):
     code_generator = CodeGenerator(self.feature_extractor)
     generated_file = code_generator.generate(self.vnodes)
     self.assertEqual(generated_file, self.file.content)