| 1040 | #--- TEXT ------------------------------------------------------------------------------------------ |
| 1041 | |
| 1042 | class Text(list): |
| 1043 | |
| 1044 | def __init__(self, string, token=[WORD, POS, CHUNK, PNP, REL, ANCHOR, LEMMA], language="en", encoding="utf-8"): |
| 1045 | """ A list of Sentence objects parsed from the given string. |
| 1046 | The string is the Unicode return value from parse(). |
| 1047 | """ |
| 1048 | self.encoding = encoding |
| 1049 | # Extract token format from TokenString if possible. |
| 1050 | if _is_tokenstring(string): |
| 1051 | token, language = string.tags, getattr(string, "language", language) |
| 1052 | if string: |
| 1053 | for s in string.split("\n"): |
| 1054 | self.append(Sentence(s, token, language)) |
| 1055 | |
| 1056 | def insert(self, index, sentence): |
| 1057 | list.insert(self, index, sentence) |
| 1058 | self[-1].text = self |
| 1059 | def append(self, sentence): |
| 1060 | list.append(self, sentence) |
| 1061 | self[-1].text = self |
| 1062 | def extend(self, sentences): |
| 1063 | for s in sentences: |
| 1064 | self.append(s) |
| 1065 | |
| 1066 | def remove(self, sentence): |
| 1067 | list.remove(self, sentence) |
| 1068 | sentence.text = None |
| 1069 | def pop(self, index): |
| 1070 | sentence = list.pop(self, index) |
| 1071 | sentence.text = None |
| 1072 | return sentence |
| 1073 | |
| 1074 | @property |
| 1075 | def sentences(self): |
| 1076 | return list(self) |
| 1077 | |
| 1078 | def copy(self): |
| 1079 | t = Text("", encoding=self.encoding) |
| 1080 | for sentence in self: |
| 1081 | t.append(sentence.copy()) |
| 1082 | return t |
| 1083 | |
| 1084 | # Text.string and unicode(Text) are Unicode strings. |
| 1085 | @property |
| 1086 | def string(self): |
| 1087 | return u"\n".join([unicode(sentence) for sentence in self]) |
| 1088 | def __unicode__(self): |
| 1089 | return self.string |
| 1090 | #def __repr__(self): |
| 1091 | # return "\n".join([repr(sentence) for sentence in self]) |
| 1092 | |
| 1093 | @property |
| 1094 | def xml(self): |
| 1095 | """ Yields the sentence as an XML-formatted string (plain bytestring, UTF-8 encoded). |
| 1096 | All the sentences in the XML are wrapped in a <text> element. |
| 1097 | """ |
| 1098 | xml = [] |
| 1099 | xml.append('<?xml version="1.0" encoding="%s"?>' % XML_ENCODING.get(self.encoding, self.encoding)) |
no outgoing calls
searching dependent graphs…