briacp

Word Count MS-Word style

Mar 23rd, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.62 KB | None | 0 0
  1. //:name=Word Count MS-Word style :description=
  2. import org.omegat.core.data.ProtectedPart
  3.  
  4. console.println("Word Count MSWord\n");
  5.  
  6. def total_source_word_count = 0;
  7. def total_target_word_count = 0;
  8.  
  9. def files = project.projectFiles;
  10.  
  11. for (i in 0 ..< files.size())
  12. {
  13.     fi = files[i];
  14.  
  15.   def source_word_count = 0;
  16.   def target_word_count = 0;
  17.  
  18.     console.println("== " + fi.filePath);
  19.     for (j in 0 ..< fi.entries.size())
  20.     {
  21.       ste = fi.entries[j];
  22.       source = ste.getSrcText();
  23.  
  24.       source_word_count += count_segment(source);
  25.  
  26.       if (project.getTranslationInfo(ste)) {
  27.  
  28.         def targ = project.getTranslationInfo(ste).translation
  29.         if (targ == null) continue;
  30.         for (ProtectedPart pp : ste.getProtectedParts()) {
  31.           targ = targ.replace(pp.getTextInSourceSegment(), pp.getReplacementWordsCountCalculation())
  32.         }
  33.  
  34.         target_word_count += count_segment(targ);
  35.       }
  36.  
  37.     }
  38.  
  39.   total_source_word_count += source_word_count;
  40.   total_target_word_count += target_word_count;
  41.   console.println("Source\t" + source_word_count);
  42.   console.println("Target\t" + target_word_count);
  43.   console.println("");
  44. }
  45.  
  46. console.println("==================================");
  47. console.println("Total");
  48. console.println("Source\t" + total_source_word_count);
  49. console.println("Target\t" + total_target_word_count);
  50.  
  51. def count_segment (s) {
  52.   if (s == null) return 0;
  53.  
  54.   def spaces = /[\u00a0|\p{Blank}|\p{Space}]+/;
  55.   def w = s.trim().replaceAll(spaces, " ").split(spaces);
  56.   def c = w.length;
  57.  
  58.   //console.print(Arrays.toString(w) + "\t" + c + "\n");
  59.  
  60.   return c;
  61. }
Add Comment
Please, Sign In to add comment