Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Processing/Article/Section.py
blob: aecae66440aa3a9df8adc74ce28669ff4677f5e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Copyright (C) IBM Corporation 2008

from Processing.Article.Paragraph import *

"""
Created by Jonathan Mace

The classes here each correspond to a sentence in the given text buffer.

You should not instantiate these classes directly.

Use the "level above" class or the Article class to apply changes to the textbuffer
or structure of the article.

"""

"""
a Section instance contains a list of paragraphs.  It has methods for inserting,
deleting and rearranging paragraphs within itself, as well as other housekeeping
functions.

"""

class RawSection:
    
    def __init__(self, id, source_article_id, source_section_id, paragraphs, buf):
        self.id = id
        self.source_article_id = source_article_id
        self.source_section_id = source_section_id
        self.paragraphs = paragraphs
        self.buf = buf
        
    def insertParagraph(self, paragraph_data, lociter):
        insertionindex = self.__get_best_paragraph(lociter)
        insertioniter = self.paragraphs[insertionindex].getStart()
        paragraph = Paragraph(paragraph_data, self.buf, insertioniter)
        self.paragraphs.insert(insertionindex, paragraph)            
        
    def deleteParagraph(self, lociter):
        deletionindex = self.__get_exact_paragraph(lociter)
        if deletionindex != len(self.paragraphs) - 1:
            paragraph = self.paragraphs[deletionindex]
            paragraph.delete()
            del self.paragraphs[deletionindex]
        if len(self.paragraphs) == 1:
            return True
        else:
            return False
        
    def removeParagraph(self, lociter):
        removalindex = self.__get_exact_paragraph(lociter)
        if removalindex != len(self.paragraphs) - 1:
            paragraph = self.paragraphs[removalindex]
            paragraph.delete()
            del self.paragraphs[removalindex]
        if len(self.paragraphs) == 1:
            return True
        else:
            return False
        
    def splitParagraph(self, lociter):
        paragraphindex = self.__get_exact_paragraph(lociter)
        paragraph = self.paragraphs[paragraphindex]
        source_article_id = paragraph.source_article_id
        source_section_id = paragraph.source_section_id
        source_paragraph_id = paragraph.source_paragraph_id
        firstdata = paragraph.getDataRange(paragraph.getStart(), lociter)
        seconddata = paragraph.getDataRange(lociter, paragraph.getEnd())
        mark = self.buf.create_mark(None, lociter, False)
        if firstdata != [] and seconddata != []:
            self.deleteParagraph(lociter)
            
            insertioniter = self.buf.get_iter_at_mark(mark)
            paragraphdata = Paragraph_Data(None, source_article_id, source_section_id, source_paragraph_id, firstdata)
            paragraph = Paragraph(paragraphdata, self.buf, insertioniter)
            self.paragraphs.insert(paragraphindex, paragraph)   
                
            insertioniter = self.buf.get_iter_at_mark(mark)
            paragraphdata = Paragraph_Data(None, source_article_id, source_section_id, source_paragraph_id, seconddata)
            paragraph = Paragraph(paragraphdata, self.buf, insertioniter)
            self.paragraphs.insert(paragraphindex+1, paragraph)   
            
        
        
                
    def delete(self):
        for paragraph in self.paragraphs:
            paragraph.delete()
            
    def remove(self):
        for paragraph in self.paragraphs:
            paragraph.remove()
            
    def deleteSelection(self, startiter, enditer):
        startindex = self.__get_exact_paragraph(startiter)
        endindex = self.__get_exact_paragraph(enditer)
        if endindex == len(self.paragraphs)-1:
            endindex = endindex - 1
        if startindex == endindex:
            empty = self.paragraphs[startindex].deleteSelection(startiter, enditer)
            if empty:
                self.paragraphs[startindex].delete()
                del self.paragraphs[startindex]
        elif startindex < endindex:
            startmark = self.buf.create_mark(None, startiter, True)
            endmark = self.buf.create_mark(None, enditer, True)
            
            endparagraph = self.paragraphs[endindex]
            empty = endparagraph.deleteSelection(endparagraph.getStart(), self.buf.get_iter_at_mark(endmark))
            if empty:
                self.paragraphs[endindex].delete()
                del self.paragraphs[endindex]
            self.buf.delete_mark(endmark)
            
            for i in range(startindex+1, endindex):
                self.paragraphs[startindex+1].delete()
                del self.paragraphs[startindex+1]
            
            startparagraph = self.paragraphs[startindex]
            empty = startparagraph.deleteSelection(self.buf.get_iter_at_mark(startmark), startparagraph.getEnd())
            if empty:
                self.paragraphs[startindex].delete()
                del self.paragraphs[startindex]
            self.buf.delete_mark(startmark)
        if len(self.paragraphs) == 1:
            return True
        else:
            return False
        
    
    def getParagraph(self, lociter):
        paragraphindex = self.__get_exact_paragraph(lociter)
        return self.paragraphs[paragraphindex]   
    
    def getBestParagraph(self, lociter):
        paragraphindex = self.__get_best_paragraph(lociter)
        if paragraphindex == len(self.paragraphs):
            return self.paragraphs[-1]
        else:
            return self.paragraphs[paragraphindex]
        
    def getStart(self):
        return self.paragraphs[0].getStart()
    
    def getEnd(self):
        return self.paragraphs[-1].getEnd()
        
    def __get_best_paragraph(self, lociter):
        paragraphindex = self.__get_exact_paragraph(lociter)
        paragraph = self.paragraphs[paragraphindex]
        left = paragraph.getStart().get_offset()
        middle = lociter.get_offset()
        right = paragraph.getEnd().get_offset()
        leftdist = middle - left
        rightdist = right - middle
        
        if (paragraphindex < len(self.paragraphs)) and (leftdist > rightdist):
            paragraphindex = paragraphindex +1 
        return paragraphindex
        
    def __get_exact_paragraph(self, lociter):
        i = 0
        for i in range(len(self.paragraphs)-1):
            start = self.paragraphs[i+1].getStart()
            if lociter.compare(start) == -1:
                return i
        return len(self.paragraphs)-1
    
    def getId(self):
        return self.id
    
    def getData(self):
        id = self.id
        source_article_id = self.source_article_id
        source_section_id = self.source_section_id
        paragraphs_data = []
        for paragraph in self.paragraphs[0:len(self.paragraphs)-1]:
            paragraphs_data.append(paragraph.getData())
        
        data = Section_Data(id, source_article_id, source_section_id, paragraphs_data)
        return data
    
    def getDataRange(self, startiter, enditer):
        startindex = self.__get_exact_paragraph(startiter)
        endindex = self.__get_exact_paragraph(enditer)
        if startindex == endindex:
            return self.paragraphs[startindex].getDataRange(startiter, enditer)
        else:
            startdata = []
            startparagraph = self.paragraphs[startindex]
            if startiter.compare(startparagraph.getStart()) == 0:
                startdata.append(self.paragraphs[startindex].getData())
            else:
                startdata.extend(startparagraph.getDataRange(startiter, startparagraph.getEnd()))
                dummydata = Sentence_Data(id = -1, text = "")
                startdata.append(dummydata)
            
            middledata = []
            for paragraph in self.paragraphs[startindex+1:endindex]:
                middledata.append(paragraph.getData())
                
            enddata = [] 
            if endindex != len(self.paragraphs):           
                endparagraph = self.paragraphs[endindex]
                enddata.extend(endparagraph.getDataRange(endparagraph.getStart(), enditer))
                
            data = startdata + middledata + enddata
                
            return data
    
    def mark(self):
        markiter = self.getStart()
        self.markmark = self.buf.create_mark(None, markiter, True)
        arrow = gtk.gdk.pixbuf_new_from_xpm_data(arrow_xpm)
        self.buf.insert_pixbuf(markiter, arrow)
        
    def unmark(self):
        markiter = self.buf.get_iter_at_mark(self.markmark)
        markenditer = self.buf.get_iter_at_offset(markiter.get_offset()+1)
        self.buf.delete(markiter, markenditer)
        self.buf.delete_mark(self.markmark)
        
    def getParagraphs(self):
        return self.paragraphs 
    
    def pad(self):
        # Pad adds a dummy paragraph containing the sentence " ", to this section
        insertioniter = self.paragraphs[-1].getStart()
        dummydata = Sentence_Data(id = -1, text = " ")
        dummyparagraphdata = Paragraph_Data(id = -1, sentences_data = [dummydata])
        paragraph = Paragraph(dummyparagraphdata, self.buf, insertioniter)
        self.paragraphs.insert(-1, paragraph)
        
    def clean(self):
        # Removes the effects of pad.
        # Returns true if, after removing the pad, the section has no meaningful content and can therefore be destroyed
        if len(self.paragraphs) > 1:
            paragraph = self.paragraphs[-2]
            paragraphisempty = paragraph.clean()  
            if paragraphisempty:
                del self.paragraphs[-2]
            if len(self.paragraphs) == 1:
                return True
            else:
                return False
        else:
            return True
        
    def getText(self):
        return self.buf.get_slice(self.getStart(), self.getEnd())
        
    def checkIntegrity(self, nextiter):
        i = 0
        paragraphs = []
        while i < len(self.paragraphs) - 1:
            paragraph = self.paragraphs[i]
            nextparagraph = self.paragraphs[i+1]
            
            if paragraph.getStart().compare(nextparagraph.getStart()) == -1:
                text = self.buf.get_slice(paragraph.getStart(), nextparagraph.getStart())
                if len(text) > 0 and text[-1] != "\n":
                    print "concatenating paragraphs"
                    nextparagraph.sentences = paragraph.sentences + nextparagraph.sentences
                else:
                    paragraphs.extend(paragraph.checkIntegrity(nextparagraph.getStart()))
            else:
                paragraph.remove()
                del self.paragraphs[i]
                i = i - 1
            
            i = i + 1
                    
        paragraph = self.paragraphs[-1]
        
        if paragraph.getStart().compare(nextiter) == -1:
            paragraphs.extend(paragraph.checkIntegrity(nextiter))
        
        sections = []
        paragraphstart = 0
        for i in range(len(paragraphs)-1):
            if paragraphs[i].getText() == "\n":
                sections.append(RawSection(self.id, self.source_article_id, self.source_section_id, paragraphs[paragraphstart:i+1], self.buf))
                paragraphstart = i + 1
        sections.append(RawSection(self.id, self.source_article_id, self.source_section_id, paragraphs[paragraphstart:len(paragraphs)], self.buf))

        return sections
    
    def generateIds(self):
        if self.id == None or self.id == -1:
            self.id = random.randint(100, 100000)
        for paragraph in self.paragraphs[0:len(self.paragraphs)]:
            paragraph.generateIds()
        self.paragraphs[-1].id = -1

class Section( RawSection ):
        
    def __init__(self, section_data, buf, insertioniter):
        id = section_data.id
        source_article_id = section_data.source_article_id
        source_section_id = section_data.source_section_id
        
        paragraphs = []
        insertionmark = buf.create_mark(None, insertioniter, False)
          
        for paragraph_data in section_data.paragraphs_data:
            insertioniter = buf.get_iter_at_mark(insertionmark)
            paragraphs.append(Paragraph(paragraph_data, buf, insertioniter))
            
        insertioniter = buf.get_iter_at_mark(insertionmark)
        endparagraphdata = Paragraph_Data(id = 1, sentences_data = [])
        paragraphs.append(Paragraph(endparagraphdata, buf, insertioniter))
        
        buf.delete_mark(insertionmark)
        
        RawSection.__init__(self, id, source_article_id, source_section_id, paragraphs, buf)
     
class dummySection(Section):
    def __init__(self, buf, insertioniter, leftgravity):
        self.id = -1
        self.source_article_id = -1
        self.source_section_id = -1
        self.buf = buf
        self.paragraphs = [ dummyParagraph(buf, insertioniter, leftgravity) ]