| 116 | #--- WORD ------------------------------------------------------------------------------------------ |
| 117 | |
| 118 | class Word(object): |
| 119 | |
| 120 | def __init__(self, sentence, string, lemma=None, type=None, index=0): |
| 121 | """ A word in the sentence. |
| 122 | - lemma: base form of the word; "was" => "be". |
| 123 | - type: the part-of-speech tag; "NN" => a noun. |
| 124 | - chunk: the chunk (or phrase) this word belongs to. |
| 125 | - index: the index in the sentence. |
| 126 | """ |
| 127 | try: string = string.decode("utf-8") # ensure Unicode |
| 128 | except: |
| 129 | pass |
| 130 | self.sentence = sentence |
| 131 | self.index = index |
| 132 | self.string = string # "was" |
| 133 | self.lemma = lemma # "be" |
| 134 | self.type = type # VB |
| 135 | self.chunk = None # Chunk object this word belongs to (i.e., a VP). |
| 136 | self.pnp = None # PNP chunk object this word belongs to. |
| 137 | # word.chunk and word.pnp are set in chunk.append(). |
| 138 | self.custom_tags = Tags(self) # User-defined tags. |
| 139 | |
| 140 | def copy(self, chunk=None, pnp=None): |
| 141 | w = Word( |
| 142 | self.sentence, |
| 143 | self.string, |
| 144 | self.lemma, |
| 145 | self.type, |
| 146 | self.index) |
| 147 | w.chunk = chunk |
| 148 | w.pnp = pnp |
| 149 | w.custom_tags = Tags(w, items=self.custom_tags) |
| 150 | return w |
| 151 | |
| 152 | def _get_tag(self): |
| 153 | return self.type |
| 154 | def _set_tag(self, v): |
| 155 | self.type = v |
| 156 | |
| 157 | tag = pos = part_of_speech = property(_get_tag, _set_tag) |
| 158 | |
| 159 | @property |
| 160 | def phrase(self): |
| 161 | return self.chunk |
| 162 | |
| 163 | @property |
| 164 | def prepositional_phrase(self): |
| 165 | return self.pnp |
| 166 | |
| 167 | prepositional_noun_phrase = prepositional_phrase |
| 168 | |
| 169 | @property |
| 170 | def tags(self): |
| 171 | """ Yields a list of all the token tags as they appeared when the word was parsed. |
| 172 | For example: ["was", "VBD", "B-VP", "O", "VP-1", "A1", "be"] |
| 173 | """ |
| 174 | # See also. Sentence.__repr__(). |
| 175 | ch, I,O,B = self.chunk, INSIDE+"-", OUTSIDE, BEGIN+"-" |