1#
2# Copyright 2019 Cademia Siciliana <tech@cademiasiciliana.org>
3#
4# This file is part of translate.
5#
6# translate is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# translate is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, see <http://www.gnu.org/licenses/>.
18
19"""This module represents the Sicilian language.
20
21.. seealso:: http://en.wikipedia.org/wiki/Sicilian_language
22"""
23
24
25from translate.filters.checks import CheckerConfig, FilterFailure, TranslationChecker
26from translate.filters.decorators import critical
27from translate.lang import common
28
29
30def contains_illegal(illegal_substrings, string):
31    """Check if string contains any of the specified illegal substrings.
32
33    :param illegal_substrings: an iterable of illegal substrings
34    :param string: the string to check against occurences of illegal substrings
35    :return: True if string contains any of the illegal substrings
36    """
37    for s in illegal_substrings:
38        if s in string:
39            return True
40    return False
41
42
43sicilianconfig = CheckerConfig(
44    validchars="àèìòùḍÀÈÌÒÙḌ",
45)
46
47
48class SicilianChecker(TranslationChecker):
49    """A Checker class for Sicilian"""
50
51    def __init__(self, **kwargs):
52        checkerconfig = kwargs.get("checkerconfig", None)
53
54        if checkerconfig is None:
55            checkerconfig = CheckerConfig()
56            kwargs["checkerconfig"] = checkerconfig
57
58        checkerconfig.update(sicilianconfig)
59        super().__init__(**kwargs)
60
61    @critical
62    def italianisms(self, str1, str2):
63        """Check if the translation contains common errors done by italophones.
64
65        Mainly inspired by musttranslatewords(), but with a different logic: return True if
66        the given word appears in the translation but not in the source (if it's in the source,
67        then presumably it's being kept untranslated).
68
69        :param str1: the source string
70        :param str2: the target (translated) string
71        :return: True if str2 doesn't contain an "italianism"
72        """
73        str1 = self.removevariables(str1)
74        str2 = self.removevariables(str2)
75
76        errors = {
77            "io": "ju/jeu/iu/...",
78            "tantu": "assai",
79            "menu": "cchiù picca",
80        }
81
82        # The above is full of strange quotes and things in utf-8 encoding.
83        # single apostrophe perhaps problematic in words like "doesn't"
84        for separator in self.config.punctuation:
85            str1 = str1.replace(separator, " ")
86            str2 = str2.replace(separator, " ")
87
88        words1 = self.filteraccelerators(str1).split()
89        words2 = self.filteraccelerators(str2).split()
90        stopwords = [
91            f"{word} ({errors[word]})"
92            for word in words2
93            if word.lower() in errors.keys() and word not in words1
94        ]
95
96        if stopwords:
97            raise FilterFailure("Please translate: %s" % (", ".join(stopwords)))
98
99        return True
100
101    @critical
102    def vocalism(self, str1, str2):
103        """Check correct word-endings. All words should end with a/i/u, but a handful of exceptions:
104
105          - me, to, so (possessive pronouns)
106          - po (verb "putiri")
107          - no
108          - jo (personal pronoun)
109          - se (yes)
110          - nne (in)
111
112        :param str1: the source string
113        :param str2: the target (translated) string
114        :return: True if there are no words with endings not in respect of vocalism (or if they appear in source string as well)
115        """
116        exceptions = ["me", "to", "so", "po", "no", "jo", "se", "nne"]
117
118        stopwords = []
119
120        for word in self.config.lang.words(str2):
121            if word not in str1 and word.lower() not in exceptions:
122                if word.lower().endswith(("e", "o")) and word.lower() not in stopwords:
123                    stopwords.append(word.lower())
124
125        if stopwords:
126            raise FilterFailure("Please respect vocalism: %s" % (", ".join(stopwords)))
127        return True
128
129    @critical
130    def suffixes(self, str1, str2):
131        """Check for common word suffixes to be written correctly.
132
133        :param str1: the source string
134        :param str2: the target (translated) string
135        :return: True if there are no common suffixes wrongly written
136        """
137        suffixes = {
138            "zzioni": "zziuni",
139        }
140
141        stopwords = []
142
143        for word in self.config.lang.words(str2):
144            for suffix in suffixes.keys():
145                if word not in str1 and word.lower().endswith(suffix):
146                    stopwords.append(f"{word} (-{suffixes[suffix]})")
147
148        if stopwords:
149            raise FilterFailure(
150                "Please use the correct word endings: %s" % (", ".join(stopwords))
151            )
152        return True
153
154
155class scn(common.Common):
156    """This class represents Sicilian."""
157
158    checker = SicilianChecker()
159
160    ignoretests = {
161        "all": ["doublewords"],
162    }
163