add BookReaderGetText.py and BookReaderGetTextWrapper.php
[bookreader.git] / BookReaderIA / datanode / BookReaderGetText.py
1 #!/usr/bin/python
2
3 # Copyright(c)2008-2010 Internet Archive. Software license AGPL version 3.
4
5 # This file is part of BookReader.
6
7 #     BookReader is free software: you can redistribute it and/or modify
8 #     it under the terms of the GNU Affero General Public License as published by
9 #     the Free Software Foundation, either version 3 of the License, or
10 #     (at your option) any later version.
11
12 #     BookReader is distributed in the hope that it will be useful,
13 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #     GNU Affero General Public License for more details.
16
17 #     You should have received a copy of the GNU Affero General Public License
18 #     along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
19 #     
20 #     The BookReader source is hosted at http://github.com/openlibrary/bookreader/
21
22
23 #watch out for blank lines (<LINE></LINE>)
24
25 from lxml import etree
26 import sys
27 import json
28
29 minWordsInBlock = 50
30 maxWordsInBlock = 100
31
32 path = sys.argv[1]
33 pageNum = int(sys.argv[2])
34
35 tree = etree.parse(path)
36
37 objects = tree.findall('//OBJECT')
38
39 #print 'got %s objects' % len(objects)
40
41 page = objects[pageNum]
42
43 lines = page.findall('.//LINE')
44
45 #print 'got %s .//lines' % len(lines)
46
47 textBlocks = []
48 block = ''
49 rects = []
50
51 numWords = 0
52
53 for line in lines:
54
55     top = sys.maxint
56     left = sys.maxint
57     right = -1
58     bottom = -1
59     
60     numWordsInLine = 0
61
62     words = line.findall('.//WORD')
63
64     #print 'at start of line, rects ='
65     #print rects
66     
67     for word in words:
68
69         numWordsInLine += 1
70         
71         text = word.text
72         #print 'got text ' + text
73         
74         coords = word.get('coords').split(',') #l,b,r,t
75         coords = map(int, coords)
76         
77         if int(coords[0]) < left:
78             left = coords[0]
79             
80         if coords[1] > bottom:
81             bottom = coords[1]
82
83         if coords[2] > right:
84             right = coords[2]
85
86         if coords[3] < top:
87             top = coords[3] 
88                                
89         block += word.text + ' '
90         numWords += 1
91     
92         if text.endswith('.') and (numWords>minWordsInBlock):
93             #print 'end of block with numWords=%d' % numWords
94             #print 'block = ' + block
95             
96             rects.append([left, bottom, right, top])            
97             
98             #textBlocks.append(block.strip())
99             rects.insert(0, block.strip())            
100             textBlocks.append(rects)
101             block = ''
102             rects = []
103             numWords = 0
104             numWordsInLine = 0
105             top = sys.maxint
106             left = sys.maxint
107             right = -1
108             bottom = -1
109
110     #end of line
111     if numWordsInLine > 0:
112         rects.append([left, bottom, right, top])
113
114     if numWords>maxWordsInBlock:
115         #textBlocks.append(block.strip())        
116         rects.insert(0, block.strip())            
117         textBlocks.append(rects)        
118         block = ''
119         numWords = 0
120         rects = []        
121      
122     #print 'at end of line, rects ='
123     #print rects
124
125 if '' != block:
126     #textBlocks.append(block.strip())
127     rects.insert(0, block.strip())            
128     textBlocks.append(rects)
129
130 print json.dumps(textBlocks)