natural language toolkit pp attachment corpus reader c 20012023 nltk project steven bird stevenbird1gmail com edward loper edlopergmail com url https www nltk org for license information see license txt read lines from the prepositional phrase attachment corpus the pp attachment corpus contains several files having the format sentenceid verb noun1 preposition noun2 attachment for example 42960 gives ity to administration v 46742 gives inventors of microchip n the pp attachment is to the verb phrase v or noun phrase n i e vp gives np ity pp to administration vp gives np inventors pp of microchip the corpus contains the following files training training set devset development test set used for algorithm development test test set used to report results bitstrings word classes derived from mutual information clustering for the wall street journal ratnaparkhi adwait 1994 a maximum entropy model for prepositional phrase attachment proceedings of the arpa human language technology conference http www cis upenn eduadwaitpapershlt94 ps the pp attachment corpus is distributed with nltk with the permission of the sentenceid verb noun1 preposition noun2 attachment natural language toolkit pp attachment corpus reader c 2001 2023 nltk project steven bird stevenbird1 gmail com edward loper edloper gmail com url https www nltk org for license information see license txt read lines from the prepositional phrase attachment corpus the pp attachment corpus contains several files having the format sentence_id verb noun1 preposition noun2 attachment for example 42960 gives ity to administration v 46742 gives inventors of microchip n the pp attachment is to the verb phrase v or noun phrase n i e vp gives np ity pp to administration vp gives np inventors pp of microchip the corpus contains the following files training training set devset development test set used for algorithm development test test set used to report results bitstrings word classes derived from mutual information clustering for the wall street journal ratnaparkhi adwait 1994 a maximum entropy model for prepositional phrase attachment proceedings of the arpa human language technology conference http www cis upenn edu adwait papers hlt94 ps the pp attachment corpus is distributed with nltk with the permission of the sentence_id verb noun1 preposition noun2 attachment
from nltk.corpus.reader.api import *
from nltk.corpus.reader.util import *


class PPAttachment:
    def __init__(self, sent, verb, noun1, prep, noun2, attachment):
        self.sent = sent
        self.verb = verb
        self.noun1 = noun1
        self.prep = prep
        self.noun2 = noun2
        self.attachment = attachment

    def __repr__(self):
        return (
            "PPAttachment(sent=%r, verb=%r, noun1=%r, prep=%r, "
            "noun2=%r, attachment=%r)"
            % (self.sent, self.verb, self.noun1, self.prep, self.noun2, self.attachment)
        )


class PPAttachmentCorpusReader(CorpusReader):
    

    def attachments(self, fileids):
        return concat(
            [
                StreamBackedCorpusView(fileid, self._read_obj_block, encoding=enc)
                for (fileid, enc) in self.abspaths(fileids, True)
            ]
        )

    def tuples(self, fileids):
        return concat(
            [
                StreamBackedCorpusView(fileid, self._read_tuple_block, encoding=enc)
                for (fileid, enc) in self.abspaths(fileids, True)
            ]
        )

    def _read_tuple_block(self, stream):
        line = stream.readline()
        if line:
            return [tuple(line.split())]
        else:
            return []

    def _read_obj_block(self, stream):
        line = stream.readline()
        if line:
            return [PPAttachment(*line.split())]
        else:
            return []