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