langstats_file_builder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script generates a directory based on the English language variables
  5. * but only composed of the 10,000 (can be configured) most frequent words
  6. * used in Chamilo. This implies first using the langstats.php script, which
  7. * in turn implies configuring an additional variable in configuration.php
  8. * (see langstats.php for more info).
  9. * When running the language_builder, please make sure this parameter is
  10. * set to 0 in the configuration.php file, otherwise it will take *ages*.
  11. */
  12. /**
  13. * Requires
  14. */
  15. require_once '../../inc/global.inc.php';
  16. require_once 'langstats.class.php';
  17. global $_configuration;
  18. $_configuration['language_measure_frequency'] = 0;
  19. $langstats = new langstats();
  20. $orig_lang = 'english';
  21. /**
  22. * Init
  23. */
  24. $words_limit = 10000; //change this if you want more words
  25. $terms_limit = 3000; //change this if you think you'll need more terms
  26. $terms = $langstats->get_popular_terms($terms_limit);
  27. $words_counter = 0;
  28. $i = 0;
  29. $terms_in_limit = array();
  30. $lang_dir = api_get_path(SYS_LANG_PATH);
  31. $arch_dir = api_get_path(SYS_ARCHIVE_PATH);
  32. /**
  33. * Code run
  34. */
  35. foreach ($terms as $row) {
  36. if ($words_counter > 10000) { break; }
  37. $words = str_word_count(get_lang($row['term_name'], null, $orig_lang));
  38. $words_counter += $words;
  39. $terms_in_limit[$row['term_name']] = $i;
  40. //echo "Term <b>".$row['term_name']."</b> is <b>'".get_lang($row['term_name'],null,$orig_lang)."'</b> which means $words words<br /><br />\n";
  41. //if ($words_counter%1000 >= 0) {
  42. //echo "Reached $words_counter words at term $i (".$row['term_name']." used ".$row['term_count']." times)...<br />\n";
  43. //}
  44. $i++;
  45. }
  46. //echo $words_counter.'<br />';
  47. echo "Reached ".count($terms_in_limit)." terms for the $words_counter most-used words<br /><br />\n";
  48. echo "Scanning English files, trying to find these terms...<br />\n";
  49. if (!is_dir($arch_dir.'/langstats')) {
  50. mkdir($arch_dir.'/langstats');
  51. mkdir($arch_dir.'/langstats/'.$orig_lang);
  52. }
  53. $list_files = scandir($lang_dir.'/'.$orig_lang);
  54. $j = 1;
  55. $terms_found = array();
  56. $words_found = 0;
  57. $global_var = array(); //keep the combination of all vars
  58. $terms_in_limit = array_flip($terms_in_limit);
  59. foreach ($list_files as $file) {
  60. if (substr($file, 0, 1) == '.') {continue; }
  61. //echo "'".substr($file,0,-8)."',<br />"; //print in a PHP array format
  62. $vars = file($lang_dir.'/'.$orig_lang.'/'.$file);
  63. $local_var = array();
  64. $file_string = '<?php'."\n";
  65. foreach ($vars as $line) {
  66. $var = array();
  67. $res = preg_match('/^(\$\w*)/', $line, $var);
  68. if ($res > 0) {
  69. //echo $var[1]."<br />";
  70. if (in_array(substr($var[1], 1), $terms_in_limit)) {
  71. //echo "Var ".$var[1]." was in the limit<br />";
  72. $local_var[$var[1]] = $line;
  73. $file_string .= $line;
  74. $terms_found[] = substr($var[1], 1); //e.g. store Tools
  75. $words_found += str_word_count(get_lang($var[1], null, $orig_lang));
  76. } elseif (in_array(substr($var[1], 5), $terms_in_limit)) {
  77. //echo "Var ".$var[1]." was in the limit<br />";
  78. $local_var[$var[1]] = $line;
  79. $file_string .= $line;
  80. $terms_found[] = substr($var[1], 5); //e.g. store langTools
  81. $words_found += str_word_count(get_lang(substr($var[1], 5), null, $orig_lang));
  82. } //else do not care
  83. }
  84. }
  85. echo "Writing ".$arch_dir.'/langstats/'.$orig_lang.'/'.$file."<br />\n";
  86. file_put_contents($arch_dir.'/langstats/'.$orig_lang.'/'.$file, $file_string);
  87. $global_var += $local_var;
  88. };
  89. $terms_diff = count($global_var) - count($terms_in_limit);
  90. echo count($global_var)." terms found in English files (summing up to $words_found words). Some terms ($terms_diff in this case) might have appeared in two different files<br />";
  91. /**
  92. * Display results
  93. */
  94. echo "Difference between filtered and found in English:<br />";
  95. //print_r($terms_found);
  96. echo "<pre>".print_r(array_diff($terms_in_limit, $terms_found), 1)."</pre>";
  97. echo "#";