-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
210 lines (167 loc) · 4.9 KB
/
index.php
File metadata and controls
210 lines (167 loc) · 4.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
use GuzzleHttp\Psr7\Response;
require_once __DIR__ . '/vendor/autoload.php';
class Product {
public $name;
public $notProcessedDescription;
public $description;
public $sentimentScore;
public $neutralScore;
public $posScore;
public $negScore;
function __construct($name, $notProcessedDescription, $description)
{
$this->name = $name;
$this->notProcessedDescription = $notProcessedDescription;
$this->description = $description;
$this->sentimentScore = null;
}
public function makeScorePretty()
{
if ($this->sentimentScore == null) {
return;
}
$this->neutralScore = $this->sentimentScore[0][0]->score;
$this->posScore = $this->sentimentScore[0][1]->score;
$this->negScore = $this->sentimentScore[0][2]->score;
}
}
function getDir()
{
return './data';
}
function saveProductsToFile($products)
{
file_put_contents(getDir(), serialize($products));
}
function getProductsFromFile()
{
return unserialize(file_get_contents(getDir()));
}
// získáváme skóre sentimentu pomocí 3rd api
function determineSentimentScore($products)
{
$client = new GuzzleHttp\Client();
for ($i = 0; $i < count($products); $i++)
{
$item = $products[$i];
if ($item->sentimentScore != null) {
continue;
}
try {
$response = $client->request('POST', 'https://api-inference.huggingface.co/models/ProsusAI/finbert', [
'headers' => [
'Authorization' => 'Bearer secretkey'
],
'body' => json_encode([
'inputs' => $item->description
])
]);
if ($response->getStatusCode() != 200)
{
continue;
}
$responseData = json_decode($response->getBody());
$products[$i]->sentimentScore = $responseData; // ukládáme data
$products[$i]->makeScorePretty(); // hodíme data do čitelnější podoby
} catch (Exception $e) {
// too many requests a nebo jiný možný error
break;
}
}
saveProductsToFile($products);
return $products;
}
// preprocessing dat z csv
function processData()
{
$allProducts = array();
$fileToRead = fopen('data.csv', 'r');
if (file_exists(getDir()))
{
return getProductsFromFile();
}
if ($fileToRead !== FALSE)
{
$counter = 0;
while (($data = fgetcsv($fileToRead, null, ',')) !== FALSE)
{
if ($counter != 0)
{
$product = new Product($data[0], $data[1], strip_tags($data[1]));
array_push($allProducts, $product);
}
$counter++;
}
}
fclose($fileToRead);
saveProductsToFile($allProducts);
return $allProducts;
}
function filterNullScore($products) {
return array_filter($products, function($obj) {
if ($obj->sentimentScore == null) {
return false;
}
return true;
});
}
function findMaxScore($products, $property)
{
$products = filterNullScore($products);
$lastHighIndex = -1;
$lastHigh = 0;
for ($i = 0; $i < count($products); $i++)
{
$product = $products[$i];
if ($product->$property > $lastHigh)
{
$lastHighIndex = $i;
$lastHigh = $product->$property;
}
}
return $lastHighIndex;
}
function findMostPositive($products)
{
return findMaxScore($products, "posScore");
}
function findMostNegative($products)
{
return findMaxScore($products, "negScore");
}
function renderData($products)
{
foreach ($products as $product)
{
$htmlToRender = <<< EOT
<div>
<h3>{title}</h3>
<p>{description}</p>
<div>
neutral score: {neutral} positive score: {positive} negative score: {negative}
</div>
</div>
EOT;
$htmlToRender = str_replace("{title}", $product->name, $htmlToRender);
$htmlToRender = str_replace("{description}", $product->description, $htmlToRender);
$htmlToRender = str_replace("{neutral}", $product->neutralScore, $htmlToRender);
$htmlToRender = str_replace("{positive}", $product->posScore, $htmlToRender);
$htmlToRender = str_replace("{negative}", $product->negScore, $htmlToRender);
echo($htmlToRender);
}
}
$products = processData();
$finalProducts = determineSentimentScore($products);
$mostPositive = findMostPositive($finalProducts);
$mostNegative = findMostNegative($finalProducts);
if ($mostPositive != -1) {
echo("Nejvíce pozitivní: " . $finalProducts[$mostPositive]->name);
}
echo("<br/>");
if ($mostNegative != -1) {
echo("Nejvíce negativní: " . $finalProducts[$mostNegative]->name);
}
renderData($finalProducts);
print($mostNegative);
?>