在java中突出显示文本 [英] Highlighting Text in java
问题描述
我们正在开发一个抄袭检测框架。在那里,我必须强调文档中可能存在的抄袭短语。首先对文档进行预处理,然后删除停用词,删除和删除号码。因此使用预处理令牌突出显示变得困难
As和示例:
We are developing a plagiarism detection framework. In there i have to highlight the possible plagiarized phrases in the document. The document gets preprocessed with stop word removal, stemming and number removal first. So the highlighting gets difficult with the preprocessed token As and example:
原始文本:极限编程是敏捷软件开发的一种方法,强调频繁发布在简短的开发周期中称为时间框。这样可以通过多个短的开发周期而不是一个很长的开发周期来降低更改的成本。极限编程包括成对编程(代码审查,单元测试)。它还避免实现当前时间框中未包含的功能,因此可以最小化时间表蠕变。
Orginal Text: "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. "
短语想突出显示: 极限编程包括成对编程
预处理令牌:Extrem程序成对程序
preprocessed token : Extrem program pair-wise program
无论如何我可以在原始文件中突出显示预处理的令牌????
Is there anyway I can highlight the preprocessed token in the original document????
Thanx
推荐答案
Y.你最好使用 JTextPane 或 JEditorPane ,而不是 JTextArea 。
You'd better use JTextPane or JEditorPane, instead of JTextArea.
文本区域是一个普通文本组件,这意味着虽然它可以显示任何字体的文本,但所有文本都是相同的字体。
因此, JTextArea
不是进行任何文本格式化的便捷组件。
So, JTextArea
is not a convenient component to make any text formatting.
关于相反,使用 JTextPane
或 JEditorPane
,可以很容易地改变样式(突出显示)加载文本的任何部分。
On the contrary, using JTextPane
or JEditorPane
, it's quite easy to change style (highlight) of any part of loaded text.
参见如何使用编辑器窗格和文本窗格获取详细信息。
以下代码突出显示了文本的所需部分。
这不是你想要的。它只是在文本中找到了确切的短语。
The following code highlights the desired part of your text. It's not exectly what you want. It simply finds the exact phrase in the text.
但是我希望如果你应用算法,你可以轻松地
修改它以满足你的需要。 / p>
But I hope that if you apply your algorithms, you can easily modify it to fit your needs.
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);
}
}
}
此示例基于< a href =http://www.exampledepot.com/egs/javax.swing.text/style_HiliteWords.html\"rel =nofollow>突出显示JTextComponent中的单词。
这篇关于在java中突出显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!