Highlight text in java

We are developing a plagiarism detection framework. There I must highlight possible plagiaristic phrases in the document. First, the document is pre-processed by deleting the stop word, output and deleting the number. Thus, backlighting becomes complicated with a pre-processed token. Like an example:

Orginal Text: โ€œExtreme programming is one of the agile software development approaches that emphasizes frequent releases in short development cycles, called timelines. This reduces the cost of change by a few short development cycles rather than one long. Extreme programming includes pair programming (for code verification, unit testing). It also avoids the implementation of functions that are not included in the current field time, so minimizing the schedule can be minimized. "

Phrase

wants to highlight: Extreme programming involves programming with a couple

pre-processed token: Extrem software program

In any case, can I highlight the pre-processed token in the source document ????

Thanx

+3
source share
3 answers

Better use a JTextPane or JEditorPane instead of a JTextArea .

The text area is a โ€œsimpleโ€ text component, which means taht, although it can display text in any font, all text has the same font.

So, JTextArea not a convenient component for formatting text.

Conversely, using JTextPane or JEditorPane , itโ€™s quite easy to change the style (selection) of any part of the loaded text.

For more information, see How to Use Editing Panels and Text Panels .

Update:

The following code highlights the right part of your text. This is not what you want. He simply finds the exact phrase in the text.

But I hope that if you apply your algorithms, you can easily change it to suit 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); } } } 

This example is based on Highlighting words in a JTextComponent .

+3
source

From a technical point of view: you can select or develop a markup language and add annotations or tags to the source document. Or do you want to create a second file that records all possible plagiarism.

With markup, your text might look like this:

 [...] rather than one long one. <plag ref="1234">Extreme programming includes pair-wise programming</plag> (for code review, unit testing). [...] 

(ref link refers to some metadata record that describes the original)

+1
source

You can use java.text.AttributedString to annotate pre-processed tokens in the source document. Then apply the TextAttributes to the appropriate ones (which will act in the original document.

+1
source

All Articles