简体   繁体   中英

how to cast a string to corelabel in java?

I am using the Stanford Parser . I get an error:

can not parse a string to Corelabel

Here is some code :

List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for(CoreMap sentence: sentences) 
{
    CoreLabel temp=sentence.toString().replace(clust2, clust);
    sentences.set(m.sentNum-1,temp);              
}     

You cannot cast, while you probably can convert, but this depends on what CoreLabel is. In some cases it may be CoreLabel coreLabel = new CoreLabel (string) .

public static void Parse() throws InvalidFormatException, IOException {
    // http://sourceforge.net/apps/mediawiki/opennlp/index.php?title=Parser#Training_Tool
    InputStream is = new FileInputStream("en-parser-chunking.bin");

    ParserModel model = new ParserModel(is);

    Parser parser = ParserFactory.create(model);

    String sentence = "Programcreek is a very huge and useful website.";
    Parse topParses[] = ParserTool.parseLine(sentence, parser, 1);

    for (Parse p : topParses)
        p.show();

    is.close();

    /*
     * (TOP (S (NP (NN Programcreek) ) (VP (VBZ is) (NP (DT a) (ADJP (RB
     * very) (JJ huge) (CC and) (JJ useful) ) ) ) (. website.) ) )
     */
}

u probably can't convert.but this code will work for u

The following function exists under the CoreLabel.java class which you can use.

/** This is provided as a simple way to make a CoreLabel for a word from a String.
   *  It's often useful in fixup or test code. It sets all three of the Text, OriginalText,
   *  and Value annotations to the given value.
   *
   *  @param word The word string to make a CoreLabel for
   *  @return A CoreLabel for this word string
   */
  public static CoreLabel wordFromString(String word) {
    CoreLabel cl = new CoreLabel();
    cl.setWord(word);
    cl.setOriginalText(word);
    cl.setValue(word);
    return cl;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM