-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIEEEGatewayExtractor.class.php
More file actions
193 lines (169 loc) · 5.89 KB
/
Copy pathIEEEGatewayExtractor.class.php
File metadata and controls
193 lines (169 loc) · 5.89 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
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
<?php
/**
* User: Guillem LLuch
* Date: 18/12/16
* Time: 13:42
*/
namespace glluchcom\csdlExtractor;
class IEEEGatewayExtractor
{
/**
* @var string Xpath expression to get the information need. In this case
* is the titles of the articles.
*/
protected $controlledPattern = "/root/document/controlledterms/term";
protected $thesaurusPattern = "/root/document/thesaurusterms/term";
protected $controlledTerms = array();
protected $thesaurusTerms = array();
protected $termsRelated = array();
/**
* @return array
*/
public function getTermsRelated($filename)
{
if (count($this->termsRelated) == 0) $this->extractAll($filename);
return $this->termsRelated;
}
public function extractAll($filename)
{
$files0 = file_get_contents($filename);
$files = explode("\n", $files0);
foreach ($files as $file) {
if ($file != "") {
$term = trim($file);
$dir = "xml/" . $term . "/";
$terms = $this->extractTerms($dir, true);
$this->termsRelated[$term] = $terms;
}
}
}
/**
* For a term search all related term in the term dir.
* @param $path . The name of the dir where the xml file reside.
* @param $orderByValues . Boolean which indicates if the order will base
* on values (true) or on key, by alphabetic order (false)
* @return array . A complex array made by terms controlled and thesaurus
* with its weight
*
**/
protected function extractTerms($path, $orderByValues)
{
$dir = opendir($path);
/** @var array $related sourceTerm =< array ( type of term =<
* array(term =< its weight) )*/
$related = array();
$related["controlled"] = array();
$related["thesaurus"] = array();
while ($file = readdir($dir)) {
if (!is_dir($file) && $file != "." && $file != "..") {
$fileInfo = pathinfo($file);
if (@$fileInfo["extension"] === "xml") {
$name0 = $fileInfo["filename"];
$names = explode("_", $name0);
$location = $fileInfo["dirname"];
$pos = $names[0];
//$name = $names[1];
$text = file_get_contents($path . $file);
$xmldoc = new \DOMDocument();
if (@$xmldoc->loadXML($text)) {
$terms = $this->extractOne($xmldoc);
$terms["pos"] = $pos;
//$terms["name"] = $name;
$related = $this->allTermsMerge($related, $terms, $pos);
}
}
}
}
if ($orderByValues) {
arsort($related["controlled"], SORT_NUMERIC);
arsort($related["thesaurus"], SORT_NUMERIC);
} else {
ksort($related["controlled"]);
ksort($related["thesaurus"]);
}
return $related;
}
public function extractOne($domXml)
{
$xpath = new \DOMXPath($domXml);
//var_dump($xpath);
$items = $xpath->query($this->controlledPattern);
if ($items) {
//var_dump($items);
foreach ($items as $item) {
$this->controlledTerms[] = trim($item->nodeValue);
}
}
$items = $xpath->query($this->thesaurusPattern);
if ($items) {
//var_dump($items);
foreach ($items as $item) {
$this->thesaurusTerms[] = trim($item->nodeValue);
}
}
return array("controlled" => $this->controlledTerms,
"thesaurus" => $this->thesaurusTerms);
}
/**
* Add terms in a matrix of terms. The terms act as column title and its value
* is its aggregate counts.
* @param $related . The matrix where the new counts will be added.
* @param $terms . A matrix containing new counts to be added to related.
* @param $pos . A factor which represents the rank of the actual $terms.
* So, a smaller number is more important than a greater one.
* @return mixed . A new matrix with the new terms added and the old one
* updated.
*/
protected function allTermsMerge($related, $terms, $pos)
{
if ($related === null || !is_array($related)) {
echo "ERROR in the type of object " . PHP_EOL;
echo " pos=" . $pos . PHP_EOL;
var_dump($related);
return false;
}
$related["controlled"] = $this->termsMerge($related["controlled"],
$terms["controlled"], $pos);
$related["thesaurus"] = $this->termsMerge($related["thesaurus"],
$terms["thesaurus"], $pos);
return $related;
}
protected function termsMerge($related, $terms, $pos)
{
if ($related === null || !is_array($related)) {
echo "ERROR in the type of object " . PHP_EOL;
echo " pos=" . $pos . PHP_EOL;
var_dump($related);
return false;
}
foreach ($terms as $term) {
if ($term !== null && $term != "") {
$weight = 0;
if (array_key_exists($term, $related)) { //$term is in $related
$weight = $related[$term];
}
$weight += $this->calcWeight($pos); //for 0 => 1. for 20 => 0'6;
//echo "term is ";
//var_dump($term);
$related[$term] = $weight;
}
}
return $related;
}
protected function calcWeight($rank)
{
return $this->expDecay($rank);
}
/**
*
* @param $rank int (between 0 and 50)
*/
protected function expDecay($rank)
{
return exp(-0.07824 * $rank);
}
protected function linealDecay($rank)
{
return (50 - $rank / 50);
}
}