testExtracters.php
4.54 KB
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
<?php
require_once(KT_DIR . '/tests/test.php');
class DocumentExtractorsTestCase extends KTUnitTestCase {
function setup()
{
//Indexer::checkForRegisteredTypes();
$this->indexer = Indexer::get();
$this->path = KT_DIR . '/tests/documentProcessor/dataset/';
$this->tempPath = KT_DIR . '/var/tmp';
}
function tearDown()
{
}
/**
* Extract the text from a word (.doc) document
*/
function testExtractWord()
{
// Get extracted test
$text = $this->extractText('word_doc', 'doc', 'application/msword');
// Run test
$this->assertTrue(strpos($text, 'Human reason, in one sphere of its cognition, is called upon') !== false);
// unset text
unset($text);
}
/**
* Extract text from an excel document (.xls)
*/
function testExtractExcel()
{
// Get extracted test
$text = $this->extractText('excel_doc', 'xls', 'application/vnd.ms-excel');
// Run test
$this->assertTrue(strpos($text, 'The time has come the walrus sang') !== false);
unset($text);
}
/**
* Extract text from a powerpoint document (.ppt)
*/
function testExtractPowerPoint()
{
// Get extracted test
$text = $this->extractText('powerpoint_doc', 'ppt', 'application/vnd.ms-powerpoint');
// Run test
$this->assertTrue(strpos($text, 'Human reason, in one sphere of its cognition, is called upon') !== false);
unset($text);
}
function testExtractPDF()
{
// Get extracted test
$text = $this->extractText('pdf_doc', 'pdf', 'application/pdf');
// Run test
$this->assertTrue(strpos($text, 'Human reason, in one sphere of its cognition, is called upon') !== false);
unset($text);
}
function testI18N()
{
// Get extracted test
$text = $this->extractText('i18n_doc', 'doc', 'application/msword');
//$text = utf8_decode($text);
// extracted text is utf-8 encoded - compare to utf-8 encoded text
// Run tests
// arabic
$this->assertTrue(strpos($text, 'فلووةتبنيمشسغداأ') !== false);
// bulgarian
$this->assertTrue(strpos($text, 'фвсеийнскят') !== false);
// chinese - simplified
$this->assertTrue(strpos($text, '外遇的家伙德国莫泊桑') !== false);
// hebrew
$this->assertTrue(strpos($text, 'הערבמתגן') !== false);
// russian
$this->assertTrue(strpos($text, 'ДлдвспинтГт') !== false);
unset($text);
}
function extractText($sourceFile, $extension, $mimeType)
{
static $extractors = array();
// get extractor
$query = "select me.id, me.name from mime_types mt
INNER JOIN mime_extractors me ON mt.extractor_id = me.id
WHERE filetypes = '{$extension}'";
$res = DBUtil::getOneResult($query);
// On first run the mime_extractors table is empty - populate it for the tests
if(empty($res) || PEAR::isError($res)){
$this->indexer->registerTypes(true);
$query = "select me.id, me.name from mime_types mt
INNER JOIN mime_extractors me ON mt.extractor_id = me.id
WHERE filetypes = '{$extension}'";
$res = DBUtil::getOneResult($query);
}
// Instantiate extractor
if(array_key_exists($res['name'], $extractors)){
$extractor = $extractors[$res['name']];
}else{
$extractor = $this->indexer->getExtractor($res['name']);
$extractors[$res['name']] = $extractor;
}
$this->assertNotNull($extractor);
if(empty($extractor)) return '';
// Extract content
$targetFile = tempnam($this->tempPath, 'ktindexer');
$extractor->setSourceFile($this->path . $sourceFile);
$extractor->setTargetFile($targetFile);
$extractor->setMimeType($mimeType);
$extractor->setExtension($extension);
$extractor->extractTextContent();
$text = file_get_contents($targetFile);
$text = $this->filterText($text);
@unlink($targetFile);
return $text;
}
function filterText($text)
{
$src = array("([\r\n])","([\n][\n])","([\n])","([\t])",'([ ][ ])');
$tgt = array("\n","\n",' ',' ',' ');
// shrink what is being stored.
do
{
$orig = $text;
$text = preg_replace($src, $tgt, $text);
} while ($text != $orig);
return $text;
}
}
?>