我写了一个算法,该算法以(word,(lower,upper))
形式的条目列表作为输入,并应该用(lower,upper)
至word
范围内的文本中的提及来代替。
示例:
文字:“Gauß是德国数学家。” 1.(“ He”,(0,3)),2.(“物理学家”,(18,29))
结果:“他是德国物理学家。”
不幸的是,我在算法中找不到错误。
private static String substituteByOffset(String text, List<Pair<String, Pair<Integer, Integer>>> mentions) {
int offset = 0;
for (Pair<String, Pair<Integer, Integer>> entry : mentions) {
String subst = entry.first;
Pair<Integer, Integer> offsets = entry.second;
Integer lower = offsets.first;
Integer upper = offsets.second;
text = text.substring(0, lower + offset) + subst + text.substring(upper + offset, text.length());
offset = subst.length() - (upper - lower);
}
return text;
}
答案 0 :(得分:1)
substring(int beginIndex,int endIndex)返回一个新字符串,该字符串是 此字符串的子字符串。子字符串从指定的字符串开始 beginIndex并扩展到索引endIndex-1处的字符。 子字符串的长度为endIndex-beginIndex。
因此:
"Gauß was a german mathematican.".substring(0,3) : "Gau"
"Gauß was a german mathematican.".substring(0,4) : "Gauß"
尝试这些输入1. ("He",(0,3)), 2. ("physicist",(18,29))
的结果如下:
“他ß是德国物理学家 n 。”
下面的输入将给出正确的结果:
String text = "Gauß was a german mathematican.";
List<Pair<String, Pair<Integer, Integer>>> mentions = Arrays.asList(
new Pair("He", new Pair<Integer, Integer>(0,4)),
new Pair("physicist", new Pair<Integer, Integer>(18,30)));
substituteByOffset(text, mentions); // prints "He was a german physicist."
答案 1 :(得分:0)
如前所述,我在上面的问题中犯了一个错误,但是我一直在寻找的错误是缺少将偏移量添加到新偏移量中。
private static String substituteByOffset(String text, List<Pair<String, Pair<Integer, Integer>>> mentions) {
int offset = 0;
for (Pair<String, Pair<Integer, Integer>> entry : mentions) {
String subst = entry.first;
Pair<Integer, Integer> offsets = entry.second;
Integer lower = offsets.first;
Integer upper = offsets.second;
text = text.substring(0, lower + offset) + subst + text.substring(upper + offset, text.length());
offset = subst.length() - (upper - lower) + offset;
}
return text;
}