Example usage for edu.stanford.nlp.trees TreebankLanguagePack isPunctuationWord

List of usage examples for edu.stanford.nlp.trees TreebankLanguagePack isPunctuationWord

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees TreebankLanguagePack isPunctuationWord.

Prototype

boolean isPunctuationWord(String str);

Source Link

Document

Accepts a String that is a punctuation word, and rejects everything else.

Usage

From source file:reck.parser.lexparser.RECKLexicalizedParser.java

License:Open Source License

/** Adds a sentence final punctuation mark to sentences that lack one.
 *  This method adds a period (the first sentence final punctuation word
 *  in a parser language pack) to sentences that don't have one within
 *  the last 3 words (to allow for close parentheses, etc.).  It checks
 *  tags for punctuation, if available, otherwise words.
 *  @param sentence The sentence to check
 *  @param length The length of the sentence (just to avoid recomputation)
 *///  w  w  w .j a v  a  2 s .co  m
private void addSentenceFinalPunctIfNeeded(List<HasWord> sentence, int length) {
    int start = length - 3;
    if (start < 0)
        start = 0;
    TreebankLanguagePack tlp = op.tlpParams.treebankLanguagePack();
    for (int i = length - 1; i >= start; i--) {
        Object item = sentence.get(i);
        // An object (e.g., MapLabel) can implement HasTag but not actually store
        // a tag so we need to check that there is something there for this case.
        // If there is, use only it, since word tokens can be ambiguous.
        String tag = null;
        if (item instanceof HasTag) {
            tag = ((HasTag) item).tag();
        }
        if (tag != null && !"".equals(tag)) {
            if (tlp.isSentenceFinalPunctuationTag(tag)) {
                return;
            }
        } else if (item instanceof HasWord) {
            String str = ((HasWord) item).word();
            if (tlp.isPunctuationWord(str)) {
                return;
            }
        } else {
            String str = item.toString();
            if (tlp.isPunctuationWord(str)) {
                return;
            }
        }
    }
    // none found so add one.
    if (Test.verbose) {
        System.err.println("Adding missing final punctuation to sentence.");
    }
    String[] sfpWords = tlp.sentenceFinalPunctuationWords();
    if (sfpWords.length > 0) {
        sentence.add(new Word(sfpWords[0]));
    }
}