gpt4 book ai didi

Java Tree实现父子概念

转载 作者:行者123 更新时间:2023-12-01 09:36:35 26 4
gpt4 key购买 nike

我的输入字符串

1. An atomizer comprising liquid supply and an atomizing assembly detachably connected
2. The atomizer according to claim 1, wherein
3. The atomizer according to claim 1, wherein
4. Apparatus as claimed in any of the foregoing claims, wherein
5. Apparatus as claimed in claim 4, wherein

条件

没有一个人是独立的 parent

并且没有 2 与 1 相关

并且 3 不与 1 相关

4号是独立的

并且 5 不与 4 相关

我的输出应该像下面的顺序

1
2
3
4
5

让我知道哪种数据结构最适合我的预期输出

目前我已经完成了依赖和独立的拆分,请进一步提供解决方案

for (int i = 1; i < claimList.getLength(); i++) {
String dummy = claimList.item(i).getNodeValue();

Pattern pattern = Pattern.compile((".*\\bclaimed in claim\\b.*?"));
Matcher matcher = pattern.matcher(dummy);
Boolean isAvailable = matcher.find();
if (isAvailable) {
dependentClaims.add("\n" + claimList.item(i).getNodeValue());
} else {
independentClaims.add("\n" + claimList.item(i).getNodeValue());
}
}

最佳答案

Map<Integer, Set<Integer>> claims = new TreeMap<>();

Pattern pattern = Pattern.compile(("\\bclaim\\s+(\\d+)", Pattern.CASE_INSENSITIVE));

// org.w3c.Node uses zero-based indices too.
for (int i = 0; i < claimList.getLenth(); i++) {
String text = claimList.item(i).getNodeValue();
int claimNo = i + 1;
// Or better take the claimno from the text:
claimNo = Integer.parseInt(text.replaceFirst("(?s)^(\\d*).*$", "0$1"), 10);
// (?s) matches dot `.` also with line breaks
// 0$1 ensures a number
if (claimNo == 0) {
continue;
}
claims.put(claimNo, new TreeSet());

Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
int refClaimNo = Integer.parseInt(matcher.group(1));
if (!claims.containsKey(refClaimNo)) {
claims.put(claimNo, new TreeSet());
}
claims.get(refClaimNo).add(claimNo);
}
}

claims.entrySet().forEach((e) -> {
System.out.println(e.getKey());
e.getValue().forEach((c) -> {
System.out.println(" " + c);
});
});

这会处理列出引用对象的引用声明(反之亦然)。当然还有前向引用和多重引用。

在生产代码中,应确保将 org.w3c.Element 作为节点并获取其文本内容。声明类或类似的类也可能有意义。

关于Java Tree实现父子概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38850969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com