我有超過5000個文本文件的大集合,有超過200,000個單詞。問題是,當我嘗試將整個集合組合到一個數組中時,爲了查找集合中的唯一字,沒有顯示輸出(這是由於數組的大小很大)。下面的一段代碼對於小號沒有問題。的集合,例如30個文件,但不能在非常大的集合上操作。幫我解決這個問題。由於如何在PHP中處理相對較大的數組?
<?php
ini_set('memory_limit', '1024M');
$directory = "archive/";
$dir = opendir($directory);
$file_array = array();
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if ($type == 'file') {
$contents = file_get_contents($filename);
$text = preg_replace('/\s+/', ' ', $contents);
$text = preg_replace('/[^A-Za-z0-9\-\n ]/', '', $text);
$text = explode(" ", $text);
$text = array_map('strtolower', $text);
$stopwords = array("a", "an", "and", "are", "as", "at", "be", "by", "for", "is", "to");
$text = (array_diff($text,$stopwords));
$file_array = array_merge($file_array, $text);
}
}
closedir($dir);
$total_word_count = count($file_array);
$unique_array = array_unique($file_array);
$unique_word_count = count($unique_array);
echo "Total Words: " . $total_word_count."<br>";
echo "Unique Words: " . $unique_word_count;
?>
的文本文件數據集可以在這裏找到:https://archive.ics.uci.edu/ml/machine-learning-databases/00217/C50.zip
你有沒有試圖使內存限制高? – putvande
我有2GB的內存。 – user3814982
您是否嘗試過使用XML文件或CSV? – M98