添加同义词不同的代码 - java [英] Adding synonym words different code numbers - java

查看:98
本文介绍了添加同义词不同的代码 - java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序部分,它在所有部分中都有特定的数字。我想要的是让迭代器继续计算单词,即使单词是相同的。当同一个词再次出现时,这个最近的代码设置相同的数字。
例如它应该大致如下所示:

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

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

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

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

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

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

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

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();

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$
} 

//在另一部分中,读取文件部分。 :readList(sections);

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

推荐答案

如果我理解正确,你想在文档中添加节号,重复部分名称。您 sections list似乎包含描述这些节的数组,第一个元素是节号,第二个元素是节名。

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.

您可以使用节标题和预期的周围标记构建正则表达式,然后将其替换为前面带有相应数字的标题。现在的问题似乎是可以重复节标题,并使用 replaceAll 您可以找到所有这些节并从中添加相同的数字。

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.

您可以使用 replaceAll 。 0 / docs / api / java / lang / String.html#replaceFirst%28java.lang.String,%20java.lang.String%29rel =nofollow> replaceFirst 仅替换该节标题的第一次出现。当然,这需要对部分列表进行排序。

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.

这篇关于添加同义词不同的代码 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