From 825f4ea4ca8e09350f4f6a89fa9291bf537226fb Mon Sep 17 00:00:00 2001 From: Felix Kozma Date: Sun, 18 Feb 2024 14:12:21 +0100 Subject: [PATCH] feat: check the procent matched of a function --- src/main/java/CodeCheck/OneFunction.java | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/CodeCheck/OneFunction.java b/src/main/java/CodeCheck/OneFunction.java index 2784c4d..351073d 100644 --- a/src/main/java/CodeCheck/OneFunction.java +++ b/src/main/java/CodeCheck/OneFunction.java @@ -12,7 +12,7 @@ public class OneFunction { public List content = new ArrayList<>(); public int equals(OneFunction obj) { - if (obj.content.size() > 2 && obj.name.equals(name) && obj.removeLogAndCommentsAndMore().equals(removeLogAndCommentsAndMore())) + if (obj.content.size() > 2 && obj.name.equals(name) && obj.removeExtraInformationAndGetString().equals(removeExtraInformationAndGetString())) return 1; if (obj.name.equals(name) && obj.content.size() > 4 && content.size() > 4) return 2; if (obj.name.equals(name)) return 3; @@ -23,13 +23,25 @@ public int equals(OneFunction obj) { public Comparison compare(OneFunction other, LLM llm) { switch (equals(other)) { case 1: return new Comparison(true, "Functions are identical").setFunctions(this, other); - case 2: return new Comparison(false, true, llmComparison(other, llm)).setFunctions(this, other); + case 2: return similar(other, llm); case 3: return new Comparison(false, false, "Empty or closetherby"); default: return new Comparison(); } } + private Comparison similar(OneFunction obj, LLM llm) { + List thisCont = removeExtraInformation(); + List objCont = obj.removeExtraInformation(); + + int matchingLines = objCont.stream().filter(objLine -> thisCont.contains(objLine)).toList().size(); + + if ((matchingLines + 0.0)/objCont.size() > 0.5 || matchingLines > 10) + return new Comparison(false, true, "Matching lines " + matchingLines + " out of " + objCont.size()).setFunctions(this, obj); + else + return new Comparison(false, true, llmComparison(obj, llm)).setFunctions(this, obj); + } + @Override public String toString() { return file + ":" + line + " (" + name + ")"; @@ -57,7 +69,16 @@ private String removeLogAndComments() { return this.content.stream().filter(s -> !s.matches("^\\s*\\/\\/")).filter(s -> s.startsWith("Log")).reduce("", (s, s1) -> s + "\n" + s); } - private String removeLogAndCommentsAndMore() { - return this.content.stream().filter(s -> !s.matches("^\\s*\\/\\/")).filter(s -> s.replace(" ", "").length() < 2).collect(Collectors.joining("\n")); + private String removeExtraInformationAndGetString() { + return String.join("\n", removeExtraInformation()); + } + + private List removeExtraInformation() { + return this.content.stream() + .map(s -> s.replaceAll("(.*)(//.*)+", "$1")) + .filter(s -> !s.matches("^\\s*//")) + .map(s -> s.replace(" ", "")) + .filter(s -> !s.startsWith("Log")) + .filter(s -> !s.isEmpty()).toList(); } }