Returns a list of Chunk and Chink objects from the given sentence. Chink is a subclass of Chunk used for words that have Word.chunk == None (e.g., punctuation marks, conjunctions).
(sentence)
| 1019 | # s.chunked(s) => [Chunk('black cats/NP'), Chink('and/O'), Chunk('white dogs/NP')] |
| 1020 | |
| 1021 | def chunked(sentence): |
| 1022 | """ Returns a list of Chunk and Chink objects from the given sentence. |
| 1023 | Chink is a subclass of Chunk used for words that have Word.chunk == None |
| 1024 | (e.g., punctuation marks, conjunctions). |
| 1025 | """ |
| 1026 | # For example, to construct a training vector with the head of previous chunks as a feature. |
| 1027 | # Doing this with Sentence.chunks would discard the punctuation marks and conjunctions |
| 1028 | # (Sentence.chunks only yields Chunk objects), which amy be useful features. |
| 1029 | chunks = [] |
| 1030 | for word in sentence: |
| 1031 | if word.chunk is not None: |
| 1032 | if len(chunks) == 0 or chunks[-1] != word.chunk: |
| 1033 | chunks.append(word.chunk) |
| 1034 | else: |
| 1035 | ch = Chink(sentence) |
| 1036 | ch.append(word.copy(ch)) |
| 1037 | chunks.append(ch) |
| 1038 | return chunks |
| 1039 | |
| 1040 | #--- TEXT ------------------------------------------------------------------------------------------ |
| 1041 |