public void Setup()
 {
     _settings = new PlagiarismSettings()
     {
         Threshold      = 0.6,
         Configurations = new List <ComparisonConfig>
         {
             new() { SamplingWindow = 6, KGramLength = 3 },
             new() { SamplingWindow = 8, KGramLength = 5 }
         }
     };
     _hashService        = new HashService();
     _plagiarismService  = new PlagiarismService(_settings, _hashService);
     _functionsToCompare = new List <FunctionAggregate>(new[]
     {
         GetFunctionAggregate(
             "var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider();var factory = serviceProvider.GetService<ILoggerFactory>();"
             )
     });
     _suspectedFunction = GetFunctionAggregate(
         "var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider();");
 }
Example #2
0
        public Task Handle(PlagiarismAnalyzeNotification notification, CancellationToken cancellationToken)
        {
            using var scope = _serviceProvider.CreateScope();
            var codePlagiarismService = scope.ServiceProvider
                                        .GetRequiredService <ICodePlagiarismService>();
            var donePlagiarismService = scope.ServiceProvider
                                        .GetRequiredService <IDispatcher <PlagiarismAnalyzeResponse> >();

            var suspectedFunction = new FunctionAggregate(new FunctionId(notification.SuspectedFunction.Id),
                                                          notification.SuspectedFunction.Code, _hashService.GetHash(notification.SuspectedFunction.Code));
            var comparedFunctions = notification.ComparedFunctions
                                    .Select(f => new FunctionAggregate(new FunctionId(f.Id), f.Code, _hashService.GetHash(f.Code)))
                                    .ToList();
            var functionWithSimilarities =
                codePlagiarismService.AnalyseCode(suspectedFunction, comparedFunctions);
            var similarFunctions = functionWithSimilarities.SimilarFunctions
                                   .Select(f => new FunctionSimilarity(f.Id.Value, f.SimilarityRate, f.Hash))
                                   .ToList();
            var res = new PlagiarismAnalyzeResponse(functionWithSimilarities.Id.Value, functionWithSimilarities.Hash,
                                                    similarFunctions);

            return(donePlagiarismService.Dispatch(res));
        }
        public FunctionAggregate AnalyseCode(FunctionAggregate suspectedFunctionAggregate,
                                             IList <FunctionAggregate> functionsToCompare)
        {
            foreach (var functionToCompare in functionsToCompare)
            {
                if (functionToCompare.Id.Equals(suspectedFunctionAggregate.Id))
                {
                    continue;
                }

                var averageSimilarity =
                    GetAverageSimilarityRate(suspectedFunctionAggregate.Code, functionToCompare.Code);
                if (averageSimilarity > _settings.Threshold)
                {
                    suspectedFunctionAggregate.AddSimilarFunction(
                        functionToCompare.Id,
                        functionToCompare.Hash,
                        averageSimilarity);
                }
            }

            return(suspectedFunctionAggregate);
        }