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, IsOneOf
    26
    27
    28  @accepts(unicode, unicode)
    29  @returns(IsOneOf(unicode, type(None)))
    30 -def correct(source, target):
    31      """Runs a set of easy and automatic corrections
    32
    33      Current corrections include:
    34        - Ellipses - align target to use source form of ellipses (either three dots or the Unicode ellipses characters)
    35        - Missing whitespace and start or end of the target
    36        - Missing punction (.:?) at the end of the target
    37      """
    38      if target == "":
    39          return target
    40      if "..." in source and u"…" in target:
    41          return target.replace(u"…", "...")
    42      if u"…" in source and "..." in target:
    43          return target.replace("...", u"…")
    44      if decoration.spacestart(source) != decoration.spacestart(target) or decoration.spaceend(source) != decoration.spaceend(target):
    45          return decoration.spacestart(source) + target.strip() + decoration.spaceend(source)
    46      punctuation = (".", ":", ". ", ": ", "?")
    47      puncendid = decoration.puncend(source, punctuation)
    48      puncendstr = decoration.puncend(target, punctuation)
    49      if puncendid != puncendstr:
    50          if not puncendstr:
    51              return target + puncendid
    52      if source[:1].isalpha() and target[:1].isalpha():
    53          if source[:1].isupper() and target[:1].islower():
    54              return target[:1].upper() + target[1:]
    55          elif source[:1].islower() and target[:1].isupper():
    56              return target[:1].lower() + target[1:]
    57      return None
    58