Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Created August 18, 2018 14:41
Show Gist options
  • Save ammarfaizi2/646fdba0179848c75f5cdac13fa0d0d9 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/646fdba0179848c75f5cdac13fa0d0d9 to your computer and use it in GitHub Desktop.
<?php
error_reporting(0);
class WorldCloud
{
protected $stopword;
protected $word;
protected $n;
public function __construct($word, $n)
{
$this->word = $word;
$this->n = $n;
}
public function stopword()
{
$stopwordRaw = fopen("stopwordbahasa.csv", "r");
$stopword = [];
while (($line = fgets($stopwordRaw)) !== false) {
$stopword[] = trim($line);
}
fclose($stopwordRaw);
return $stopword;
}
public function process()
{
$stopword = $this->stopword();
$words = explode(" ", $this->word);
$results = array_values(array_diff($words, $stopword));
$strings = [];
$start = 0 - $this->n;
$total = count($results);
foreach ($results as $key => $string) {
$output = [];
for ($i = 0; $i < $this->n; $i++) {
$output[] = $results[$start + $this->n];
$start++;
}
$start--;
$strings[] = implode(' ', $output);
}
array_pop($strings);
$filtered = array_filter($strings, function ($element) {
return is_string($element) && '' !== trim($element);
});
return $filtered;
}
}
$sentences = [
"bupati simeulue doyan makan pecel",
"paman pergi ke rumah ahli biologi bersama jokowi",
"kpu akan langsung klarifikasi ke parpol bila ada laporan soal dcs",
"bendera merah putih fatmawati dari disobek ajudan bung karno hingga ditolak pak harto",
];
for ($i=1; $i <= 4; $i++) {
print "\n n = {$i} \n";
foreach ($sentences as $sentence) {
print "Sentence: ".$sentence."\n";
$wc = new WorldCloud($sentence, $i);
print "Result: ".json_encode($wc->process())."\n\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment