Advertisement
KosIvantsov

merge_split

Sep 8th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 8.26 KB | None | 0 0
  1. /** :name=  Merge or split segments :description= \
  2.  *          Merge current segment with the next or split it at the selection
  3.  *
  4.  * @author  Yu Tang, Dimitry Prihodko, Kos Ivantsov
  5.  * @date    2016-09-16
  6.  * @version 0.4.6 (for OmegaT 3.x and 4.x)
  7.  */
  8. import org.apache.commons.lang.WordUtils
  9. import org.omegat.core.segmentation.MapRule
  10. import org.omegat.core.segmentation.Rule
  11. import org.omegat.core.segmentation.SRX
  12. import org.omegat.util.Language
  13. import org.omegat.util.OStrings
  14. import org.omegat.util.StaticUtils
  15. import org.omegat.util.StringUtil
  16. import javax.swing.JOptionPane
  17.  
  18. import static javax.swing.JOptionPane.*
  19. import static org.omegat.gui.main.ProjectUICommands.projectReload
  20. //import static org.omegat.util.StaticUtils.escapeNonRegex
  21. import static org.omegat.util.StaticUtils.*
  22. import static org.omegat.util.StringUtil.*
  23.  
  24. utils = (StringUtil.getMethods().toString().findAll("makeValidXML")) ? StringUtil : StaticUtils
  25.  
  26. //check if we have selection at the end of the current source
  27. def entry = editor.currentEntry
  28. def src = entry.srcText
  29. def split
  30. if (editor.selectedText) {
  31.     sel = editor.selectedText
  32.     split = src.endsWith(sel) ? true : false
  33.     if (split) {
  34.         console.println(res.getString("endSelected"))
  35.     }else{
  36.         console.println(res.getString("wrongSelected"))
  37.     }
  38. }
  39.  
  40. split = split ? true : false
  41.  
  42. initializeScript()
  43.  
  44. // check if requirements are met
  45. if (! isReadyForNewRule()) {
  46.     return
  47. }
  48.  
  49. //get fragments to split or merge
  50. String beforeBreak = split ? src - sel : entry.srcText
  51. String afterBreak = split ? sel : entry.key.next
  52.  
  53.  
  54. // exists check for the MappingRule
  55. Language srcLang = project.projectProperties.sourceLanguage
  56. def mapRule = project.projectProperties.projectSRX.findMappingRule(srcLang)
  57. if (! mapRule) {
  58.     message = res.getString("noMappingRule") + res.getString("terminating")
  59.     message.alert()
  60.     return
  61. }
  62.  
  63. // show confirm dialog
  64. def separator = ""
  65. if (! srcLang.isCJK()) {
  66.     separator = " "
  67. }
  68. String message = split ?
  69. WordUtils.wrap("$beforeBreak\n\n$afterBreak\n\n", 180) + res.getString("proceed") :
  70. WordUtils.wrap("$beforeBreak$separator$afterBreak\n\n", 180) + res.getString("proceed")
  71. if (message.confirm() != 0) {
  72.     console.clear()
  73.     console.println(res.getString("noNewRule"))
  74.     return
  75. }
  76.  
  77. // create new rule
  78. boolean breakRule = split ? true : false // Exception
  79. beforeBreak = beforeBreak.toRegexPattern()
  80. afterBreak = afterBreak.toRegexPattern()
  81. if (! split && ! srcLang.isCJK()) {
  82.     afterBreak = /\s?/ + afterBreak
  83. }
  84. def rule = new Rule(breakRule, beforeBreak, afterBreak)
  85.  
  86. // check if there's a conficting split rule
  87. def conflict = mapRule.rules.find {
  88.     it.beforebreak.trim() == beforeBreak.trim() && it.afterbreak.replaceAll(/^\\s/, '').trim() == afterBreak.replaceAll(/^\\s/, '').trim()
  89. }
  90. if (conflict) {
  91.     mapRule.rules.remove(conflict)
  92. }
  93.  
  94.  
  95. // exists check for the new rule
  96. def found = mapRule.rules.find {
  97.     it.beforebreak == beforeBreak && it.afterbreak == afterBreak
  98. }
  99. if (found) {
  100.     message = res.getString("ruleExists") + res.getString("terminating")
  101.     message.alert()
  102.     return
  103. }
  104.  
  105. // register new rule to the segmentation
  106. mapRule.rules[0..<0] = rule // Appends a new rule to the head of List.
  107.  
  108. // reload the project
  109. if (showConfirmDialog(mainWindow, OStrings.getString("MW_REOPEN_QUESTION"),
  110.     OStrings.getString("MW_REOPEN_TITLE"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
  111.     projectReload()
  112.     }else{
  113.     console.print(res.getString("noReload"))
  114.     return
  115. }
  116.  
  117. // fin
  118. console.println(res.getString("newSegmentationActive") + "(${new Date().timeString})")
  119.  
  120. // ******************************************************
  121. // methods
  122. // ******************************************************
  123.     public static String escapeNonRegex(String text) {
  124.         return escapeNonRegex(text, true);
  125.     }
  126.  
  127.     /**
  128.      * Escapes the passed string for use in regex matching, so special regex
  129.      * characters are interpreted as normal characters during regex searches.
  130.      *
  131.      * This is done by prepending a backslash before each occurrence of the
  132.      * following characters: \^.+[]{}()&|-:=!<>
  133.      *
  134.      * If the parameter escapeWildcards is true, asterisks (*) and questions
  135.      * marks (?) will also be escaped. If false, these will be converted to
  136.      * regex tokens (* ->
  137.      *
  138.      * @param text
  139.      *            The text to escape
  140.      * @param escapeWildcards
  141.      *            If true, asterisks and question marks are also escaped. If
  142.      *            false, these are converted to there regex equivalents.
  143.      *
  144.      * @return The escaped text
  145.      */
  146.     public static String escapeNonRegex(String text, boolean escapeWildcards) {
  147.         // handle backslash
  148.         text = text.replaceAll("\\\\", "\\\\\\\\"); // yes, that's the correct
  149.                                                     // nr of backslashes
  150.  
  151.         // String escape = "^.*+[]{}()&|-:=?!<>";
  152.         for (char c : "^.+[]{}()&|-:=!<>".toCharArray()) {
  153.             text = text.replaceAll("\\" + c, "\\\\" + c);
  154.         }
  155.  
  156.         // handle "wildcard characters" ? and * (only if requested)
  157.         // do this last, or the additional period (.) will cause trouble
  158.         if (escapeWildcards) {
  159.             // simply escape * and ?
  160.             text = text.replaceAll("\\?", "\\\\?");
  161.             text = text.replaceAll("\\*", "\\\\*");
  162.         } else {
  163.             // convert * (0 or more characters) and ? (1 character)
  164.             // to their regex equivalents (\S* and \S? respectively)
  165.             // text = text.replaceAll("\\?", "\\S?"); // do ? first, or * will
  166.             // be converted twice
  167.             // text = text.replaceAll("\\*", "\\S*");
  168.             // The above lines were not working:
  169.             // [ 1680081 ] Search: simple wilcards do not work
  170.             // The following correction was contributed by Tiago Saboga
  171.             text = text.replaceAll("\\?", "\\\\S"); // do ? first, or * will be
  172.                                                     // converted twice
  173.             text = text.replaceAll("\\*", "\\\\S*");
  174.         }
  175.  
  176.         return text;
  177.     }
  178.  
  179. boolean isReadyForNewRule() {
  180.     if (! project.isProjectLoaded()) {
  181.         message = res.getString("noProjectOpen") + res.getString("terminating")
  182.         return message.alert()
  183.     }
  184.  
  185.     def srx = project.projectProperties.projectSRX
  186.     if (! srx) {
  187.         message = res.getString("noProjectSegmentation") + res.getString("terminating")
  188.         return message.alert()
  189.     }
  190.  
  191.     def entry = editor.currentEntry
  192.     def src = entry.srcText
  193.     def split
  194.     def sel
  195.     if (editor.selectedText) {
  196.         sel = editor.selectedText
  197.         split = src.endsWith(sel) ? true : false
  198.     }
  199.     split = split ? true :false
  200.  
  201.     if (! split && (! entry.srcText || ! entry.key.next)) {
  202.         message = res.getString("noMerge") + res.getString("terminating")
  203.         return message.alert()
  204.     }
  205.    
  206.     if ( split && (src == sel) ) {
  207.         message = res.getString("noSplit") + res.getString("terminating")
  208.         return message.alert()
  209.     }
  210.  
  211.     // OK
  212.     true
  213. }
  214.  
  215. void initializeScript() {
  216.  
  217.     def entry = editor.currentEntry
  218.     def src = entry.srcText
  219.     def split
  220.     if (editor.selectedText) {
  221.         def sel = editor.selectedText
  222.         split = src.endsWith(sel) ? true : false
  223.     }
  224.  
  225.     // String class
  226.     String.metaClass.toXML = { ->
  227.         utils.makeValidXML(delegate as String)
  228.     }
  229.     String.metaClass.toRegexPattern = { ->
  230.         escapeNonRegex(delegate as String)
  231.     }
  232.     String.metaClass.alert = { ->
  233.         showMessageDialog null, delegate, split ? res.getString("splitTitle") : res.getString("mergeTitle"), INFORMATION_MESSAGE
  234.         false
  235.     }
  236.     String.metaClass.confirm = { ->
  237.         showConfirmDialog null, delegate, split ? res.getString("splitMessage") : res.getString("mergeMessage"), YES_NO_OPTION
  238.     }
  239.  
  240.     // SRX class
  241.     SRX.metaClass.findMappingRule = { Language srclang ->
  242.         delegate.mappingRules.find { MapRule maprule ->
  243.             maprule.compiledPattern.matcher(srclang.language).matches()
  244.         }
  245.     }
  246.  
  247.     // Language class
  248.     Language.metaClass.isCJK = { ->
  249.         delegate.languageCode.toUpperCase(Locale.ENGLISH) in ['ZH', 'JA', 'KO']
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement