首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用java突出显示文本

用java突出显示文本
EN

Stack Overflow用户
提问于 2011-06-30 04:58:34
回答 3查看 17.1K关注 0票数 6

我们正在开发一个剽窃检测框架。在这里,我必须突出说明文件中可能抄袭的短语。首先对文档进行停止字删除、词干处理和数字删除处理。因此,对于预处理的令牌,高亮显示变得很困难,例如:

原文:“极限编程是敏捷软件开发的一种方法,它强调在短的开发周期(被称为时间框)中频繁发布。这将通过多个短的开发周期而不是一个长的开发周期来降低更改的成本。极限编程包括成对的编程(用于代码评审、单元测试)。它还避免了实现不包含在当前时间框中的特性,从而可以最小化计划的爬行。”

想突出显示的短语:极限编程包括成对编程

预处理令牌: Extrem程序成对程序

我是否可以在原始文档中高亮显示预处理的令牌?

谢谢

EN

回答 3

Stack Overflow用户

发布于 2011-06-30 08:01:25

你最好使用JTextPaneJEditorPane,而不是JTextArea

文本区域是一个“纯文本”组件,意思是taht,尽管它可以以任何字体显示文本,但所有文本都是相同的字体。

因此,JTextArea不是制作任何文本格式的方便组件。

相反,使用JTextPaneJEditorPane可以很容易地更改加载文本的任何部分的样式(突出显示)。

详情请参见如何使用编辑器窗格和文本窗格

更新:

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

但我希望,如果您应用您的算法,您可以轻松地修改它,以满足您的需要。

代码语言:javascript
运行
复制
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);
        }
    }
}

此示例基于高亮显示JTextComponent中的单词

票数 4
EN

Stack Overflow用户

发布于 2011-06-30 05:09:25

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

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

代码语言:javascript
运行
复制
[...] rather than one long one. <plag ref="1234">Extreme programming 
includes pair-wise programming</plag> (for code review, unit testing). [...]

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

票数 1
EN

Stack Overflow用户

发布于 2011-06-30 06:14:17

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6530105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档