langstats_file_builder.php 4.1 KB

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