?? sentence.java
字號:
package com.xjt.nlp.word;/** * <p>Title: Java中文分詞組件</p> * <p>Description: 本組件以中科院ICTCLAS系統為基礎,在其基礎之上改編,本組件僅供學習和研究用途,任何商業用途將自行承擔法律后果,與組件編寫人無關。</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京師范大學</p> * @author 陳天 * @version 1.0 */import java.util.LinkedList;import java.util.List;import java.util.Iterator;public class Sentence { private List sentence = new LinkedList(); public Sentence() { } /** * 得到某個詞匯 * @param index 第幾個詞匯 * @return 詞匯對象 */ public Word getWord(int index){ return (Word)sentence.get(index); } /** * 得到一句話的詞匯數量 * @return 詞匯數量 */ public int totalWords(){ return sentence.size(); } /** * 在一句話的最后增加一個詞 * @param word 詞對象 */ public void addWord(Word word){ sentence.add(word); } /** * 從一句話中去掉某個詞 * @param word */ public void removeWord(Word word){ sentence.remove(word); } /** * 從一句話中去掉某個詞 * @param index */ public void removeWord(int index){ sentence.remove(index); } /** * 合成一個完整的句子,不帶任何詞性標注 * @return */ public String toSentence(){ StringBuffer buffer = new StringBuffer(); for (int i=0;i<sentence.size();i++){ Word word = (Word)sentence.get(i); buffer.append(word.getWord()); } return buffer.toString(); } /** * 合成一個帶詞性標注的句子 * @return */ public String toString(){ StringBuffer buffer = new StringBuffer(); for (int i=0;i<sentence.size();i++){ Word word = (Word)sentence.get(i); buffer.append(word.toString()); } return buffer.toString(); } /** * 將句子中的若干個單詞和并 * @param startIndex 開始要合并的單詞的位置 * @param endIndex 最后一個要合并的單詞的位置 * @param newAttr 合并后的新詞性 */ public void mergeWord(int startIndex, int endIndex, String newAttr){ if (startIndex<0 || endIndex>=sentence.size()) throw new IllegalArgumentException("詞匯合并的開始位置和結束位置不正確"); if (startIndex>=endIndex) throw new IllegalArgumentException("詞匯合并的開始位置不能大于或者等于結束位置"); Word startWord=(Word)sentence.get(startIndex); for (int i=startIndex+1;i<=endIndex;i++){ Word word = (Word) sentence.get(startIndex+1); startWord.setWord(startWord.getWord()+word.getWord()); sentence.remove(startIndex+1); } startWord.setAttribute(newAttr); } public static void main(String[] args) { Sentence sentence=new Sentence(); Word word1 = new Word("中華","n"); Word word2 = new Word("人民","nr"); Word word3 = new Word("共和國","n"); sentence.addWord(word1); sentence.addWord(word2); sentence.addWord(word3); sentence.removeWord(word2); System.out.println(sentence.toSentence()); sentence.mergeWord(0,1,"nz"); System.out.println(sentence.toString()); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -