コード例 #1
0
 /** Replaces the current word token */
 public void replaceWord(String newWord) {
   if (currentWordPos != -1) {
     try {
       /* ORIGINAL
         document.remove(currentWordPos, currentWordEnd - currentWordPos);
         document.insertString(currentWordPos, newWord, null);
       */
       // Howard's Version for Ekit
       Element element =
           ((javax.swing.text.html.HTMLDocument) document).getCharacterElement(currentWordPos);
       AttributeSet attribs = element.getAttributes();
       document.remove(currentWordPos, currentWordEnd - currentWordPos);
       document.insertString(currentWordPos, newWord, attribs);
       // End Howard's Version
       // Need to reset the segment
       document.getText(0, document.getLength(), text);
     } catch (BadLocationException ex) {
       throw new RuntimeException(ex.getMessage());
     }
     // Position after the newly replaced word(s)
     // Position after the newly replaced word(s)
     first = true;
     currentWordPos = getNextWordStart(text, currentWordPos + newWord.length());
     if (currentWordPos != -1) {
       currentWordEnd = getNextWordEnd(text, currentWordPos);
       nextWordPos = getNextWordStart(text, currentWordEnd);
       sentanceIterator.setText(text);
       sentanceIterator.following(currentWordPos);
     } else moreTokens = false;
   }
 }
コード例 #2
0
 public DocumentWordTokenizer(Document document) {
   this.document = document;
   // Create a text segment over the etire document
   text = new Segment();
   sentanceIterator = BreakIterator.getSentenceInstance();
   try {
     document.getText(0, document.getLength(), text);
     sentanceIterator.setText(text);
     currentWordPos = getNextWordStart(text, 0);
     // If the current word pos is -1 then the string was all white space
     if (currentWordPos != -1) {
       currentWordEnd = getNextWordEnd(text, currentWordPos);
       nextWordPos = getNextWordStart(text, currentWordEnd);
     } else {
       moreTokens = false;
     }
   } catch (BadLocationException ex) {
     moreTokens = false;
   }
 }
コード例 #3
0
 /** Returns the next word in the text */
 public String nextWord() {
   if (!first) {
     currentWordPos = nextWordPos;
     currentWordEnd = getNextWordEnd(text, currentWordPos);
     nextWordPos = getNextWordStart(text, currentWordEnd + 1);
     int current = sentanceIterator.current();
     if (current == currentWordPos) startsSentance = true;
     else {
       startsSentance = false;
       if (currentWordEnd > current) sentanceIterator.next();
     }
   }
   // The nextWordPos has already been populated
   String word = null;
   try {
     word = document.getText(currentWordPos, currentWordEnd - currentWordPos);
   } catch (BadLocationException ex) {
     moreTokens = false;
   }
   wordCount++;
   first = false;
   if (nextWordPos == -1) moreTokens = false;
   return word;
 }