2010-06-02 119 views
0

我有'一個字符串例如=「該網站擁有的Java開發者年鑑等。複製所有的例子,並且這些例子直接粘貼到你的應用程序」後,令牌如何合併數組列表元素?

,做一些我想要的字符串例子,我有數組列表等:

ArrayList <token > arl = " "this site holds ", "holds all the examples ", "the examples from The Java Developers", " Copy and paste ") 

「這個網站持有」,我知道位置開始和結束字符串測試:星= 1個端= 3 「包含所有的實施例中」,我知道位置STAT = 3端= 6,我知道position stat = 5 end = 10, 「複製並粘貼」我知道position stat = 14 end = 「The Java Developers的例子」 17,

我們可以看到,arl中的某些元素重疊:「本網站擁有」,「擁有所有示例」,「來自The Java Developers的示例」。

這裏的問題是,我該如何合併overlaping元素recived的ArrayList像

ArrayList的結果=「」這個網站擁有的Java開發人員的所有實例」,‘’複製和粘貼‘’;

這裏我的代碼:但只合並拳頭elecment如果檢查元素overloaping

public ArrayList<TextChunks> finalTextChunks(ArrayList<TextChunks> textchunkswithkeyword) { 
     ArrayList<TextChunks > result = (ArrayList<TextChunks>) textchunkswithkeyword.clone(); 
      //System.out.print(result.size()); 
      int j; 
      for(int i=0;i< result.size() ;i++) { 
       int index = i; 
       if(i+1>=result.size()){ 
        break; 
       } 
       j=i+1; 
       if(result.get(i).checkOverlapingTwoTextchunks(result.get(j))== true) { 
        TextChunks temp = new TextChunks(); 
        temp = handleOverlaping(textchunkswithkeyword.get(i),textchunkswithkeyword.get(j),resultSearchEngine); 
        result.set(i, temp); 
        result.remove(j); 
        i = index; 
        continue; 
      } 
     } 
     return result; 
    } 
} 

感謝avadce

+0

我不知道我明白你在問什麼。你能澄清你的問題嗎?也許通過使用一個看起來不像問題一部分的示例字符串? – jasonmp85 2010-06-02 04:25:55

+0

Sory因爲我的英文很弱,我一直在編輯我的問題,希望你能理解! – tiendv 2010-06-02 04:44:56

回答

2

以下應做到這一點,或者至少說明合併這些塊的想法。基本上我正在摧毀現有的塊並重新創建新的塊。聽起來很可怕,但簡化了很多。我只是將這些單詞存儲在List中並遍歷該單詞列表以構建新的(合併!)塊。

private List<TextChunks> finalTextChunks(List<TextChunks> textchunkswithkeyword) { 

    private List<TextChunks> result = new ArrayList<TextChunk>(); 
    private List<String> wordList = new ArrayList<String>(); 

    // store all words in an arraylist, words are stored at their correct positions, 
    // ignored words from the original text are represented by null entries 
    for (TextChunks chunk : textchunkswithkeyword) { 
    int start = chunk.getStartTextchunks(); 
    List<Token> tokens = chunk.getTokens(); // TODO - implement getTokens() in TextChunks class 
    for (int i = 0; i < tokens.length; i++) { 
     wordList.set(start+i, tokens.get(i).toString()); // TODO - overwrite toString() in Token class 
    } 
    } 

    // recreate the chunks 
    int start = 0; 
    boolean isChunk = false; 
    StringBuilder chunkBuilder; 

    for (int i = 0; i < wordList.size(); i++) { 
    String word = wordList.get(i); 
    if (word == null) { 
     if (isChunk) { 
     // end of chunk detected 
     TextChunk chunk = new TextChunk(chunkBuilder.toString().split(" "), start, i); 
     result.add(chunk); 
     isChunk = false; 
     } else { 
     // do nothing 
     } 
    } else { 
     if (isChunk) { 
     // chunk gets longer by one word 
     chunkBuilder.append(" ").append(word); 
     } else { 
     // new chunk starts here 
     chunkBuilder = new StringBuilder(word); 
     start = i; 
     isChunk = true; 
     } 
    } 
    if (isChunk) { 
    // create and add the last chunk 
    TextChunks chunk = new TextChunk(chunkBuilder.toString(), start, wordList.size()-1); 
    result.add(chunk); 
    } 
    return result; 
} 

(警告 - 絕對沒有測試過,我既沒有一個IDE也不手頭編譯)

編輯

改變了代碼 - 你說,那TextChunk類包含一個令牌(單詞?)數組。這只是三個簡單的修改。

EDIT 2

最後的編輯 - 我部分地適應我的代碼到你的類。你需要做什麼

  1. 實現getTokens()方法TextChunks僅僅返回arrt
  2. 實施TextChunks構造函數的String(用空格隔開的話),開始和結束。您的Token類已經提供了一種靜態方法,用於將令牌字符串中的字符串轉換爲
  3. 覆蓋類Token中的toString()方法,以便僅返回令牌String。
+0

感謝您的幫助,但我認爲,您的方式不能幫助我。 我的應用程序有兩種類: - 類令牌{string}裏 令牌是一個字符串:在這個類有像一些方法: 除去空間,從字符串做arraytoken。 - Class textchunk {array token。 int start,int end} class textchunk擴展標記具有數組標記和兩個值開始和結束。 所以裏面的Textchunk是數組令牌(字符串) – tiendv 2010-06-02 06:45:04

+0

你可以看到我的代碼理解的東西,我不能清楚地解釋 ! – tiendv 2010-06-02 07:22:41

+0

@tiendv - 我看過了,你從我的回答中刪除了你的正面投票。這有點令人沮喪,因爲它花了很長時間瞭解你的問題和你的代碼並提供了一個實用的解決方案。你期望什麼? – 2010-06-02 08:38:24