Ich habe die folgende Zeichenfolge.fangen ähnliche Wörter in einer Zeile
What is (Jim)'s gift (limit)? <=> Personname <=> Amount::Spent
in dieser Linie möchte ich die Start- und Endpositionen (
und )
finden und zu drucken.
In meinem aktuellen Code kann ich es drucken, aber das Problem ist, es wird mehrmals gedruckt (ich bin mir sicher, dass dies wegen der while
ist).
Mein Code ist wie folgt.
String line = "What is (Rakesh)'s gift (limit)? <=> Personname <=> Amount::Spent";
if (line.contains("<=>")) {
String[] example_split = line.split("<=>", 2);
System.out.println("String is " + example_split[1]);
if (example_split[0].length() > 1) {
String[] example_entity = example_split[1].split("<=>");
for (String splitStrings : example_entity) {
int openParamCount = line.length() - line.replace("(", "").length();
int closeParamCount = line.length() - line.replace("(", "").length();
System.out.println(openParamCount + "\t" + closeParamCount);
if (!(openParamCount == closeParamCount))
System.out.println("Paranthesis don't match for " + line);
if (!(openParamCount == example_entity.length))
System.out.println(
"The entities provided and the words marked in paranthesis don't match for " + line);
int entities_count = 0;
int no_of_entities = example_entity.length;
Set utterancesSet = new HashSet<>();
int startPosition = 0;
int endPosition = 0;
while (entities_count < no_of_entities) {
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\((.*?)\\)");
Matcher regexMatcher = regex.matcher(line);
while (regexMatcher.find()) {
startPosition = regexMatcher.start() + 1;
endPosition = regexMatcher.start() - 1;
matchList.add(regexMatcher.group(1));
System.out.println("start position is " + startPosition + " end position is " + endPosition
+ " Entity Type" + example_entity[entities_count]);
}
entities_count++;
}
}
}
}
Erwartete Ausgabe:
String is Personname <=> Amount::Spent
2 2
start position is 9 end position is 12 Entity Type Personname
start position is 22 end position is 27 Entity Type Amount::Spent
Stromausgang
String is Personname <=> Amount::Spent
2 2
start position is 9 end position is 12 Entity Type Personname
start position is 22 end position is 27 Entity Type Personname
start position is 9 end position is 12 Entity Type Amount::Spent
start position is 22 end position is 27 Entity Type Amount::Spent
2 2
start position is 9 end position is 12 Entity Type Personname
start position is 22 end position is 27 Entity Type Personname
start position is 9 end position is 12 Entity Type Amount::Spent
start position is 22 end position is 27 Entity Type Amount::Spent
lassen Sie es mich wissen, wohin gehe ich falsch und wie kann ich dieses Problem beheben.
Dank