0
Map<String, List<String>> words = new HashMap<String, List<String>>();
            List<Map> listOfHash = new ArrayList<Map>();

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    String word = getTagValue("word", eElement);
                    List<String> add_word = new ArrayList<String>();
                    String pos = getTagValue("POS", eElement);
                    if(words.get(pos)!=null){
                        add_word.addAll(words.get(pos));
                        add_word.add(word);
                    }
                    else{
                        add_word.add(word);
                    }
                    words.put(pos, add_word);
                }
            }

これは私が書いたコードの一部です (Stanford CoreNLP を使用しています)。私が直面している問題は、現在、このコードが 1 つのマップ、つまり「単語」でのみ機能していることです。ここで、パーサーが区切り文字である「000000000」を認識したらすぐに、新しいマップをリストに追加し、キーと値を挿入する必要があります。「000000000」が表示されない場合、キーと値は同じマップに追加されます。いくら頑張っても出来ないので助けてください。

4

1 に答える 1

2

listOfHashにはすべてのマップが含まれていると思います...

たとえば、名前wordsをに変更して追加します。currentMap「000000000」が表示されたら、新しいマップをインスタンス化し、に割り当てcurrentMap、リストに追加して、次に進みます...

何かのようなもの:

if ("000000000".equals(word)){
    currentMap = new HashMap<String, List<String>>();
    listOfHash.add(currentMap);
    continue; // if we wan't to skip the insertion of "000000000"
}

また、最初のマップをlistOfHashに追加することを忘れないでください。

また、コードに他の問題があることもわかりました。ここに変更されたバージョンがあります(私はそれをコンパイルしようとしませんでした):

Map<String, List<String>> currentMap = new HashMap<String, List<String>>();
List<Map> listOfHash = new ArrayList<Map>();
listOfHash.add(currentMap);


for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        String word = getTagValue("word", eElement);    

        if ("000000000".equals(word)){
            currentMap = new HashMap<String, List<String>>();
            listOfHash.add(currentMap);
            continue; // if we wan't to skip the insertion of "000000000"
        }

        String pos = getTagValue("POS", eElement);

        List<String> add_word = currentMap.get(pos);
        if(add_word==null){
            add_word = new ArrayList<String>();
            currentMap.put(pos, add_word);
        }
        add_word.add(word);
    }

}
于 2012-06-26T09:48:45.133 に答える