-
Notifications
You must be signed in to change notification settings - Fork 1
/
CombineEntities.php
162 lines (150 loc) · 5.15 KB
/
CombineEntities.php
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
<?php
$aiHandle = fopen($argv[1], 'r');
$slHandle = fopen($argv[2], 'r');
$trHandle = fopen($argv[3], 'r');
$output = fopen($argv[4], 'w');
if (!$aiHandle || !$trHandle || !$output) {
die("Problem opening file(s)");
}
$aiHeader = prefixHeader('AlchemyAPI ', fgetcsv($aiHandle));
$slHeader = prefixHeader('Spotlight ', fgetcsv($slHandle));
$trHeader = prefixHeader('TextRazor ', fgetcsv($trHandle));
$aiDummies = generateDummyRow(count($aiHeader));
$slDummies = generateDummyRow(count($slHeader));
$trDummies = generateDummyRow(count($trHeader));
fputcsv($output, array_merge(['Matched Rows', 'Matched IDs'], $aiHeader, $slHeader, $trHeader));
/* AlchemyAPI indexing routine -- no longer used, since we're now making this the center of the join
$aiRows = $aiFreebaseIndex = $aiKeywordIndex = [];
while ($line = fgetcsv($aiHandle)) {
$aiRows[] = $line;
if (isset($aiKeywordIndex[strtolower($line[0])])) {
echo "Warning: duplicate AlchemyAPI row for {$line[0]}\n";
}
$aiKeywordIndex[strtolower($line[0])] = & $aiRows[count($aiRows) - 1];
if (!empty($line[8])) {
$aiFreebaseIndex[$line[8]] = & $aiRows[count($aiRows) - 1];
}
}
fclose($aiHandle);
*/
$trRows = $trFreebaseIndex = $trKeywordIndex = [];
while ($line = fgetcsv($trHandle)) {
$trRows[] = $line;
if (isset($trKeywordIndex[strtolower($line[0])])) {
echo "Warning: duplicate TextRazor row for {$line[0]}\n";
}
$trKeywordIndex[strtolower($line[0])] = & $trRows[count($trRows) - 1];
if (!empty($line[3])) {
$fbId = 'http://rdf.freebase.com/ns' . $line[3];
$trFreebaseIndex[$fbId] = & $trRows[count($trRows) - 1];
}
}
fclose($trHandle);
$slRows = $slDbpediaIndex = $slKeywordIndex = [];
while ($line = fgetcsv($slHandle)) {
$slRows[] = $line;
$slDbpediaIndex[$line[0]] = & $slRows[count($slRows) - 1];
foreach (array_unique(array_map('strtolower', explode(',', $line[3]))) as $keyword) {
if (!empty($keyword)) {
if (isset($slKeywordIndex[$keyword])) {
echo "Warning: duplicate Spotlight row for {$keyword}\n";
}
$slKeywordIndex[$keyword] = & $slRows[count($slRows) - 1];
}
}
}
fclose($slHandle);
// first try to find matches around AlchemyAPI, which has the potential for a three-way join
$slMatchedIds = $trMatchedKeywords = [];
while ($line = fgetcsv($aiHandle)) {
unset($trMatchedLine);
unset($slMatchedLine);
$matchedRows = $matchedIds = 0;
if (!empty($line[8])) {
if (isset($trFreebaseIndex[$line[8]])) {
$matchedIds++;
$trMatchedLine = & $trFreebaseIndex[$line[8]];
}
}
if (!isset($trMatchedLine) && isset($trKeywordIndex[strtolower($line[0])])) {
$trMatchedLine = & $trKeywordIndex[strtolower($line[0])];
}
if (!isset($trMatchedLine)) {
$trMatchedLine = & $trDummies;
} else {
$matchedRows++;
$trMatchedKeywords[] = $trMatchedLine[0];
}
if (!is_array($trMatchedLine)) {
var_dump($trMatchedLine);
die('Unexpected value');
}
if (!empty($line[7])) {
if (isset($slDbpediaIndex[$line[7]])) {
$matchedIds++;
$slMatchedLine = & $slDbpediaIndex[$line[7]];
}
}
if (!isset($slMatchedLine) && isset($slKeywordIndex[strtolower($line[0])])) {
$slMatchedLine = & $slKeywordIndex[strtolower($line[0])];
}
if (!isset($slMatchedLine)) {
$slMatchedLine = & $slDummies;
} else {
$matchedRows++;
$slMatchedIds[] = $slMatchedLine[0];
}
fputcsv($output, array_merge([$matchedRows, $matchedIds], $line, $slMatchedLine, $trMatchedLine));
}
fclose($aiHandle);
// check unmatched TextRazor rows to see if we can join them to Spotlight.
foreach ($trRows as $line) {
// already stored this line? skip it!
if (in_array($line[0], $trMatchedKeywords)) {
continue;
}
unset($slMatchedLine);
$matchedRows = $matchedIds = 0;
if (!empty($line[2])) {
$parts = explode('/', $line[2]);
$lastPart = array_pop($parts);
$dbpediaId = 'http://dbpedia.org/resource/' . $lastPart;
if (isset($slDbpediaIndex[$dbpediaId])) {
$matchedIds++;
$slMatchedLine = & $slDbpediaIndex[$dbpediaId];
}
}
if (!isset($slMatchedLine) && isset($slKeywordIndex[strtolower($line[0])])) {
$slMatchedLine = & $slKeywordIndex[strtolower($line[0])];
}
if (!isset($slMatchedLine)) {
$slMatchedLine = & $slDummies;
} else {
$matchedRows++;
$slMatchedIds[] = $slMatchedLine[0];
}
fputcsv($output, array_merge([$matchedRows, $matchedIds], $aiDummies, $slMatchedLine, $line));
}
// finally, dump out unmatched Spotlight rows.
foreach ($slRows as $line) {
if (!in_array($line[0], $slMatchedIds)) {
fputcsv($output, array_merge([1, 0], $aiDummies, $line, $trDummies));
}
}
fclose($output);
function prefixHeader($prefix, $row)
{
$result = [];
foreach ($row as $current) {
$result[] = $prefix . $current;
}
return $result;
}
function generateDummyRow($c, $text = 'n/a')
{
$row = [];
for ($i = 0; $i < $c; $i++) {
$row[] = $text;
}
return $row;
}