201 lines
7.9 KiB
PHP
201 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Controllers\TwitterController;
|
|
use App\Models\Account;
|
|
use App\Models\Block;
|
|
use App\Models\Concour;
|
|
use App\Models\Contest;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Laravel\Dusk\Browser;
|
|
use Symfony\Component\BrowserKit\HttpBrowser;
|
|
use Symfony\Component\HttpClient\HttpClient;
|
|
use Illuminate\Http\Request;
|
|
use Throwable;
|
|
|
|
class BotController extends Controller
|
|
{
|
|
public function test($user,$contest){
|
|
$user = Account::find($user);
|
|
|
|
$contest = Contest::findOrFail($contest);
|
|
|
|
$text = $contest->description;
|
|
|
|
$tweetspecial = $this->getSpecialComment($text,$user,$contest->tweetid);
|
|
|
|
$tweetcomment = $this->getComment($text);
|
|
|
|
$tags = $this->getTags($text);
|
|
|
|
$hashtags = $this->getHashtags($text);
|
|
|
|
dd($tweetspecial,$tweetcomment,$tags,$hashtags);
|
|
}
|
|
|
|
private function getSpecialComment($text,$user,$id){
|
|
|
|
//On recherche si une reponse special est attendu
|
|
$word_special_comments = config('twitter.word_special_comment');
|
|
$combined_regex = implode('|', array_map('preg_quote', $word_special_comments, array_fill(0, count($word_special_comments), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
try {
|
|
$API = new APIController();
|
|
|
|
// On check les notifs
|
|
$texts = $API->getweets($user, $id);
|
|
|
|
// Filtrer les phrases vides
|
|
$texts = array_filter($texts, function($phrase) {
|
|
return !empty(trim($phrase)); // Ignore les chaînes vides ou contenant uniquement des espaces
|
|
});
|
|
|
|
// Initialiser un tableau pour stocker les occurrences des phrases
|
|
$occurrences = [];
|
|
|
|
foreach ($texts as $index1 => $phrase1) {
|
|
foreach ($texts as $index2 => $phrase2) {
|
|
if ($index1 !== $index2) {
|
|
$similarity = $this->cosineSimilarity($phrase1, $phrase2);
|
|
// Vous pouvez ajuster le seuil de similarité en fonction de vos besoins
|
|
if ($similarity > 0.5) {
|
|
// Incrémenter le compteur pour les deux phrases
|
|
$occurrences[$index1] = isset($occurrences[$index1]) ? $occurrences[$index1] + 1 : 1;
|
|
$occurrences[$index2] = isset($occurrences[$index2]) ? $occurrences[$index2] + 1 : 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Trouver l'index de la phrase avec le comptage le plus élevé
|
|
$indexPhrasePlusFrequente = (!empty($occurrences)) ? array_search(max($occurrences), $occurrences) : null;
|
|
|
|
// Récupérer la phrase avec le comptage le plus élevé
|
|
$phrasePlusFrequente = ($indexPhrasePlusFrequente !== null) ? $texts[$indexPhrasePlusFrequente] : null;
|
|
|
|
|
|
if ($phrasePlusFrequente != null) {
|
|
// Supprimer les hashtags
|
|
$phrasePlusFrequente = preg_replace('/#\w+\s?/', '', $phrasePlusFrequente);
|
|
|
|
// Supprimer les tags
|
|
$phrasePlusFrequente = preg_replace('/@\w+\s?/', '', $phrasePlusFrequente);
|
|
|
|
// Supprimer les emojis
|
|
$phrasePlusFrequente = $this->remove_emojis($phrasePlusFrequente);
|
|
|
|
return $phrasePlusFrequente;
|
|
} else {
|
|
$tweetcomments = config('twitter.sentence_for_random_comment');
|
|
return Arr::random($tweetcomments);
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
$tweetcomments = config('twitter.sentence_for_random_comment');
|
|
return Arr::random($tweetcomments);
|
|
}
|
|
}
|
|
}
|
|
private function getComment($text){
|
|
//On recherche si un commentaire est attendu
|
|
$word_comments = config('twitter.word_comment');
|
|
$combined_regex = implode('|', array_map('preg_quote', $word_comments, array_fill(0, count($word_comments), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
return true;
|
|
}
|
|
}
|
|
private function getTags($text){
|
|
//On recherche si ça demande un tag
|
|
$word_tags = config('twitter.word_tag');
|
|
$combined_regex = implode('|', array_map('preg_quote', $word_tags, array_fill(0, count($word_tags), '/')));
|
|
$tags = collect(config('twitter.tags'));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
|
|
//On recherche si ça demande 1 tag
|
|
$one_people_lists = config('twitter.one_people_list');
|
|
$combined_regex = implode('|', array_map('preg_quote', $one_people_lists, array_fill(0, count($one_people_lists), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
$values = $tags->random(1);
|
|
return '@' . $values[0];
|
|
}
|
|
|
|
//On recherche si ça demande 2 tags
|
|
$two_people_lists = config('twitter.two_people_list');
|
|
$combined_regex = implode('|', array_map('preg_quote', $two_people_lists, array_fill(0, count($two_people_lists), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
$values = $tags->random(2);
|
|
return '@' . $values[0]. ' @' . $values[1];
|
|
}
|
|
|
|
//On recherche si ça demande 3 tags ou plus
|
|
$three_or_more_people_lists = config('twitter.three_or_more_people_list');
|
|
$combined_regex = implode('|', array_map('preg_quote', $three_or_more_people_lists, array_fill(0, count($three_or_more_people_lists), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
$values = $tags->random(3);
|
|
return '@' . $values[0]. ' @' . $values[1]. ' @' . $values[2];
|
|
}
|
|
}
|
|
}
|
|
private function getHashtags($text){
|
|
preg_match_all("/#[a-zA-Z0-9]+/", $text, $hashtags);
|
|
|
|
if (isset($hashtags[0])) {
|
|
$hashtags = array_unique($hashtags[0]);
|
|
$blacklist = config('twitter.hashtag_to_blacklist');
|
|
|
|
// Comparer les deux tableaux et supprimer les correspondances du premier tableau
|
|
$resultLower = array_udiff($hashtags, $blacklist, 'strcasecmp');
|
|
|
|
// Construire la chaîne finale des hashtags restants
|
|
$final = implode(" ", $resultLower);
|
|
|
|
return $final;
|
|
}
|
|
else{
|
|
return '';
|
|
}
|
|
}
|
|
private function cosineSimilarity($text1, $text2)
|
|
{
|
|
$words1 = str_word_count(strtolower($text1), 1);
|
|
$words2 = str_word_count(strtolower($text2), 1);
|
|
|
|
$allWords = array_unique(array_merge($words1, $words2));
|
|
$vector1 = $vector2 = [];
|
|
|
|
// Construire les vecteurs avec les fréquences des mots
|
|
foreach ($allWords as $word) {
|
|
$vector1[] = in_array($word, $words1) ? 1 : 0;
|
|
$vector2[] = in_array($word, $words2) ? 1 : 0;
|
|
}
|
|
|
|
$dotProduct = 0;
|
|
|
|
// Calculer le produit scalaire des vecteurs
|
|
for ($i = 0; $i < count($allWords); $i++) {
|
|
$dotProduct += $vector1[$i] * $vector2[$i];
|
|
}
|
|
|
|
$magnitude1 = sqrt(array_sum($vector1));
|
|
$magnitude2 = sqrt(array_sum($vector2));
|
|
|
|
if ($magnitude1 * $magnitude2 == 0) {
|
|
return 0; // Pour éviter une division par zéro
|
|
}
|
|
|
|
return $dotProduct / ($magnitude1 * $magnitude2);
|
|
}
|
|
private function remove_emojis($string)
|
|
{
|
|
// Match all emojis (including extended ones)
|
|
$regex_emojis = '/[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{1F900}-\x{1F9FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}]/u';
|
|
$clear_string = preg_replace($regex_emojis, '', $string);
|
|
|
|
return $clear_string;
|
|
}
|
|
|
|
} |