软件工程作业2

这个作业属于哪个课程 https://edu.cnblogs.com/campus/gdgy/SoftwareEngineeringClassof2023
这个作业要求在哪里 https://www.cnblogs.com/huanghi4833/p/18760121
这个作业的目标 掌握 GitHub 、Git 的基本使用方法, 积累个人编程项目的经验

一、PSP表格

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
计划 50 60
需求分析 20 20
设计 60 50
编码 60 70
代码复审 30 30
单元测试 80 60
性能优化 60 60
报告撰写 60 50
合计 420 400

二、设计方案
1.用户界面 (UI) 设计:
使用Java Swing库创建一个图形用户界面,让用户能够与程序交互。
界面包含输入字段用于显示文件路径,按钮用于选择文件和执行查重操作。

2.文件选择:
提供两个按钮,分别用于让用户选择原文文件和抄袭版文件。
使用JFileChooser组件来弹出文件选择对话框,允许用户从本地文件系统中选择文件。

3.查重逻辑:
当用户点击“查重”按钮时,程序读取两个文件的文本内容。
使用Levenshtein距离算法来计算两个文本之间的差异程度,从而估计相似度。

4.结果显示:
计算出的相似度(重复率)通过对话框显示给用户。

三、核心代码展示
`
import javax.swing.;
import java.io.
;

public class PaperPlagiarismCheckerApp {

private JFrame frame;
private JTextField originalFilePathField;
private JTextField plagiarizedFilePathField;

public static void main(String[] args) {
    SwingUtilities.invokeLater(PaperPlagiarismCheckerApp::new);
}

public PaperPlagiarismCheckerApp() {
    initializeUI();
}

private void initializeUI() {
    frame = new JFrame();
    frame.setBounds(100, 100, 600, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    originalFilePathField = new JTextField();
    originalFilePathField.setBounds(10, 10, 400, 25);
    frame.getContentPane().add(originalFilePathField);

    JButton selectOriginalButton = new JButton("选择原文");
    selectOriginalButton.setBounds(420, 10, 150, 25);
    frame.getContentPane().add(selectOriginalButton);
    selectOriginalButton.addActionListener(e -> selectFile(originalFilePathField));

    plagiarizedFilePathField = new JTextField();
    plagiarizedFilePathField.setBounds(10, 45, 400, 25);
    frame.getContentPane().add(plagiarizedFilePathField);

    JButton selectPlagiarizedButton = new JButton("选择抄袭版");
    selectPlagiarizedButton.setBounds(420, 45, 150, 25);
    frame.getContentPane().add(selectPlagiarizedButton);
    selectPlagiarizedButton.addActionListener(e -> selectFile(plagiarizedFilePathField));

    JButton checkPlagiarismButton = new JButton("查重");
    checkPlagiarismButton.setBounds(250, 80, 100, 25);
    frame.getContentPane().add(checkPlagiarismButton);
    checkPlagiarismButton.addActionListener(e -> checkPlagiarism());

    frame.setVisible(true);
}

private void selectFile(JTextField textField) {
    JFileChooser fileChooser = new JFileChooser();
    int result = fileChooser.showOpenDialog(frame);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        textField.setText(selectedFile.getAbsolutePath());
    }
}

private void checkPlagiarism() {
    String originalFilePath = originalFilePathField.getText();
    String plagiarizedFilePath = plagiarizedFilePathField.getText();

    if (originalFilePath.isEmpty() || plagiarizedFilePath.isEmpty()) {
        JOptionPane.showMessageDialog(frame, "请选择两个文件。", "错误", JOptionPane.ERROR_MESSAGE);
        return;
    }

    double similarity = checkPlagiarismLogic(originalFilePath, plagiarizedFilePath);
    JOptionPane.showMessageDialog(frame, "重复率为: " + similarity + "%", "查重结果", JOptionPane.INFORMATION_MESSAGE);
}

private double checkPlagiarismLogic(String originalFilePath, String plagiarizedFilePath) {
    String originalContent = readFileContent(originalFilePath);
    String plagiarizedContent = readFileContent(plagiarizedFilePath);
    if (originalContent.isEmpty() || plagiarizedContent.isEmpty()) {
        return 0.0;
    }

    int distance = computeLevenshteinDistance(originalContent, plagiarizedContent);
    int maxDistance = Math.max(originalContent.length(), plagiarizedContent.length());

    return (1 - (double) distance / maxDistance) * 100;
}

private String readFileContent(String filePath) {
    StringBuilder contentBuilder = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
        String currentLine;
        while ((currentLine = reader.readLine()) != null) {
            contentBuilder.append(currentLine).append("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return contentBuilder.toString();
}

private int computeLevenshteinDistance(CharSequence str1, CharSequence str2) {
    int[][] distance = new int[str1.length() + 1][str2.length() + 1];

    for (int i = 0; i <= str1.length(); i++)
        distance[i][0] = i;
    for (int j = 1; j <= str2.length(); j++)
        distance[0][j] = j;

    for (int i = 1; i <= str1.length(); i++)
        for (int j = 1; j <= str2.length(); j++)
            distance[i][j] = minimum(
                    distance[i - 1][j] + 1,
                    distance[i][j - 1] + 1,
                    distance[i - 1][j - 1] + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : 1));

    return distance[str1.length()][str2.length()];
}

private int minimum(int a, int b, int c) {
    return Math.min(Math.min(a, b), c);
}

}
`

四、运行结果:

posted @ 2025-03-10 01:09  sscxc  阅读(52)  评论(0)    收藏  举报