突出显示java中的文本

突出显示java中的文本,java,swing,indexing,highlighting,jtextarea,Java,Swing,Indexing,Highlighting,Jtextarea,我们正在开发一个剽窃检测框架。在这里,我必须强调文件中可能存在的剽窃短语。文档首先通过停止字删除、词干分析和数字删除进行预处理。因此,使用预处理的标记进行高亮显示变得很困难 例如: 原文:“极限编程是敏捷软件开发的一种方法,它强调在称为时间盒的短开发周期中频繁发布。这导致通过具有多个较短的开发周期(而不是一个较长的开发周期)来降低变更成本极限编程包括成对编程(用于代码审查、单元测试)。此外,它还避免了实现当前时间框中未包含的功能,因此可以最小化进度爬行。" 想要强调的短语:极限编程包括成对编程

我们正在开发一个剽窃检测框架。在这里,我必须强调文件中可能存在的剽窃短语。文档首先通过停止字删除、词干分析和数字删除进行预处理。因此,使用预处理的标记进行高亮显示变得很困难 例如:

原文:“极限编程是敏捷软件开发的一种方法,它强调在称为时间盒的短开发周期中频繁发布。这导致通过具有多个较短的开发周期(而不是一个较长的开发周期)来降低变更成本极限编程包括成对编程(用于代码审查、单元测试)。此外,它还避免了实现当前时间框中未包含的功能,因此可以最小化进度爬行。"

想要强调的短语:极限编程包括成对编程

预处理令牌:极值程序成对程序

我是否可以在原始文档中突出显示预处理的标记


Thanx从技术角度来看:您可以选择或开发一种标记语言,并在原始文档中添加注释或标记。或者您可以创建第二个文件来记录所有潜在的剽窃行为

使用标记时,文本可能如下所示:

[...] rather than one long one. <plag ref="1234">Extreme programming 
includes pair-wise programming</plag> (for code review, unit testing). [...]
[…]而不是一个长的。极限编程
包括成对编程(用于代码审查、单元测试)。[…]

(ref引用一些描述原始文档的元数据记录)

您可以使用java.text.AttributedString对原始文档中预处理的标记进行注释。 然后将TextAttributes应用于相关的属性(这些属性将在原始文档中生效)。

您最好使用或,而不是

文本区域是“纯”文本组件,这意味着尽管它可以以任何字体显示文本,但所有文本都使用相同的字体

因此,
JTextArea
不是进行任何文本格式化的方便组件

相反,使用
JTextPane
JEditorPane
,很容易更改加载文本任何部分的样式(突出显示)

有关详细信息,请参阅

更新: 以下代码突出显示了文本的所需部分。 这不是你想要的,它只是在文本中找到确切的短语

但我希望如果你运用你的算法,你可以很容易地 修改它以满足您的需要

import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class LineHighlightPainter {

    String revisedText = "Extreme programming is one approach "
            + "of agile software development which emphasizes on frequent"
            + " releases in short development cycles which are called "
            + "time boxes. This result in reducing the costs spend for "
            + "changes, by having multiple short development cycles, "
            + "rather than one long one. Extreme programming includes "
            + "pair-wise programming (for code review, unit testing). "
            + "Also it avoids implementing features which are not included "
            + "in the current time box, so the schedule creep can be minimized. ";
    String token = "Extreme programming includes pair-wise programming";

    public static void main(String args[]) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
                    new LineHighlightPainter().createAndShowGUI();
                }
            });
        } catch (InterruptedException ex) {
            // ignore
        } catch (InvocationTargetException ex) {
            // ignore
        }
    }

    public void createAndShowGUI() {
        JFrame frame = new JFrame("LineHighlightPainter demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea area = new JTextArea(9, 45);
        area.setLineWrap(true);
        area.setWrapStyleWord(true);
        area.setText(revisedText);

        // Highlighting part of the text in the instance of JTextArea
        // based on token.
        highlight(area, token);

        frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    // Creates highlights around all occurrences of pattern in textComp
    public void highlight(JTextComponent textComp, String pattern) {
        // First remove all old highlights
        removeHighlights(textComp);

        try {
            Highlighter hilite = textComp.getHighlighter();
            Document doc = textComp.getDocument();
            String text = doc.getText(0, doc.getLength());

            int pos = 0;
            // Search for pattern
            while ((pos = text.indexOf(pattern, pos)) >= 0) {
                // Create highlighter using private painter and apply around pattern
                hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
                pos += pattern.length();
            }

        } catch (BadLocationException e) {
        }
    }

    // Removes only our private highlights
    public void removeHighlights(JTextComponent textComp) {
        Highlighter hilite = textComp.getHighlighter();
        Highlighter.Highlight[] hilites = hilite.getHighlights();

        for (int i = 0; i < hilites.length; i++) {
            if (hilites[i].getPainter() instanceof MyHighlightPainter) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }
    // An instance of the private subclass of the default highlight painter
    Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);

    // A private subclass of the default highlight painter
    class MyHighlightPainter
            extends DefaultHighlighter.DefaultHighlightPainter {

        public MyHighlightPainter(Color color) {
            super(color);
        }
    }
}
import java.lang.reflect.InvocationTargetException;
导入javax.swing.*;
导入javax.swing.text.*;
导入java.awt.*;
公共级线条高亮画家{
String ReviedText=“极限编程是一种方法”
+“强调频繁开发的敏捷软件开发”
+“在短开发周期内发布,称为”
+“时间框。这将减少花费在以下方面的成本”
+通过具有多个短开发周期进行更改
+“而不是一个长的。极限编程包括”
+成对编程(用于代码检查、单元测试)
+“此外,它还避免实现未包含的功能”
+“在当前时间框中,因此可以最小化计划的爬行。”;
String token=“极限编程包括成对编程”;
公共静态void main(字符串参数[]){
试一试{
SwingUtilities.invokeAndWait(新的Runnable(){
公开募捐{
新建LineHighlightPainter().createAndShowGUI();
}
});
}捕获(中断异常例外){
//忽略
}捕获(调用TargetException ex){
//忽略
}
}
public void createAndShowGUI(){
JFrame=新JFrame(“LineHighlightPainter演示”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea=新的JTextArea(9,45);
区域。setLineWrap(真);
area.setWrapStyleWord(true);
area.setText(修订文本);
//突出显示JTextArea实例中的部分文本
//基于令牌。
突出显示(区域、标记);
frame.getContentPane().add(新的JScrollPane(区域),BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
//在textComp中所有出现的图案周围创建高光
公共空白突出显示(JTextComponent textComp,字符串模式){
//首先删除所有旧的高光
移除突出显示(textComp);
试一试{
Hilliter hilite=textComp.getHighlighter();
Document doc=textComp.getDocument();
String text=doc.getText(0,doc.getLength());
int pos=0;
//搜索模式
而((pos=text.indexOf(pattern,pos))>=0){
//使用私人画师创建荧光笔并在图案周围应用
hilite.addHighlight(pos,pos+pattern.length(),myHighlightPainter);
pos+=pattern.length();
}
}捕获(错误位置异常e){
}
}
//只删除我们的私人高光
公共void removeHighlights(JTextComponent textComp){
Hilliter hilite=textComp.getHighlighter();
hilliter.Highlight[]hilites=hilite.getHighlights();
for(int i=0;i