Stripping Tags Everywhere, Groovy Way

Every once in a while you have to deal with a match that has wrong tags. Hopefully, pretty soon OmegaT will be smart enough to deal with such matches for you, making it possible to insert a wrongly tagged match in such a way that you wouldn’t have to fix tags — they’ll get fixed on their own. But while we are not there yet, a practical workaround is to use the match tag-free and to insert proper tags wherever needed (OmegaT 3 lets you insert them one by one, and a new default shortcut for that is Ctrl+T).
In this post I share 5 groovy scripts to strip tags in different situations (headings link to pastebin.com, files can be downloaded from there):

  • Replacing target with match
    /*
     * #Purpose: Replace current target with tag-free match 
     * #Details: http: // wp.me/p3fHEs-4W
     * 
     * @author   Kos Ivantsov
     * @date     2013-06-26
     * @version  0.1
     */
    
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    import org.omegat.core.Core;
    
    // abort if a project is not opened yet
    def prop = project.projectProperties
    if (!prop) {
      final def title = 'Replace with Match (no tags)'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    def match = Core.getMatcher()
    def near = match.getActiveMatch()
    if (near != null) {
      def matchtranslation = "$near.translation"
      matchtranslation = matchtranslation.replaceAll(/<\/?[a-z]+[0-9]* ?\/?>/, '')
      editor.replaceEditText(matchtranslation);
    }
    
  • Inserting match
    /*
     * #Purpose: Insert tag-free match into current target 
     * #Details: http: // wp.me/p3fHEs-4W
     * 
     * @author   Kos Ivantsov
     * @date     2013-06-26
     * @version  0.1
     */
    
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    import org.omegat.core.Core;
    
    // abort if a project is not opened yet
    def prop = project.projectProperties
    if (!prop) {
      final def title = 'Insert Match (no tags)'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    def match = Core.getMatcher()
    def near = match.getActiveMatch()
    if (near != null) {
      def matchtranslation = "$near.translation"
      matchtranslation = matchtranslation.replaceAll(/<\/?[a-z]+[0-9]* ?\/?>/, '')
      editor.insertText(matchtranslation)
    }
    
  • Replacing target with source
    /*
     * #Purpose: Replace current target with tag-free source 
     * #Details: http: // wp.me/p3fHEs-4W
     * 
     * @author   Kos Ivantsov
     * @date     2013-06-26
     * @version  0.1
     */
    
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    
    // abort if a project is not opened yet
    def prop = project.projectProperties
    if (!prop) {
      final def title = 'Replace with Source (no tags)'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    def stext = editor.currentEntry.getSrcText().replaceAll(/<\/?[a-z]+[0-9]* ?\/?>/, '')
    editor.replaceEditText(stext)
    
  • Inserting source
    /*
     * #Purpose: Insert tag-free source into current target 
     * #Details: http: // wp.me/p3fHEs-4W
     * 
     * @author   Kos Ivantsov
     * @date     2013-06-26
     * @version  0.1
     */
    
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    
    // abort if a project is not opened yet
    def prop = project.projectProperties
    if (!prop) {
      final def title = 'Insert source (no tags)'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    def stext = editor.currentEntry.getSrcText().replaceAll(/<\/?[a-z]+[0-9]* ?\/?>/, '')
    editor.insertText(stext)
    
  • Stripping tags in target
    /*
     * #Purpose: Remove tags in the current target 
     * #Details: http: // wp.me/p3fHEs-4W
     * 
     * @author   Kos Ivantsov
     * @date     2013-06-26
     * @version  0.1
     */
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    
    // abort if a project is not opened yet
    def prop = project.projectProperties
    if (!prop) {
      final def title = 'Strip tags in current segment'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    target = editor.getCurrentTranslation()
    if (target != null) {
    target = target.replaceAll(/<\/?[a-z]+[0-9]* ?\/?>/, '')
    }
    editor.replaceEditText(target)
    

There are plenty of other ways to remove tags in OmegaT, some of them even posted as my recipes, but the beauty of using groovy is that scripts can be run from withing OmegaT, with its own keyboard shortcut, without needing to assign an OS shortcut to an external script/application.
As usual, inspiration for the scripts was an idea shared at OmegaT Yahoo! Group


Good luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s