langstats_file_builder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. $language_file = array(
  16. 'admin',
  17. 'create_course',
  18. 'document',
  19. 'exercice',
  20. 'gradebook',
  21. 'help',
  22. 'hotspot',
  23. 'install',
  24. 'learnpath',
  25. 'registration',
  26. 'reservation',
  27. 'scormdocument',
  28. 'shibboleth',
  29. 'survey',
  30. 'tracking',
  31. 'trad4all',
  32. 'wiki',
  33. );
  34. require_once '../../inc/global.inc.php';
  35. require_once 'langstats.class.php';
  36. global $_configuration;
  37. $_configuration['language_measure_frequency'] = 0;
  38. $langstats = new langstats();
  39. $orig_lang = 'english';
  40. /**
  41. * Init
  42. */
  43. $words_limit = 10000; //change this if you want more words
  44. $terms_limit = 3000; //change this if you think you'll need more terms
  45. $terms = $langstats->get_popular_terms($terms_limit);
  46. $words_counter = 0;
  47. $i = 0;
  48. $terms_in_limit = array();
  49. $lang_dir = api_get_path(SYS_LANG_PATH);
  50. $arch_dir = api_get_path(SYS_ARCHIVE_PATH);
  51. /**
  52. * Code run
  53. */
  54. foreach ($terms as $row) {
  55. if ($words_counter > 10000) { break; }
  56. $words = str_word_count(get_lang($row['term_name'],null,$orig_lang));
  57. $words_counter += $words;
  58. $terms_in_limit[$row['term_name']] = $i;
  59. //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";
  60. //if ($words_counter%1000 >= 0) {
  61. //echo "Reached $words_counter words at term $i (".$row['term_name']." used ".$row['term_count']." times)...<br />\n";
  62. //}
  63. $i++;
  64. }
  65. //echo $words_counter.'<br />';
  66. echo "Reached ".count($terms_in_limit)." terms for the $words_counter most-used words<br /><br />\n";
  67. echo "Scanning English files, trying to find these terms...<br />\n";
  68. if (!is_dir($arch_dir.'/langstats')) {
  69. mkdir($arch_dir.'/langstats');
  70. mkdir($arch_dir.'/langstats/'.$orig_lang);
  71. }
  72. $list_files = scandir($lang_dir.'/'.$orig_lang);
  73. $j = 1;
  74. $terms_found = array();
  75. $words_found = 0;
  76. $global_var = array(); //keep the combination of all vars
  77. $terms_in_limit = array_flip($terms_in_limit);
  78. foreach ($list_files as $file) {
  79. if (substr($file,0,1) == '.') {continue;}
  80. //echo "'".substr($file,0,-8)."',<br />"; //print in a PHP array format
  81. $vars = file($lang_dir.'/'.$orig_lang.'/'.$file);
  82. $local_var = array();
  83. $file_string = '<?php'."\n";
  84. foreach ($vars as $line) {
  85. $var = array();
  86. $res = preg_match('/^(\$\w*)/',$line,$var);
  87. if ($res>0) {
  88. //echo $var[1]."<br />";
  89. if (in_array(substr($var[1],1),$terms_in_limit)) {
  90. //echo "Var ".$var[1]." was in the limit<br />";
  91. $local_var[$var[1]] = $line;
  92. $file_string .= $line;
  93. $terms_found[] = substr($var[1],1); //e.g. store Tools
  94. $words_found += str_word_count(get_lang($var[1],null,$orig_lang));
  95. } elseif (in_array(substr($var[1],5),$terms_in_limit)) {
  96. //echo "Var ".$var[1]." was in the limit<br />";
  97. $local_var[$var[1]] = $line;
  98. $file_string .= $line;
  99. $terms_found[] = substr($var[1],5); //e.g. store langTools
  100. $words_found += str_word_count(get_lang(substr($var[1],5),null,$orig_lang));
  101. } //else do not care
  102. }
  103. }
  104. echo "Writing ".$arch_dir.'/langstats/'.$orig_lang.'/'.$file."<br />\n";
  105. file_put_contents($arch_dir.'/langstats/'.$orig_lang.'/'.$file,$file_string);
  106. $global_var += $local_var;
  107. };
  108. $terms_diff = count($global_var)-count($terms_in_limit);
  109. 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 />";
  110. /**
  111. * Display results
  112. */
  113. echo "Difference between filtered and found in English:<br />";
  114. //print_r($terms_found);
  115. echo "<pre>".print_r(array_diff($terms_in_limit,$terms_found),1)."</pre>";
  116. echo "#";