Advertisement
KosIvantsov

merge

Jan 15th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. /** :name=Merge or split segments :description=Merge current segment with the next or split it at the selection
  2. *
  3. * @author Yu Tang, Dimitry Prihodko, Kos Ivantsov
  4. * @date 2016-01-12
  5. * @version 0.3
  6. */
  7. import org.apache.commons.lang.WordUtils
  8. import org.omegat.core.segmentation.MapRule
  9. import org.omegat.core.segmentation.Rule
  10. import org.omegat.core.segmentation.SRX
  11. import org.omegat.util.Language
  12. import org.omegat.util.OStrings
  13. import javax.swing.JOptionPane
  14.  
  15. import static javax.swing.JOptionPane.*
  16. import static org.omegat.gui.main.ProjectUICommands.projectReload
  17. import static org.omegat.util.StaticUtils.escapeNonRegex
  18. import static org.omegat.util.StringUtil.makeValidXML
  19.  
  20. //check if we have selection at the end of the current source
  21. def entry = editor.currentEntry
  22. def src = entry.srcText
  23. def split
  24. if (editor.selectedText) {
  25. sel = editor.selectedText
  26. split = src.endsWith(sel) ? true : false
  27. if (split) {
  28. console.println(res.getString("endSelected"))
  29. }else{
  30. console.println(res.getString("wrongSelected"))
  31. }
  32. }
  33.  
  34. split = split ? true : false
  35.  
  36. initializeScript()
  37.  
  38. // check if requirements are met
  39. if (! isReadyForNewRule()) {
  40. return
  41. }
  42.  
  43. //get fragments to split or merge
  44. String beforeBreak = split ? src - sel : entry.srcText
  45. String afterBreak = split ? sel : entry.key.next
  46.  
  47.  
  48. // exists check for the MappingRule
  49. Language srcLang = project.projectProperties.sourceLanguage
  50. def mapRule = project.projectProperties.projectSRX.findMappingRule(srcLang)
  51. if (! mapRule) {
  52. message = res.getString("noMappingRule") + res.getString("terminating")
  53. message.alert()
  54. return
  55. }
  56.  
  57. // show confirm dialog
  58. def separator = ""
  59. if (! srcLang.isCJK()) {
  60. separator = " "
  61. }
  62. String message = split ?
  63. WordUtils.wrap("$beforeBreak\n$afterBreak\n\n", 180) + res.getString("proceed") :
  64. WordUtils.wrap("$beforeBreak$separator$afterBreak\n\n", 180) + res.getString("proceed")
  65. if (message.confirm() == NO_OPTION) {
  66. console.clear()
  67. console.println(res.getString("noNewRule"))
  68. return
  69. }
  70.  
  71. // create new rule
  72. boolean breakRule = split ? true : false // Exception
  73. beforeBreak = beforeBreak.toRegexPattern()
  74. afterBreak = afterBreak.toRegexPattern()
  75. if (! split && ! srcLang.isCJK()) {
  76. afterBreak = /\s/ + afterBreak
  77. }
  78. def rule = new Rule(breakRule, beforeBreak, afterBreak)
  79.  
  80. // check if there's a conficting split rule
  81. def conflict = mapRule.rules.find {
  82. it.beforebreak.trim() == beforeBreak.trim() && it.afterbreak.replaceAll(/^\\s/, '').trim() == afterBreak.replaceAll(/^\\s/, '').trim()
  83. }
  84. if (conflict) {
  85. mapRule.rules.remove(conflict)
  86. }
  87.  
  88.  
  89. // exists check for the new rule
  90. def found = mapRule.rules.find {
  91. it.beforebreak == beforeBreak && it.afterbreak == afterBreak
  92. }
  93. if (found) {
  94. message = res.getString("ruleExists") + res.getString("terminating")
  95. message.alert()
  96. return
  97. }
  98.  
  99. // register new rule to the segmentation
  100. mapRule.rules[0..<0] = rule // Appends a new rule to the head of List.
  101.  
  102. // reload the project
  103. if (showConfirmDialog(mainWindow, OStrings.getString("MW_REOPEN_QUESTION"),
  104. OStrings.getString("MW_REOPEN_TITLE"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
  105. projectReload()
  106. }else{
  107. console.print(res.getString("noReload"))
  108. return
  109. }
  110.  
  111. // fin
  112. console.println(res.getString("newSegmentationActive") + "(${new Date().timeString})")
  113.  
  114. // ******************************************************
  115. // methods
  116. // ******************************************************
  117. boolean isReadyForNewRule() {
  118. if (! project.isProjectLoaded()) {
  119. message = res.getString("noProjectOpen") + res.getString("terminating")
  120. return message.alert()
  121. }
  122.  
  123. def srx = project.projectProperties.projectSRX
  124. if (! srx) {
  125. message = res.getString("noProjectSegmentation") + res.getString("terminating")
  126. return message.alert()
  127. }
  128.  
  129. def entry = editor.currentEntry
  130. def src = entry.srcText
  131. def split
  132. def sel
  133. if (editor.selectedText) {
  134. sel = editor.selectedText
  135. split = src.endsWith(sel) ? true : false
  136. }
  137. split = split ? true :false
  138.  
  139. if (! split && (! entry.srcText || ! entry.key.next)) {
  140. message = res.getString("noMerge") + res.getString("terminating")
  141. return message.alert()
  142. }
  143.  
  144. if ( split && (src == sel) ) {
  145. message = res.getString("noSplit") + res.getString("terminating")
  146. return message.alert()
  147. }
  148.  
  149. // OK
  150. true
  151. }
  152.  
  153. void initializeScript() {
  154.  
  155. def entry = editor.currentEntry
  156. def src = entry.srcText
  157. def split
  158. if (editor.selectedText) {
  159. def sel = editor.selectedText
  160. split = src.endsWith(sel) ? true : false
  161. }
  162.  
  163. // String class
  164. String.metaClass.toXML = { ->
  165. makeValidXML(delegate as String)
  166. }
  167. String.metaClass.toRegexPattern = { ->
  168. escapeNonRegex(delegate as String)
  169. }
  170. String.metaClass.alert = { ->
  171. showMessageDialog null, delegate, split ? res.getString("splitTitle") : res.getString("mergeTitle"), INFORMATION_MESSAGE
  172. false
  173. }
  174. String.metaClass.confirm = { ->
  175. showConfirmDialog null, delegate, split ? res.getString("splitMessage") : res.getString("mergeMessage"), YES_NO_OPTION
  176. }
  177.  
  178. // SRX class
  179. SRX.metaClass.findMappingRule = { Language srclang ->
  180. delegate.mappingRules.find { MapRule maprule ->
  181. maprule.compiledPattern.matcher(srclang.language).matches()
  182. }
  183. }
  184.  
  185. // Language class
  186. Language.metaClass.isCJK = { ->
  187. delegate.languageCode.toUpperCase(Locale.ENGLISH) in ['ZH', 'JA', 'KO']
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement