-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_dataset.php
executable file
·152 lines (117 loc) · 3.39 KB
/
prepare_dataset.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
<?php
/* FUNCTIONS */
/* prepareDataset()
* This function accumulates tweets from the various files
* listed in the file list. It requires the setDatasetName()
* function to be executed first. */
/* setDatasetName($name)
* This function sets the name for the dataset. If the
* parameter $name is not specified, it automatically
* sets the current date and time as filename. */
/* CONSTANTS */
/* Filename for the file (JSON)) that stores the
* names of tweet files (CSV) collected so far. */
//define("LIST_TWEET_FILENAMES", "ListTweetFile");
/* The Directory which contains the tweet files (CSV)
* to form a dataset of. */
define("DIR_TWEET_FILES", "DirTweetFiles");
/* Filename for the file (JSON)) that stores the
* list of tweet IDs added to the dataset so far. */
define("LIST_TWEET_IDS", "ListTweetID");
/* Filename for the file (JSON)) that stores the list of
* keywords and their corresponding catagory names.. */
define("LIST_KEYWORDS", "ListKeyword");
/* GLOBAL VARIABLES */
/* Global arrays storing filenames, tweet IDs and keywords */
$FileNames=array();
$TweetIDs=array();
$KeyWords=array();
/* FUNCTION LISTING */
function prepareDataset()
{
global $FileNames;
loadFileNames();
loadTweetIDs();
loadKeywords();
foreach($FileNames as $file_name)
collectTweetsFrom($file_name);
storeTweetIDs();
}
function setDatasetName($name=null)
{
global $dataset_file_name;
if($name)
$dataset_file_name=$name;
else
$dataset_file_name="dataset.".date("Y-m-d_H-i-s");
}
function collectTweetsFrom($file_name)
{
global $TweetIDs, $KeyWords, $dataset_file_name;
if(file_exists(constant("DIR_TWEET_FILES")."/".$file_name.".csv"))
{
$fp=fopen(constant("DIR_TWEET_FILES")."/".$file_name.".csv", "r");
$fp_dataset=fopen($dataset_file_name.".csv", "a");
while(!feof($fp))
{
$new_tweet=fgetcsv($fp, 256);
//if($new_tweet && (!in_array((string) $new_tweet[2], $TweetIDs)))
if($new_tweet && (!isset($TweetIDs[$new_tweet[2]])))
{
//array_push($TweetIDs, (string) $new_tweet[2]);
$TweetIDs[$new_tweet[2]] = 1;
$new_tweet[1]=replaceKeyword($new_tweet[1], $new_tweet[0]);
$new_tweet[0]=$KeyWords[$new_tweet[0]];
unset($new_tweet[2]);
fputcsv($fp_dataset, $new_tweet);
}
}
fclose($fp);
fclose($fp_dataset);
}
}
function replaceKeyword($tweet_text, $keyword)
{
global $KeyWords;
$tweet_text=preg_replace("/$keyword/i", "", $tweet_text);
return $tweet_text;
}
/*
function loadFileNames()
{
global $FileNames;
$FileNames=json_decode(file_get_contents(constant("LIST_TWEET_FILENAMES").".json", true));
}
*/
function loadFileNames()
{
global $FileNames;
$dh=opendir(constant("DIR_TWEET_FILES"));
while($new_file_name=readdir($dh))
{
if($new_file_name!="." && $new_file_name!="..")
array_push($FileNames, preg_replace("/\.csv/", "", $new_file_name));
}
}
function loadTweetIDs()
{
global $TweetIDs;
if(file_exists(constant("LIST_TWEET_IDS").".json"))
{
$TweetIDs_temp=json_decode(file_get_contents(constant("LIST_TWEET_IDS").".json", true));
foreach($TweetIDs_temp as $id)
$TweetIDs[$id] = 1;
}
}
function storeTweetIDs()
{
global $TweetIDs;
$TweetIDs_temp=array_keys($TweetIDs);
file_put_contents(constant("LIST_TWEET_IDS").".json", json_encode($TweetIDs_temp));
}
function loadKeywords()
{
global $KeyWords;
$KeyWords=(array) json_decode(file_get_contents(constant("LIST_KEYWORDS").".json", true));
}
?>