Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/infoslicer/processing/Sentence.py
blob: 9659dbb2ec052b40d2aa69cbb86a09ad061081aa (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
# Copyright (C) IBM Corporation 2008

import gi
gi.require_version('Gtk', '3.0')
import os
from gi.repository import Gtk
from gi.repository import GdkPixbuf
import logging

from Article_Data 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 sentence keeps textmarks corresponding to the start and end of the sentence in the buffer.

It has methods for restructuring itself in the event that the textbuffer changes
from an action not controlled by the Article object it is contained in.

"""

logger = logging.getLogger('infoslicer')

class RawSentence:
    
    def __init__(self, id, source_article_id, source_section_id, source_paragraph_id, source_sentence_id, buf, formatting, leftmark, rightmark):
        self.id = id
        self.source_article_id = source_article_id
        self.source_section_id = source_section_id
        self.source_paragraph_id = source_paragraph_id
        self.source_sentence_id = source_sentence_id
        self.buf = buf
        self.formatting = formatting
        self.leftmark = leftmark
        self.rightmark = rightmark   
        self.type = "sentence"
        
    def generateIds(self):
        if self.id == None or self.id == -1:
            self.id = random.randint(100, 100000)
    
    def delete(self):
        b = self.buf
        l = b.get_iter_at_mark(self.leftmark)
        r = b.get_iter_at_mark(self.rightmark)
        b.delete(l, r)
        b.delete_mark(self.leftmark)
        b.delete_mark(self.rightmark)
        
    def remove(self):
        b = self.buf
        b.delete_mark(self.leftmark)
        b.delete_mark(self.rightmark)
        
    def getStart(self):
        return self.buf.get_iter_at_mark(self.leftmark)
        
    def getEnd(self):    
        return self.buf.get_iter_at_mark(self.rightmark)
    
    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
        source_paragraph_id = self.source_paragraph_id
        source_sentence_id = self.source_sentence_id
        text = self.getText()
        formatting = self.formatting
        
        data = Sentence_Data(id, source_article_id, source_section_id, source_paragraph_id, source_sentence_id, text, formatting)
        return data
    
    def getText(self):
        return self.buf.get_slice(self.getStart(), self.getEnd(), True)
    
    def checkIntegrity(self, nextiter):
        text = self.buf.get_slice(self.getStart(), nextiter, True)
        lines = text.splitlines(True)
        sentencestartoffset = self.getStart().get_offset()
        sentences = []
        if text == "":
            return []
        else:
            for line in lines:
                if line == "":
                    pass
                elif line == "\n":
                    startmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset), False)
                    endmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset + 1), True)
                    sentences.append(RawSentence(self.id, self.source_article_id, self.source_section_id, self.source_paragraph_id, self.source_sentence_id, self.buf, self.formatting, startmark, endmark))
                    sentencestartoffset = sentencestartoffset + 1
                elif line[-1] == "\n":
                    startmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset), False)
                    endmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset + len(line)-1), True)
                    sentences.append(RawSentence(self.id, self.source_article_id, self.source_section_id, self.source_paragraph_id, self.source_sentence_id, self.buf, self.formatting, startmark, endmark))
                    sentencestartoffset = sentencestartoffset + len(line)-1
                    startmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset), False)
                    endmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset + 1), True)
                    sentences.append(RawSentence(self.id, self.source_article_id, self.source_section_id, self.source_paragraph_id, self.source_sentence_id, self.buf, self.formatting, startmark, endmark))
                    sentencestartoffset = sentencestartoffset + 1
                else:
                    startmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset), False)
                    endmark = self.buf.create_mark(None, self.buf.get_iter_at_offset(sentencestartoffset + len(line)), True)
                    sentences.append(RawSentence(self.id, self.source_article_id, self.source_section_id, self.source_paragraph_id, self.source_sentence_id, self.buf, self.formatting, startmark, endmark))
        
        return sentences    

class Sentence( RawSentence ):
    
    def __init__(self, sentence_data, buf, insertioniter):
        
        id = sentence_data.id
        source_article_id = sentence_data.source_article_id
        source_section_id = sentence_data.source_section_id
        source_paragraph_id = sentence_data.source_paragraph_id
        source_sentence_id = sentence_data.source_sentence_id
        
        """ 
        Here, apply formatting changes when necessary.
        Yet to be implemented. """
        formatting = sentence_data.formatting
                
        rightmark = buf.create_mark(None, insertioniter, True)
        leftmark = buf.create_mark(None, insertioniter, False)
        buf.insert(insertioniter, sentence_data.text)
        left = buf.get_iter_at_mark(rightmark)
        right = buf.get_iter_at_mark(leftmark)
        buf.move_mark(leftmark, left)
        buf.move_mark(rightmark, right)
        
        RawSentence.__init__(self, id, source_article_id, source_section_id, source_paragraph_id, source_sentence_id, buf, formatting, leftmark, rightmark)

class Picture( RawSentence ):
    
    def __init__(self, picture_data, buf, insertioniter):
        id = 0
        source_article_id = picture_data.source_article_id
        source_section_id = 0
        source_paragraph_id = 0
        source_sentence_id = 0
        formatting = [] 
        
        self.text = picture_data.text
        self.orig = picture_data.orig
        
        rightmark = buf.create_mark(None, insertioniter, True)
        leftmark = buf.create_mark(None, insertioniter, False)
        
        if os.path.isfile(picture_data.text):
            pixbuf = GdkPixbuf.Pixbuf.new_from_file(picture_data.text) 
            buf.insert_pixbuf(insertioniter, pixbuf)
        else:
            logger.warning('cannot open image %s' % picture_data.text)
        
        left = buf.get_iter_at_mark(rightmark)
        right = buf.get_iter_at_mark(leftmark)
        buf.move_mark(leftmark, left)
        buf.move_mark(rightmark, right)   
        
        RawSentence.__init__(self, id, source_article_id, source_section_id, source_paragraph_id, source_sentence_id, buf, formatting, leftmark, rightmark)
        self.type = "picture"
        
    def getData(self):
        return Picture_Data(self.source_article_id, self.text, self.orig)

    def checkIntegrity(self, nextiter):
        sentences = []
        if self.getEnd().compare(nextiter) == 0:
            return [self]
        elif self.getStart().compare(self.getEnd()) > 0:
             sentences.append(self)
             if self.getEnd().compare(nextiter) > 0: 
                startmark = self.buf.create_mark(None, self.getEnd(), False)
                endmark = self.buf.create_mark(None, nextiter, True)
                nextsentence = RawSentence(self.source_article_id, 1, 1, 1, self.buf, [], startmark, endmark)
                nextsentences = nextsentence.checkIntegrity(nextiter)
                sentences.extend(nextsentences)
        return sentences
        

class dummySentence( Sentence ):
    def __init__(self, buf, insertioniter, leftgravity):
        self.id = -1
        self.source_article_id = -1
        self.source_section_id = -1
        self.source_paragraph_id = -1
        self.source_sentence_id = -1
        self.text = ""
        self.formatting = []
        self.buf = buf
        self.leftmark = self.buf.create_mark(None, insertioniter, leftgravity)
        self.rightmark = self.buf.create_mark(None, insertioniter, leftgravity)  
        self.type = "dummysentence"