2017-04-12 94 views
0

我在使用StanfordNLP使用this tutorial來嘗試命名實體識別。我得到錯誤StanfordNLP:不兼容的類型:無法將對象轉換爲CoreMap

incompatible types: Object cannot be converted to 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一個小白。 在此先感謝。

+0

你可以張貼拋出僅供參考,鏈接,完整的代碼在問題中提到的例外 – Ironluca

+0

代碼段。 –

回答

0

我設法解決了這個問題。 更改第三行此

List<CoreMap> sentences = document.get(SentencesAnnotation.class); 
相關問題