Adding synonym words different code numbers - java

心不动则不痛 提交于 2019-12-23 06:12:33

问题


I have this program part, which puts specific numbers in all sections. What I would like to have is to make the iterator keep counting the words, even if the word are same. This recent code sets the same number when same word comes again. For example it should approximately look like this:

Before -------------------->>>> After

AAAA -------------------->>>> 001 AAAA

BBB --------------------->>>> 002 BBB

CCCC ----------------->>>>>> 003 CCCC

BBB ---------------------->>>>>> 004 BBB

Can I somehow solve it with if? for example in the example above:

int i=0; if (val[1].equals("BBB"){ i++; if(i==2){sections.put("004","BBB");  //only the second B must be changed at all times, that's why I thought when i=2 then section.put(....) stuff can be used.

`

// List of all Sections, [0]=Code, [1]=Expression

I hope I explained clearly. Any suggestions?

static final ArrayList<String[]> sections= new ArrayList<String[]>();

static final ArrayList sections= new ArrayList();

private static String convert (final String s) { 
    String temp = s;
    //replaces the defined section keywords with a unique section code identifier(3 Bits)
    final Iterator<String[]> sic = sections.iterator();
    while (sic.hasNext()) {
        final String[] val = sic.next();
        temp = temp.replaceAll("\n(" + val[1] + ")\r?\n--*\r?\n",  val[0].length()>0 ? "\n\u25b4\u25ba"+val[0]+"\n$1\n": "\n\n\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
    }
    return (temp + "\u25b4").replaceAll("\u25ba212(\n" + ratioRam +"\u25b4)","\u25ba222$1"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
} 

// in another part, the file sections are read. : readList(sections) ;


回答1:


If I understand this correctly, you want to add section numbers to a document, where there might be repeating section names. You sections list seems to hold arrays describing the sections, with the first element being the section number and the second element the section name.

You do so by constructing a regular expression using the section title and the expected surrounding markup, and then replace that with the title preceded by the respective number. Now the problem seems to be that there can be repeated section titles, and using replaceAll you find all those sections and put the same number in from of them.

Instead of using replaceAll, you could use replaceFirst to replace just the first occurrence of that section title. Of course, this requires the sections list to be sorted.


Here's a minimal example (slightly simplified, but using the same regex as in your question).

String text = "bla bla \nsection\n------\nmore text\nanother section"
        + "\n----\ntext again\nsection\n---\nfinal bunch of text";
Map<String, String> sections = new TreeMap<>();
sections.put("001", "section");
sections.put("002", "another section");
sections.put("003", "section");
for (String num : sections.keySet()) {
    String title = sections.get(num);
    text= text.replaceFirst("\n(" + title + ")\r?\n--*\r?\n", "\n\u25b4\u25ba"+ num+"\n$1\n");
}
System.out.println(text);

In the output, all the sections are correctly numbered.



来源:https://stackoverflow.com/questions/23385074/adding-synonym-words-different-code-numbers-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!