gpt4 book ai didi

java - 斯坦福自然语言处理 : incompatible types: Object cannot be converted to CoreMap

转载 作者:行者123 更新时间:2023-12-02 13:26:04 25 4
gpt4 key购买 nike

我正在尝试使用StanfordNLP使用this tutorial进行命名实体识别。我收到错误

不兼容的类型:对象无法转换为 CoreMap
我尝试将其类型转换为 Object 但无法使其正常工作。

部分代码抛出错误

  // these are all the sentences in this document
// a CoreMap is essentially a Map that uses class objects as keys and has values with
// custom types
List sentences = document.get(SentencesAnnotation.class);
StringBuilder sb = new StringBuilder();
for (CoreMap sentence : sentences) {
// traversing the words in the current sentence, "O" is a sensible default to initialise
// tokens to since we're not interested in unclassified / unknown things..
String prevNeToken = "O";
String currNeToken = "O";
boolean newToken = true;
for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
currNeToken = token.get(NamedEntityTagAnnotation.class);
String word = token.get(TextAnnotation.class);
// Strip out "O"s completely, makes code below easier to understand
if (currNeToken.equals("O")) {
// LOG.debug("Skipping '{}' classified as {}", word, currNeToken);
if (!prevNeToken.equals("O") && (sb.length() > 0)) {
handleEntity(prevNeToken, sb, tokens);
newToken = true;
}
continue;
}

if (newToken) {
prevNeToken = currNeToken;
newToken = false;
sb.append(word);
continue;
}

if (currNeToken.equals(prevNeToken)) {
sb.append(" " + word);
} else {
// We're done with the current entity - print it out and reset
// TODO save this token into an appropriate ADT to return for useful processing..
handleEntity(prevNeToken, sb, tokens);
newToken = true;
}
prevNeToken = currNeToken;
}
}

我是 NLP 菜鸟。提前致谢。

最佳答案

我成功解决了这个问题。将第三行更改为此

List<CoreMap> sentences = document.get(SentencesAnnotation.class);

关于java - 斯坦福自然语言处理 : incompatible types: Object cannot be converted to CoreMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43369161/

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