Search in sources :

Example 6 with SentenceSplitter

use of edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter in project cogcomp-nlp by CogComp.

the class IllinoisTokenizer method tokenizeTextSpan.

/**
     * given a span of text, return a list of {@literal Pair< String[], IntPair[] >} corresponding
     * to tokenized sentences, where the String[] is the ordered list of sentence tokens and the
     * IntPair[] is the corresponding list of character offsets with respect to <b>the original
     * text</b>.
     *
     * @param text an arbitrary span of text.
     * @return a {@link Tokenization} object containing the ordered token strings, their character
     *         offsets, and sentence end positions (as one-past-the-end token offsets)
     */
@Override
public Tokenization tokenizeTextSpan(String text) {
    String[] splitterInput = new String[1];
    splitterInput[0] = text;
    SentenceSplitter splitter = new SentenceSplitter(splitterInput);
    Sentence[] sentences = splitter.splitAll();
    List<IntPair> characterOffsets = new LinkedList<>();
    int[] sentenceEndTokenOffsets = new int[sentences.length];
    int sentenceEndTokenIndex = 0;
    int sentIndex = 0;
    List<String> tokens = new LinkedList<>();
    for (Sentence s : splitter.splitAll()) {
        LinkedVector words = s.wordSplit();
        if (s.end >= text.length()) {
            throw new IllegalArgumentException("Error in tokenizer, sentence end ( " + s.end + ") is greater than rawtext length (" + text.length() + ").");
        }
        for (int i = 0; i < words.size(); i++) {
            Word word = (Word) words.get(i);
            IntPair wordOffsets = new IntPair(word.start, word.end + 1);
            characterOffsets.add(wordOffsets);
            tokens.add(text.substring(wordOffsets.getFirst(), wordOffsets.getSecond()));
        }
        sentenceEndTokenIndex += words.size();
        sentenceEndTokenOffsets[sentIndex++] = sentenceEndTokenIndex;
    }
    String[] tokenArray = tokens.toArray(new String[tokens.size()]);
    IntPair[] charOffsetArray = characterOffsets.toArray(new IntPair[characterOffsets.size()]);
    return new Tokenization(tokenArray, charOffsetArray, sentenceEndTokenOffsets);
}
Also used : Word(edu.illinois.cs.cogcomp.lbjava.nlp.Word) SentenceSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter) LinkedVector(edu.illinois.cs.cogcomp.lbjava.parse.LinkedVector) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) Sentence(edu.illinois.cs.cogcomp.lbjava.nlp.Sentence)

Example 7 with SentenceSplitter

use of edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter in project cogcomp-nlp by CogComp.

the class TestDiff method testDiff.

@Test
public void testDiff() {
    POSTagger tagger = new POSTagger();
    Parser parser = new PlainToTokenParser(new WordSplitter(new SentenceSplitter(testFile)));
    String sentence = "";
    int sentenceCounter = 0;
    int tokenCounter = 0;
    int correctCounter = 0;
    for (Token word = (Token) parser.next(); word != null; word = (Token) parser.next()) {
        String tag = tagger.discreteValue(word);
        if (refTags.get(tokenCounter).equals(tag)) {
            correctCounter++;
        }
        tokenCounter++;
    }
    double result = ((double) correctCounter) / tokenCounter;
    if (result < thresholdAcc) {
        fail("Tagger performance is insufficient: " + "\nProduced: " + result + "\nExpected: " + thresholdAcc);
    }
}
Also used : SentenceSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter) PlainToTokenParser(edu.illinois.cs.cogcomp.lbjava.nlp.seg.PlainToTokenParser) Token(edu.illinois.cs.cogcomp.lbjava.nlp.seg.Token) WordSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.WordSplitter) POSTagger(edu.illinois.cs.cogcomp.pos.lbjava.POSTagger) Parser(edu.illinois.cs.cogcomp.lbjava.parse.Parser) PlainToTokenParser(edu.illinois.cs.cogcomp.lbjava.nlp.seg.PlainToTokenParser) Test(org.junit.Test)

Example 8 with SentenceSplitter

use of edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter in project cogcomp-nlp by CogComp.

the class TestDiff method testDiff.

@Test
public void testDiff() {
    Chunker tagger = new Chunker();
    Parser parser = new PlainToTokenParser(new WordSplitter(new SentenceSplitter(testFile)));
    String previous = "";
    String sentence = "";
    int sentenceCounter = 0;
    for (Token w = (Token) parser.next(); w != null; w = (Token) parser.next()) {
        String prediction = tagger.discreteValue(w);
        if (prediction.startsWith("B-") || prediction.startsWith("I-") && !previous.endsWith(prediction.substring(2)))
            sentence += ("[" + prediction.substring(2) + " ");
        sentence += ("(" + w.partOfSpeech + " " + w.form + ") ");
        if (!prediction.equals("O") && (w.next == null || tagger.discreteValue(w.next).equals("O") || tagger.discreteValue(w.next).startsWith("B-") || !tagger.discreteValue(w.next).endsWith(prediction.substring(2))))
            sentence += ("] ");
        if (w.next == null) {
            sentence = sentence.trim();
            String refSentence = refSentences.get(sentenceCounter).trim();
            if (!sentence.equals(refSentence))
                fail("Produced output doesn't match reference: " + "\nProduced: " + sentence + "\nExpected: " + refSentence);
            sentence = "";
            sentenceCounter++;
        }
        previous = prediction;
    }
}
Also used : SentenceSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter) PlainToTokenParser(edu.illinois.cs.cogcomp.lbjava.nlp.seg.PlainToTokenParser) Chunker(edu.illinois.cs.cogcomp.chunker.main.lbjava.Chunker) Token(edu.illinois.cs.cogcomp.lbjava.nlp.seg.Token) WordSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.WordSplitter) Parser(edu.illinois.cs.cogcomp.lbjava.parse.Parser) PlainToTokenParser(edu.illinois.cs.cogcomp.lbjava.nlp.seg.PlainToTokenParser) Test(org.junit.Test)

