简体   繁体   中英

Reordering the string value using hashmap

My sentence is "ram is going to school" after giving input it will give an parse tree as:

2|TYPE| nx0-VPadjn-VPx1 0|NOUN| NXN 1|AUX| Vvx 3|PREP| Pnx 4|NOUN| NXN

I want to reorder it to

0|NOUN| NXN 1|AUX| Vvx 2|TYPE| nx0-VPadjn-VPx1 3|PREP| Pnx 4|NOUN| NXN

I wrote this code. What should I do to make it work?

Please tell me whether it is possible with HashMap . If possible then please tell me how?

for(int j=1;j<2;j++) //when multiple parsing is to be done then change the limit to ---parsedderived.length
{
    parsedderived[j] = parsedderived[j].replaceAll("\\^|\\(|\\)|\\~|\\.|\"", " ");
    parsedderived[j] = parsedderived[j].replaceAll("\\{\\S+\\}", "");
    parsedderived[j] = parsedderived[j].replaceAll("[ ]+", " ");
    parsedderived[j] = parsedderived[j].replaceAll("(\\d+)\\s+(\\S+)\\s+(\\S+)", "$1-$2@@$3");
    preparedStatement.setString(4, parsedderived[j]);            

    //String[] x=objSentenceStatusImpl.getStrSourceSen().split("");
    String[] derivedsen=parsedderived[j].split(" ");

    //                    for(int k=1;k<derivedsen.length;k++)
    //                    {
    //                       String[] derivedsen1=derivedsen[k].split("");
    //                       System.out.println(derivedsen);
    //                    }

    String[]Senwords=objSentenceStatusImpl.getStrSourceSen().split("\\s+");//original sentence ram is going to school
    Map<Integer, String> entriesMap = new LinkedHashMap<Integer, String>();

    for(int i = 0; i < Senwords.length; i++) {//for iterration of sen
        parsedderived[j]=parsedderived[j].replaceFirst(" ","");
        parsedderived[j]=parsedderived[j].replaceAll(" ", "##");

        System.out.println(parsedderived[j]);
    }
}

A TreeMap<Integer, String> will sort the keys for you. Just enter your Integer, String pairs, then when you iterate through treeMap.values() your strings will be in the order of their keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM