2012-01-13 11 views
1

のHashMap <ハッシュマップ >に位置インデックスとドキュメントIDを格納します。私がする必要があるのは、すべてのファイル内の各単語の
---
---言葉がこれを行うためには

を通過した各ファイルを見つける---ファイル
内の各単語の位置のインデックスを見つけます。
は、どのように私は、ディレクトリ内のテキストファイルを持っている

HashMap<String, HashMap<Integer, ArrayList<Integer>>> 

上記のような構造を使用します。

String word; 
     String pattern = "[[^\\w\\süÜıİöÖşŞğĞçÇ]\\d]+"; 
     while ((word = infile.readLine()) != null) { 
      String[] wordList = word.replaceAll(pattern, " ").split("\\s+"); 

      for (int j = 0; j < wordList.length; j++) { 
       if(!wordList[j].isEmpty()){ 
         if(!refinedDict.containsKey(wordList[j])){ 
          refinedDict.put(wordList[j], 1); 
         } 
         else{ 
          refinedDict.put(wordList[j], refinedDict.get(wordList[j])+1); 
         } 
        }//end of for 
       }//end if 
       else{ 
       //do something 
       } 
      }//end for 
     }//end while 

Set<String> keys=refinedDict.keySet(); 
List<String> list=sortList(keys); 
Iterator<String> it=list.iterator(); 
while(it.hasNext()){ 
     String key=it.next(); 
     outfile.write(key + "\t" + refinedDict.get(key) + "\n"); 



どのように私はtoto2のソリューションの実装の作品を適用した後にHashMap

EDIT
でのHashMapでのArrayListを使用することができます。しかし、---としてファイルに書き込むためには>何を
語[FILEID {位置}、{FILEID位置} ...]
を行うことができますか?
このような設計では、シリアライズ可能な実装は役に立ちません。

HashMap<String, HashMap<Integer, ArrayList<Integer>>> outer = ... 
HashMap<Integer, ArrayList<Integer>> inner = ... 
inner.put(1, new ArrayList<Integer>()); 
outer.put("key1", inner); 

あなたがようArrayListを取得できます:正確に

ArrayList<Integer> arr = outer.get("key1").get(1); 

答えて

1

は、私は2つの新しいクラスFileIdと明確にするためPositionInFile代わりIntegerのSを定義します。

public class WordLocation { 
    FileId fileId; 
    PositionInFile position; 

    ... 
} 

してからちょうどMap<String, List<WordLocation>>を持っている:

Map<String, Map<FileId, List<PositionInFile>>> wordsWithLocations; 

for (int j = 0; j < wordList.length; j++) { 
    if (!wordList[j].isEmpty()){ 
     if (!wordsWithLocations.containsKey(wordList[j])) { 
     Map<FileId, List<PositionInFile>> map = new HashMap<>(); 
     List<PositionInFile> list = new ArrayList<>(); 
     list.add(wordPosition[j]); 
     map.put(fileId, list); 
     wordsWithLocations.put(wordList[j], map); 
     } else { 
      Map<FileId, List<PositionInFile>> map = 
          wordsWithLocation.get(wordList[j]); 
      if (map.contains(fileId)) { 
      map.get(fileId).add(wordPosition[j]); 
      } else { 
      List<PositionInFile> list = new ArrayList<>(); 
      list.add(wordPosition[j]); 
      map.put(fileId, list); 
      } 
     } 
    } 
} 

... 

for (String word : wordsWithLocation) { 
    int nAppearances = 0; 
    for (List<PositionInFile> positions :  
          wordsWithLocation.get(word).values()) { 
     nAppearances += positions.size(); 
    } 
    System.out.println(word + " appears " + nAppearances + " times."); 
} 

は、しかし、私はそれを定義するために簡単かつきれいになると思います。欠点は、ファイルへの明示的なマッピングがないことです。しかし、情報はまだそこにあり、List<WordLocation>は、ファイルが処理されたのと同じ順序でリストされたロケーションを持つ必要があります。

+0

それは動作します!しかし、私は本当にそれをファイルに書き込むことを試み、失敗しました。 – anonym

0

。 しかし、ここでは値がCollection型であることをMapに使用する一般的な方法があります。

Map<String, Collection<something>> map ... 
for ... do some job 
    if map.containsKey(keyFound) { 
     map.get(foundKey).add(foundValue); 
    } else { 
     Collection <- create collection 
     Collection.add(foundValue); 
     map.put(foundKey, collection) 
    } 

Google Guavaのマルチマップをチェックすることもできます。

希望するもの...

+0

実際にキャストが必要ですか? – toto2

+0

@ toto2:あなたはそうです、ジェネリック版は必要ありません。私は編集しました。 – Tudor

0

わからないあなたは、上記のように定義されたあなたのHashMapを持っており、このようなエントリを追加すると仮定すると、

0

ネストマップが動作します。そのためのクラスを作成します。すなわち、

class WordsInFile{ 

String fileName; 
Map<String, List<Integer>> wordIdxMap; 

} 

実際にネストマップと大きな違いはありません。より読みやすく、findWord(...)...のようなメソッドを追加することで、mapのget(object)メソッドを2回呼び出すことで迷子にならないようにすることができます。あなたはあなたが何を得ようとしているかをあなたに知らせます。

それは良い考えである場合、私は知らない...

関連する問題