Example 9 with SentenceSplitter

use of edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter in project cogcomp-nlp by CogComp.

the class ChunksAndPOSTags method main.

public static void main(String[] args) {
    String filename = null;
    try {
        filename = args[0];
        if (args.length > 1)
            throw new Exception();
    } catch (Exception e) {
        System.err.println("usage: java edu.illinois.cs.cogcomp.chunker.main.ChunksAndPOSTags <input file>");
        System.exit(1);
    }
    Chunker chunker = new Chunker();
    Parser parser = new PlainToTokenParser(new WordSplitter(new SentenceSplitter(filename)));
    String previous = "";
    for (Word w = (Word) parser.next(); w != null; w = (Word) parser.next()) {
        String prediction = chunker.discreteValue(w);
        if (prediction.startsWith("B-") || prediction.startsWith("I-") && !previous.endsWith(prediction.substring(2)))
            logger.info("[" + prediction.substring(2) + " ");
        logger.info("(" + w.partOfSpeech + " " + w.form + ") ");
        if (!prediction.equals("O") && (w.next == null || chunker.discreteValue(w.next).equals("O") || chunker.discreteValue(w.next).startsWith("B-") || !chunker.discreteValue(w.next).endsWith(prediction.substring(2))))
            logger.info("] ");
        if (w.next == null)
            logger.info("\n");
        previous = prediction;
    }
}
Also used : Word(edu.illinois.cs.cogcomp.lbjava.nlp.Word) SentenceSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter) PlainToTokenParser(edu.illinois.cs.cogcomp.lbjava.nlp.seg.PlainToTokenParser) Chunker(edu.illinois.cs.cogcomp.chunker.main.lbjava.Chunker) WordSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.WordSplitter) Parser(edu.illinois.cs.cogcomp.lbjava.parse.Parser) PlainToTokenParser(edu.illinois.cs.cogcomp.lbjava.nlp.seg.PlainToTokenParser)

Example 10 with SentenceSplitter

use of edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter in project cogcomp-nlp by CogComp.

the class SegmentTagPlain method main.

public static void main(String[] args) {
    String taggerName = null;
    String inputFile = null;
    String parserName = null;
    try {
        taggerName = args[0];
        inputFile = args[1];
        if (args.length > 2) {
            parserName = args[2];
            if (args.length > 3)
                throw new Exception();
        }
    } catch (Exception e) {
        System.err.println("usage: java edu.illinois.cs.cogcomp.lbjava.edu.illinois.cs.cogcomp.lbjava.nlp.seg.SegmentTagPlain <word classifier> " + "<input file> \\\n" + "                                         [<parser>]");
        System.exit(1);
    }
    Classifier tagger = ClassUtils.getClassifier(taggerName);
    Parser parser;
    if (parserName == null)
        parser = new PlainToTokenParser(new WordSplitter(new SentenceSplitter(inputFile)));
    else
        parser = ClassUtils.getParser(parserName, new Class[] { Parser.class }, new Parser[] { new WordSplitter(new SentenceSplitter(inputFile)) });
    String previous = "";
    for (Word w = (Word) parser.next(); w != null; w = (Word) parser.next()) {
        String prediction = tagger.discreteValue(w);
        if (prediction.startsWith("B-") || prediction.startsWith("I-") && !previous.endsWith(prediction.substring(2)))
            System.out.print("[" + prediction.substring(2) + " ");
        System.out.print(w.form + " ");
        if (!prediction.equals("O") && (w.next == null || tagger.discreteValue(w.next).equals("O") || tagger.discreteValue(w.next).startsWith("B-") || !tagger.discreteValue(w.next).endsWith(prediction.substring(2))))
            System.out.print("] ");
        if (w.next == null)
            System.out.println();
        previous = prediction;
    }
}
Also used : Word(edu.illinois.cs.cogcomp.lbjava.nlp.Word) SentenceSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter) Classifier(edu.illinois.cs.cogcomp.lbjava.classify.Classifier) WordSplitter(edu.illinois.cs.cogcomp.lbjava.nlp.WordSplitter) Parser(edu.illinois.cs.cogcomp.lbjava.parse.Parser)

Aggregations

SentenceSplitter (edu.illinois.cs.cogcomp.lbjava.nlp.SentenceSplitter)10 Sentence (edu.illinois.cs.cogcomp.lbjava.nlp.Sentence)5 Word (edu.illinois.cs.cogcomp.lbjava.nlp.Word)5 WordSplitter (edu.illinois.cs.cogcomp.lbjava.nlp.WordSplitter)5 Parser (edu.illinois.cs.cogcomp.lbjava.parse.Parser)5 LinkedVector (edu.illinois.cs.cogcomp.lbjava.parse.LinkedVector)4 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)3 PlainToTokenParser (edu.illinois.cs.cogcomp.lbjava.nlp.seg.PlainToTokenParser)3 Test (org.junit.Test)3 Chunker (edu.illinois.cs.cogcomp.chunker.main.lbjava.Chunker)2 Token (edu.illinois.cs.cogcomp.lbjava.nlp.seg.Token)2 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 Vector (java.util.Vector)2 SpanLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)1 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)1 Classifier (edu.illinois.cs.cogcomp.lbjava.classify.Classifier)1 POSTagger (edu.illinois.cs.cogcomp.pos.lbjava.POSTagger)1 FileNotFoundException (java.io.FileNotFoundException)1 HashSet (java.util.HashSet)1