def train(cls, ptr: ReferencePointer, config: dict, data_service: DataService, **data) -> AnalyzerModel: """ Generate a new model on top of the specified source code. :param ptr: Git repository state pointer. :param config: Configuration of the training of unspecified structure. :param data_service: The channel to the data service in Lookout server to query for \ UASTs, file contents, etc. :param data: Extra data passed into the method. Used by the decorators to simplify \ the data retrieval. :return: Instance of `AnalyzerModel` (`model_type`, to be precise). """ return DummyAnalyzerModel()
def setUpClass(cls): cls.checker = IdTyposAnalyzer( DummyAnalyzerModel(), "", config=dict( model=str(Path(__file__).parent / "sample_corrector.asdf"), confidence_threshold=0.2, n_candidates=3)) cls.identifiers = ["get", "gpt_tokeb"] cls.test_df = pandas.DataFrame( [[0, "get", "get"], [1, "gpt tokeb", "gpt"], [1, "gpt tokeb", "tokeb"]], columns=[IdTyposAnalyzer.default_config["index_column"], Columns.Split, Columns.Token]) cls.suggestions = {1: [("get", 0.9), ("gpt", 0.3)], 2: [("token", 0.98), ("taken", 0.3), ("tokem", 0.01)]} cls.filtered_suggestions = {1: [("get", 0.9)], 2: [("token", 0.98), ("taken", 0.3)]}
def test_dummy_model(self): ptr = ReferencePointer("1", "2", "3") model = DummyAnalyzerModel.generate(FakeAnalyzer, ptr) self.assertEqual(model.name, FakeAnalyzer.name) self.assertEqual(model.version, [FakeAnalyzer.version]) self.assertEqual(model.ptr, ptr) self.assertEqual(model.vendor, "source{d}") self.assertEqual(model.description, "Model bound to fake Lookout analyzer.") buffer = io.BytesIO() model.save(buffer) buffer.seek(0) model2 = model.load(buffer) self.assertEqual(model.ptr, model2.ptr) self.assertEqual(model.name, model2.name) self.assertEqual(model.description, model2.description) self.assertEqual(model.vendor, model2.vendor)
def process_review_event( self, request: ReviewEvent) -> EventResponse: # noqa: D401 """ Callback for review events invoked by EventListener. """ base_ptr = ReferencePointer.from_pb(request.commit_revision.base) head_ptr = ReferencePointer.from_pb(request.commit_revision.head) response = EventResponse() response.analyzer_version = self.version comments = [] for analyzer in self._analyzers: try: mycfg = self._protobuf_struct_to_dict( request.configuration[analyzer.name]) self._log.info("%s config: %s", analyzer.name, mycfg) except (KeyError, ValueError): mycfg = {} self._log.debug("no config was provided for %s", analyzer.name) if analyzer.model_type != DummyAnalyzerModel: model = self._get_model(analyzer, base_ptr.url) if model is None: self._log.info("training: %s", analyzer.name) record_event("%s.train" % analyzer.name, 1) model = analyzer.train(base_ptr, mycfg, self._data_service) self._model_repository.set(self._model_id(analyzer), base_ptr.url, model) else: model = DummyAnalyzerModel() self._log.debug("running %s", analyzer.name) record_event("%s.analyze" % analyzer.name, 1) results = analyzer(model, head_ptr.url, mycfg).analyze(base_ptr, head_ptr, self._data_service) self._log.info("%s: %d comments", analyzer.name, len(results)) record_event("%s.comments" % analyzer.name, len(results)) comments.extend(results) response.comments.extend(comments) return response
def train(cls, ptr: ReferencePointer, config: dict, data_service: DataService, **data) \ -> AnalyzerModel: cls.trained = True return DummyAnalyzerModel()
def fake_review(fr, to): yield from FakeDummyAnalyzerSpy(DummyAnalyzerModel(), "", {}).analyze(fr, to, None)