translate.filters.autocorrect

Package translate :: Package filters :: Module autocorrect |
[hide private]
[frames] | no frames]

# Source Code for Module translate.filters.autocorrect

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2005, 2006, 2009 Zuza Software Foundation
     5  #
     6  # This file is part of the Translate Toolkit.
     7  #
     8  # This program is free software; you can redistribute it and/or modify
     9  # it under the terms of the GNU General Public License as published by
    10  # the Free Software Foundation; either version 2 of the License, or
    11  # (at your option) any later version.
    12  #
    13  # This program is distributed in the hope that it will be useful,
    14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  # GNU General Public License for more details.
    17  #
    18  # You should have received a copy of the GNU General Public License
    19  # along with this program; if not, see <http://www.gnu.org/licenses/>.
    20
    21  """A set of autocorrect functions that fix common punctuation and space problems automatically"""
    22
    23  from translate.filters import decoration
    24
    25  from translate.misc.typecheck import accepts, returns
    26
    27  @accepts(unicode, unicode)
    28  #@returns(IsOneOf(unicode, None))
    29 -def correct(source, target):
    30      """Runs a set of easy and automatic corrections
    31
    32      Current corrections include:
    33        - Ellipses - align target to use source form of ellipses (either three dots or the Unicode ellipses characters)
    34        - Missing whitespace and start or end of the target
    35        - Missing punction (.:?) at the end of the target
    36      """
    37      if target == "":
    38          return target
    39      if "..." in source and u"…" in target:
    40          return target.replace(u"…", "...")
    41      if u"…" in source and "..." in target:
    42          return target.replace("...", u"…")
    43      if decoration.spacestart(source) != decoration.spacestart(target) or decoration.spaceend(source) != decoration.spaceend(target):
    44          return decoration.spacestart(source) + target.strip() + decoration.spaceend(source)
    45      punctuation = (".", ":", ". ", ": ", "?")
    46      puncendid = decoration.puncend(source, punctuation)
    47      puncendstr = decoration.puncend(target, punctuation)
    48      if puncendid != puncendstr:
    49          if not puncendstr:
    50              return target + puncendid
    51      if source[:1].isalpha() and target[:1].isalpha():
    52          if source[:1].isupper() and target[:1].islower():
    53              return target[:1].upper() + target[1:]
    54          elif source[:1].islower() and target[:1].isupper():
    55              return target[:1].lower() + target[1:]
    56      return None
    57