Skip to content
Snippets Groups Projects
Commit 1a651695 authored by KAPIAS NICOLAS's avatar KAPIAS NICOLAS
Browse files

feat: count word occurences in a file

parent 35de1a3d
Branches main
No related merge requests found
<?php
class MySplFileObject extends \SplFileObject {
private $words = array();
private $wordsCounted = false;
private $wordsSorted = false;
// A l'instanciation, ouvre un fichier, extrait les mots, compte leurs occurences et les trie.
public function __construct(string $filename, string $openingMode = "r", bool $sortingMode = false) {
parent::__construct($filename, $openingMode);
$this->fetchWordsFromFile();
$this->countWords();
$this->sortWords($sortingMode);
}
// Retourne le tableau de mots
public function getWords() {
return $this->words;
}
// Définit le tableau de mots
public function setWords($array) {
$this->words = $array;
}
// Extrait les mots du fichier et les stocke dans le tableau
public function fetchWordsFromFile() {
$word = "";
$words = array();
$this->rewind(); // Retourne au début du fichier
while (!$this->eof()) { // Tant que la fin du fichier n'est pas atteinte
$char = $this->fgetc(); // Récupère le caractère et fait avancer le pointeur
if ($char !== false && ord($char) >= 192) { // Si le caractère est encodé en UTF
$char .= $this->fgetc(); // Ajouter le prochain octet au caractère
$word .= $char; // Ajouter la chaine au mot
} elseif ($char !== false && ctype_alpha($char)) { // Sinon si le caractère est encodé en ASCII
$word .= $char; // Ajouter le caractère au mot
} elseif ($word !== "") { // Sinon si le caractère n'est pas alphabétique
$words[] = strtolower($word); // Ajouter le mot au tableau
$word = ""; // Vider le mot pour préparer le prochain
}
}
$this->setWords($words);
}
// Ajoute l'occurence des mots au tableau
public function countWords(){
$array = [];
foreach ($this->words as $word) {
$array[$word] = isset($array[$word]) ? $array[$word] + 1 : 1;
}
$this->setWords($array);
$this->wordsCounted = true;
}
// Trie le tableau par les valeurs (défaut) ou par les mots
public function sortWords($mode = false) {
if ($mode) {
ksort($this->words);
} else {
arsort($this->words);
}
$this->wordsSorted = true;
}
}
?>
\ No newline at end of file
<?php
foreach (glob("./classes/*.php") as $class) {
require_once $class;
}
?>
<html>
<head>
<title>M2GPI</title>
</head>
<body>
<h1>M2GPI2024 Algorithmie</h1>
<p>Count word occurences in a file.</p>
<form enctype="multipart/form-data" method="POST" action="./index.php">
<fieldset>
<legend>Order results by</legend>
<input type="radio" id="sortingModeOccurences" name="sortingMode" value="0" checked></input>
<label for="sortingModeOccurences">occurences</label>
<input type="radio" id="sortingModeWords" name="sortingMode" value="1"></input>
<label for="sortingModeWords">words</label>
</fieldset><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<input type="file" name="file"/><br>
<input type="submit" name="upload" value="Submit file"/>
</form>
<?php
try {
if (isset($_POST['upload'])) {
$upload = "./uploads/". basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $upload)) {
echo "<table><tr><th>Mots</th><th>Occurences</th></tr>";
$file = new MySplFileObject($upload, "r", boolval($_POST["sortingMode"]));
foreach ($file->getWords() as $word => $count) {
echo "<tr><td>" . $word . "</td><td>" . $count . "</td></tr>";
}
echo "</table>";
}
}
} catch (Exception $e) {
echo "Error : " . $e->getMessage();
}
?>
</body>
</html>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment