AutospamPretrainNonSpamPipeline.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Jobs\AutospamPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Util\Lexer\Classifier;
  10. use App\AccountInterstitial;
  11. use App\Profile;
  12. use App\Status;
  13. use Illuminate\Support\Facades\Storage;
  14. use App\Services\AutospamService;
  15. class AutospamPretrainNonSpamPipeline implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. public $classifier;
  19. public $accounts;
  20. /**
  21. * Create a new job instance.
  22. */
  23. public function __construct($accounts)
  24. {
  25. $this->accounts = $accounts;
  26. $this->classifier = new Classifier();
  27. }
  28. /**
  29. * Execute the job.
  30. */
  31. public function handle(): void
  32. {
  33. $classifier = $this->classifier;
  34. $accounts = $this->accounts;
  35. foreach($accounts as $acct) {
  36. Status::whereNotNull('caption')
  37. ->whereScope('public')
  38. ->whereProfileId($acct->id)
  39. ->inRandomOrder()
  40. ->take(400)
  41. ->pluck('caption')
  42. ->each(function($c) use ($classifier) {
  43. $classifier->learn($c, 'ham');
  44. });
  45. }
  46. Storage::put(AutospamService::MODEL_HAM_PATH, $classifier->export());
  47. AutospamUpdateCachedDataPipeline::dispatch()->delay(5);
  48. }
  49. }