Returns a portion of the sentence from word start index to word stop index. The returned slice is a subclass of Sentence and a deep copy.
(self, start, stop)
| 905 | return indices |
| 906 | |
| 907 | def slice(self, start, stop): |
| 908 | """ Returns a portion of the sentence from word start index to word stop index. |
| 909 | The returned slice is a subclass of Sentence and a deep copy. |
| 910 | """ |
| 911 | s = Slice(token=self.token, language=self.language) |
| 912 | for i, word in enumerate(self.words[start:stop]): |
| 913 | # The easiest way to copy (part of) a sentence |
| 914 | # is by unpacking all of the token tags and passing them to Sentence.append(). |
| 915 | p0 = word.string # WORD |
| 916 | p1 = word.lemma # LEMMA |
| 917 | p2 = word.type # POS |
| 918 | p3 = word.chunk is not None and word.chunk.type or None # CHUNK |
| 919 | p4 = word.pnp is not None and "PNP" or None # PNP |
| 920 | p5 = word.chunk is not None and unzip(0, word.chunk.relations) or None # REL |
| 921 | p6 = word.chunk is not None and unzip(1, word.chunk.relations) or None # ROLE |
| 922 | p7 = word.chunk and word.chunk.anchor_id or None # ANCHOR |
| 923 | p8 = word.chunk and word.chunk.start == start+i and BEGIN or None # IOB |
| 924 | p9 = word.custom_tags # User-defined tags. |
| 925 | # If the given range does not contain the chunk head, remove the chunk tags. |
| 926 | if word.chunk is not None and (word.chunk.stop > stop): |
| 927 | p3, p4, p5, p6, p7, p8 = None, None, None, None, None, None |
| 928 | # If the word starts the preposition, add the IOB B-prefix (i.e., B-PNP). |
| 929 | if word.pnp is not None and word.pnp.start == start+i: |
| 930 | p4 = BEGIN+"-"+"PNP" |
| 931 | # If the given range does not contain the entire PNP, remove the PNP tags. |
| 932 | # The range must contain the entire PNP, |
| 933 | # since it starts with the PP and ends with the chunk head (and is meaningless without these). |
| 934 | if word.pnp is not None and (word.pnp.start < start or word.chunk.stop > stop): |
| 935 | p4, p7 = None, None |
| 936 | s.append(word=p0, lemma=p1, type=p2, chunk=p3, pnp=p4, relation=p5, role=p6, anchor=p7, iob=p8, custom=p9) |
| 937 | s.parent = self |
| 938 | s._start = start |
| 939 | return s |
| 940 | |
| 941 | def copy(self): |
| 942 | return self.slice(0, len(self)) |