-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUncommon.php
executable file
·276 lines (223 loc) · 8.22 KB
/
Uncommon.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
// setup
namespace Nrwtaylor\StackAgentThing;
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
ini_set("allow_url_fopen", 1);
class Uncommon extends Agent
{
public $var = 'hello';
function init()
{
$this->minimum_word_length = 4;
$this->default_file =
"/home/jsae/2018/outputs/organizations/2020-orgfinder/cache/good/file-wsp-home.md";
}
// functions ----- ----- ----- -----
function run()
{
$this->doUncommon();
}
public function doUncommon($file = null)
{
$this->commonwordsUncommon();
// Bring file in to assess and create set of words to check
if ($file == null) {
$file = $this->default_file;
if (isset($this->file)) {
$file = $this->file;
}
}
if (!file_exists($file)) {
$this->response .= 'Did not find "' . $file . '". ';
return true;
}
$data = file_get_contents($file, true);
$data = strip_tags($data);
$words = [];
$arr = explode("\n", $data);
foreach ($arr as $key => $line) {
$tokens = explode(" ", $line);
foreach ($tokens as $j => $token) {
// Strip out numbers and spaces
$word_name = trim(strtolower($token));
$word_name = trim($word_name, ";");
$word_name = trim($word_name, ':');
$word_name = trim($word_name, ",");
$word_name = trim($word_name, ".");
$word_name = trim($word_name, " ");
$word_name = trim($word_name, "'");
$word_name = trim($word_name, '"');
$word_name = trim($word_name, '?');
$word_name = trim(preg_replace('/\t+/', '', $word_name));
// Reject strange tokens
if (substr_count($word_name, ".") >= 1) {
continue;
}
if (substr_count($word_name, '/') >= 2) {
continue;
}
if (substr_count($word_name, '{') >= 1) {
continue;
}
if (substr_count($word_name, '}') >= 1) {
continue;
}
if (substr_count($word_name, '(') >= 1) {
continue;
}
if (substr_count($word_name, ')') >= 1) {
continue;
}
if (substr_count($word_name, "='") >= 1) {
continue;
}
if (substr_count($word_name, ";") >= 1) {
continue;
}
if (substr_count($word_name, "_") >= 1) {
continue;
}
if (is_numeric($word_name)) {
continue;
}
if (strlen($word_name) <= $this->minimum_word_length) {
continue;
}
if (!isset($words[$word_name])) {
$words[$word_name] = 0;
}
$words[$word_name] += 1;
}
}
// Tag types of word
$this->brilltagger_agent = new Brilltagger($this->thing, "brilltagger");
$allowed_tags = ['NN', 'VBN', 'JJ'];
//echo "\n";
foreach ($words as $word_name => $word) {
$respond = $this->isUncommon($word_name);
if ($respond === true) {
$tags = $this->brilltagger_agent->tag($word_name);
if (!isset($tags[0])) {
continue;
}
$tag = $tags[0]['tag'];
if (in_array($tags[0]['tag'], $allowed_tags)) {
$this->response .= $word_name . " ";
}
}
}
//couple of lines of crude debugging code
//echo "\n";
// corncob_lowercase.txt
// ---
// Respond to caller
if ($this->agent_input == null) {
$array = ['miao', 'miaou', 'hiss', 'prrr', 'grrr'];
$k = array_rand($array);
$v = $array[$k];
$response = "UNCOMMON | " . strtolower($v) . ".";
// Save words
//file_put_contents("/home/jsae/2018/outputs/organizations/2020-orgfinder/words/passive_survivability.txt",$v);
$this->uncommon_message = $response; // mewsage?
} else {
$this->uncommon_message = $this->agent_input;
}
$this->response .= "Looked for uncommon words. ";
}
public function isUncommon($text = null)
{
// Identify uncommon words in the set
// based on length of dictionary used
if ($text == null) {
return;
}
foreach ($this->common_words as $i => $common_word) {
if (strtolower($text) == strtolower($common_word)) {
return false;
}
}
return true;
}
public function commonwordsUncommon()
{
//alternative dictionary lists for common words
//$data = file_get_contents($this->resource_path . 'corncob/corncob_lowercase.txt');
//$data = file_get_contents($this->resource_path . 'corncob/corncob_lowercase.txt');
$dict_path = '/home/jsae/Outputs/independence/word-sources/dict/';
$file = $dict_path . 'freq10000words-en-list.txt';
if (!file_exists($file)) {
$this->response .= 'Could not find dictionary, "' . $file . '". ';
return true;
}
$data = file_get_contents($file);
//$data = file_get_contents($dict_path . 'vocab-84669-en-list.txt');
$words = [];
$arr = explode("\n", $data);
foreach ($arr as $key => $line) {
$tokens = explode(" ", $line);
foreach ($tokens as $j => $token) {
$token = trim($token);
//$word_name = strtolower($token);
//if (!isset($words[$word_name])) {$words[$word_name] = 0;}
$words[] = $token;
}
}
$this->common_words = $words;
}
public function respondResponse()
{
$this->thing->flagGreen();
$this->thing_report["info"] =
"This is Uncommon. It sees uncommon things.";
$this->thing_report["help"] =
"This is about seeing what is uncommon. Try UNCOMMON <filename>";
//$this->thing_report['sms'] = $this->sms_message;
$this->thing_report['message'] = $this->sms_message;
$this->thing_report['txt'] = $this->sms_message;
$message_thing = new Message($this->thing, $this->thing_report);
$thing_report['info'] = $message_thing->thing_report['info'];
return $this->thing_report;
}
function makeSMS()
{
$this->node_list = ["uncommon" => ["cat", "dog"]];
//$sms = "Looked for uncommon words. ";
$sms = "UNCOMMON\n";
$sms .= $this->response;
$this->sms_message = $sms;
$this->thing_report['sms'] = $sms;
}
function makeChoices()
{
$this->thing->choice->Create('channel', $this->node_list, "uncommon");
$choices = $this->thing->choice->makeLinks('uncommon');
$this->thing_report['choices'] = $choices;
}
public function readSubject()
{
// Do not use $input = $this->input
// Because that will lose the capitalisation.
// Use this instead to resolve between the subject and the agent_input.
$input = $this->subject;
if (isset($this->agent_input)) {
$input = $this->agent_input;
}
// https://stackoverflow.com/questions/9598665/php-replace-first-occurrence-of-string-from-0th-position/9598905
$string = $input;
$str_pattern = 'uncommon';
$str_replacement = '';
if (strpos($string, $str_pattern) !== false) {
$occurrence = strpos($string, $str_pattern);
$filtered_input = substr_replace(
$string,
$str_replacement,
strpos($string, $str_pattern),
strlen($str_pattern)
);
}
$filtered_input = trim($filtered_input);
$this->file = $filtered_input;
}
}