指纹比对算法
package com.example.springbootdemo.controller;
import java.io.*;
import java.util.*;
public class CodeDuplicationChecker3 {
public static void main(String[] args) {
String dir1 = "E:\\fp\\Folder1";
String dir2 = "E:\\fp\\Folder2";
File folder1 = new File(dir1);
File folder2 = new File(dir2);
if (!folder1.isDirectory() || !folder2.isDirectory()) {
System.out.println("Error: Both arguments must be valid directories.");
return;
}
Map<String, Set<String>> wordsMap1 = getFileWordsMap(folder1);
Map<String, Set<String>> wordsMap2 = getFileWordsMap(folder2);
Set<String> uniqueWords = new HashSet<>(wordsMap1.keySet());
uniqueWords.addAll(wordsMap2.keySet());
int totalUniqueWords = uniqueWords.size();
List<String> duplicatedWords = findDuplicatedWords(wordsMap1, wordsMap2);
System.out.println("Total unique words compared: " + totalUniqueWords);
System.out.println("Duplicated words found: " + duplicatedWords.size());
double duplicationRate = ((double) duplicatedWords.size() / totalUniqueWords) * 100;
System.out.println("Code duplication rate: " + duplicationRate + "%");
for (String word : duplicatedWords) {
System.out.println("Duplicated word: " + word);
Set<String> files1 = wordsMap1.getOrDefault(word, new HashSet<>());
Set<String> files2 = wordsMap2.getOrDefault(word, new HashSet<>());
System.out.println("Files in folder 1: " + files1);
System.out.println("Files in folder 2: " + files2);
System.out.println("---------------------------------------");
}
}
private static Map<String, Set<String>> getFileWordsMap(File folder) {
Map<String, Set<String>> wordsMap = new HashMap<>();
addFilesToWordsMap(folder, wordsMap);
return wordsMap;
}
private static void addFilesToWordsMap(File folder, Map<String, Set<String>> wordsMap) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
addFilesToWordsMap(file, wordsMap);
} else if (file.isFile() && file.getName().endsWith(".java")) {
try {
Set<String> wordsSet = getFileWordsSet(file);
for (String word : wordsSet) {
Set<String> filesSet = wordsMap.computeIfAbsent(word, k -> new HashSet<>());
filesSet.add(file.getAbsolutePath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private static Set<String> getFileWordsSet(File file) throws IOException {
Set<String> wordsSet = new HashSet<>();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] words = line.trim().split("[\\s\\p{Punct}]+");
wordsSet.addAll(Arrays.asList(words));
}
reader.close();
return wordsSet;
}
private static List<String> findDuplicatedWords(Map<String, Set<String>> wordsMap1, Map<String, Set<String>> wordsMap2) {
List<String> duplicatedWords = new ArrayList<>();
for (String word : wordsMap1.keySet()) {
if (wordsMap2.containsKey(word)) {
duplicatedWords.add(word);
}
}
return duplicatedWords;
}
}