-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayTweets.php
More file actions
252 lines (228 loc) · 9.33 KB
/
displayTweets.php
File metadata and controls
252 lines (228 loc) · 9.33 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
// Library Twitter API
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth; ?>
<?php
// Authentication Twitter API
$consumerkey = 'BGuLfVNNwXwLUIaS1Tv4asHbN';
$consumersecret = 'nwpFwnrYHu9vYOatJ5Ndryd0NlcPGjqdhdq2Qc28s7fQiR5Dzq';
$accesstoken = '1469222839-w78n89eGcppCEM6GLoYJH34OrsrJSapqx1fF0uI';
$accesstokensecret = '98dZ175JFF3ypdw8rL4rGp7JasStjNKWT61RNILDhXyK0';
$connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$content = $connection->get("account/verify_credentials");
?>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<title>Tweets | TweetAnalyzer</title>
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a href = "index.php" class="navbar-brand"><strong>TweetAnalyzer</strong></a>
</div>
<div>
<ul class="nav navbar-nav">
<li ><a href="index.php">Home</a></li>
<li class="active"><a href="displayTweets.php">Tweets</a></li>
<li><a href="about.html">About</a></li>
</ul>
</div>
</div>
</nav>
<div class="jumbotron">
<h1>TweetAnalyzer</h1>
<p>Classify tweet with string matching algorithm based on the public service in Bandung</p>
</div>
<div class="container">
<?php
//--- Function to display tweets category : unknown
function displayTweets($keyword, $lines) {
$i = 1;
echo '<table>';
foreach ($lines as $line) {
if ($i == 1){
echo '<tr><td bgcolor="#f0f8ff">';
$i = 2;
} else {
echo '<tr><td bgcolor="#fffffa">';
$i = 1;
}
$line = str_replace("#|","<br>",$line);
//dicek apakah sama dengan keyword pencarian, jika iya penulisan di bold <strong> hitam,
//jika tidak, ditampilkan dengan style normal hitam
$pos = strpos(strtolower($line), $keyword);
if ($pos !== false){
echo substr($line,0,$pos);
echo '<strong>'.substr($line,$pos,strlen($keyword)).'</strong>';
echo substr($line,$pos + strlen($keyword));
echo '<br><br>';
} else {
echo $line.'<br>';
}
echo '</tr></td>';
}
echo '</table>';
}
// Function to display tweets with category
function displayTweets2($f, $l, $x){
$i = 1;
echo '<table>';
foreach ($l as $line) {
if ($i == 1){
echo '<tr><td bgcolor="#f0f8ff">';
$i = 2;
} else {
echo '<tr><td bgcolor="#fffffa">';
$i = 1;
}
$patterns = explode("; ",strtolower($f[$x]));
$n = count($patterns);
$patterns[$n-1] = substr($patterns[$n-1],0,-1);
$line = str_replace("#|","<br>",$line);
$text = explode(" ", $line);
echo ' ';
//cek per kata jika sama dengan pattern didisplay dengan warna merah,
foreach ($text as $t) {
$match = false;
foreach ($patterns as $pattern) {
$pos = strpos(strtolower($t),$pattern);
if ($pos !== false) {
if ($pos == 0) {
echo ' <font color="red">'.substr($t,0,strlen($pattern)).'</font>';
} else {
echo ' '.substr($t,0,$pos);
echo '<font color="red">'.$pattern.'</font>';
}
echo substr($t,$pos+strlen($pattern));
$match = true;
}
}
//jika tidak cocok dengan pattern manapun, dicek apakah sama dengan
//keyword pencarian, jika iya penulisan di bold <strong> hitam,
//jika tidak, ditampilkan dengan style normal hitam
if (!$match) {
$keyword = substr($f[0],0,-1);
$pos = strpos(strtolower($t),$keyword);
if ($pos !== false) {
if ($pos == 0) {
echo ' <strong>'.substr($t,0,strlen($keyword)).'</strong>';
} else {
echo ' '.substr($t,0,$pos);
echo '<strong>'.$keyword.'</strong>';
}
echo substr($t,$pos+strlen($keyword));
} else {
echo ' '.$t;
}
}
}
echo '</tr></td>';
//echo '<br><br>';
}
echo '</table>';
}
//pembacaan file txt berisi data form
$form = file("C:/xampp/htdocs/TweetAnalyzer/text/form.txt");
//search tweets dengan Twitter API sesuai keyword
$keyword = substr($form[0],0,-1);
if (strcmp($keyword,"#pemkotbdg") == 0) {
$tweets = $connection->get("search/tweets", ["q" => "%23pemkotbdg", "count" => 100 , "result_type" => "recent"]);
} elseif (strcmp($keyword,"pemkotbdg") == 0) {
$tweets = $connection->get("search/tweets", ["q" => "pemkotbdg", "count" => 100 , "result_type" => "recent"]);
} elseif (strcmp($keyword,"infobdg") == 0) {
$tweets = $connection->get("search/tweets", ["q" => "infobdg", "count" => 100 , "result_type" => "recent"]);
} elseif (strcmp($keyword,"diskominfobdg") == 0) {
$tweets = $connection->get("search/tweets", ["q" => "diskominfobdg", "count" => 100 , "result_type" => "recent"]);
} elseif (strcmp($keyword,"infobandung") == 0) {
$tweets = $connection->get("search/tweets", ["q" => "infobandung", "count" => 100 , "result_type" => "recent"]);
} elseif (strcmp($keyword,"ridwankamil") == 0) {
$tweets = $connection->get("search/tweets", ["q" => "ridwankamil", "count" => 100 , "result_type" => "recent"]);
}
$string_tweets = "";
if(isset($tweets->statuses) && is_array($tweets->statuses)) {
if(count($tweets->statuses)) {
//menyatukan string tweet menjadi sebuah string dan menyimpannya di file txt
foreach($tweets->statuses as $tweet) {
$string_tweets .= str_replace("\n","#|",$tweet->text)."\n";
}
$outputFile = "C:/xampp/htdocs/TweetAnalyzer/text/tweets.txt";
file_put_contents($outputFile,$string_tweets);
//eksekusi file exe algortima string matching sesuai option (KMP or BM)
if (strcmp($form[6],"KMP") == 0){
echo exec("c:/xampp/htdocs/TweetAnalyzer/bin/StringMatchingKMP");
} else {
echo exec("c:/xampp/htdocs/TweetAnalyzer/bin/StringMatchingBM");
}
//menampilkan hasil klasifikasi tweets beserta waktu analisis
echo "<font color='#fff'><h2><strong>Hasil Analisis Tweets</strong></h2>";
?>
<script type="text/javascript">
var dt = Date();
document.write("Per Date and Time : " + dt );
</script>
<?php
echo "<br>";
//--- Dinas 1
$count = 0;
$lines = file("C:/xampp/htdocs/TweetAnalyzer/text/dinas1.txt");
foreach ($lines as $line) { $count += 1; }
echo "<br><h4><strong>Kategori PDAM</strong><br>Jumlah Tweets = ".$count."<br>Daftar Tweets</h4></font>";
displayTweets2($form, $lines, 1);
//---
//--- Dinas 2
$count = 0;
$lines = file("C:/xampp/htdocs/TweetAnalyzer/text/dinas2.txt");
foreach ($lines as $line) { $count += 1; }
echo "<font color='#fff'><br><br><h4><strong>Kategori Dinas Bina Marga dan Pengairan</strong><br>Jumlah Tweets = ".$count."<br>Daftar Tweets</h4></font>";
displayTweets2($form, $lines,2);
//---
//---Dinas 3
$count = 0;
$lines = file("C:/xampp/htdocs/TweetAnalyzer/text/dinas3.txt");
foreach ($lines as $line) { $count += 1; }
echo "<font color='#fff'><br><br><h4><strong>Kategori Dinas Perhubungan (Dishub)</strong><br>Jumlah Tweets = ".$count."<br>Daftar Tweets</h4></font>";
displayTweets2($form, $lines, 3);
//---
//---Dinas 4
$count = 0;
$lines = file("C:/xampp/htdocs/TweetAnalyzer/text/dinas4.txt");
foreach ($lines as $line) { $count += 1; }
echo "<font color='#fff'><br><br><h4><strong>Kategori Dinas Pengelola Lingkungan Hidup (DPLH)</strong><br>Jumlah Tweets = ".$count."<br>Daftar Tweets</h4></font>";
displayTweets2($form, $lines, 4);
//---
//---Dinas 5
$count = 0;
$lines = file("C:/xampp/htdocs/TweetAnalyzer/text/dinas5.txt");
foreach ($lines as $line) { $count += 1; }
echo "<font color='#fff'><br><br><h4><strong>Kategori Dinas Pendidikan (Disdik)</strong><br>Jumlah Tweets = ".$count."<br>Daftar Tweets</h4></font>";
displayTweets2($form, $lines, 5);
//---
//---Tidak terkategori
$count = 0;
$lines = file("C:/xampp/htdocs/TweetAnalyzer/text/else.txt");
foreach ($lines as $line) { $count += 1; }
echo "<font color='#fff'><br><br><h4><strong>Tidak terkategori</strong><br>Jumlah Tweets = ".$count."<br>Daftar Tweets</h4></font>";
displayTweets($keyword, $lines);
}
else {
echo 'The result is empty';
}
}
?>
<footer>
© Copyright 2014, BackgroungImage Source : http://headerjunction.com/category/Abstract
</footer>
</div>
</body>
</html>