Ejemplo n.º 1
0
    def apply(self, string, first=False, last=False):
        if not self.on:
            return string
        elif self.type == "prefix":
            return self.quirk["value"] + string
        elif self.type == "suffix":
            return string + self.quirk["value"]
        elif self.type == "replace":
            return string.replace(self.quirk["from"], self.quirk["to"])
        elif self.type == "regexp":
            fr = self.quirk["from"]
            if not first and len(fr) > 0 and fr[0] == "^":
                return string
            if not last and len(fr) > 0 and fr[len(fr) - 1] == "$":
                return string
            to = self.quirk["to"]
            pt = parseRegexpFunctions(to)
            return re.sub(fr, pt.expand, string)
        elif self.type == "random":
            if len(self.quirk["randomlist"]) == 0:
                return string
            fr = self.quirk["from"]
            if not first and len(fr) > 0 and fr[0] == "^":
                return string
            if not last and len(fr) > 0 and fr[len(fr) - 1] == "$":
                return string

            def randomrep(mo):
                choice = random.choice(self.quirk["randomlist"])
                pt = parseRegexpFunctions(choice)
                return pt.expand(mo)

            return re.sub(self.quirk["from"], randomrep, string)
        elif self.type == "spelling":
            percentage = self.quirk["percentage"] / 100.0
            words = string.split(" ")
            newl = []
            ctag = re.compile("(</?c=?.*?>)", re.I)
            for w in words:
                p = random.random()
                if not ctag.search(w) and p < percentage:
                    newl.append(mispeller(w))
                elif p < percentage:
                    split = ctag.split(w)
                    tmp = []
                    for s in split:
                        if s and not ctag.search(s):
                            tmp.append(mispeller(s))
                        else:
                            tmp.append(s)
                    newl.append("".join(tmp))
                else:
                    newl.append(w)
            return " ".join(newl)
Ejemplo n.º 2
0
 def apply(self, string, first=False, last=False):
     if not self.on:
         return string
     elif self.type == "prefix":
         return self.quirk["value"] + string
     elif self.type == "suffix":
         return string + self.quirk["value"]
     elif self.type == "replace":
         return string.replace(self.quirk["from"], self.quirk["to"])
     elif self.type == "regexp":
         fr = self.quirk["from"]
         if not first and len(fr) > 0 and fr[0] == "^":
             return string
         if not last and len(fr) > 0 and fr[len(fr)-1] == "$":
             return string
         to = self.quirk["to"]
         pt = parseRegexpFunctions(to)
         return re.sub(fr, pt.expand, string)
     elif self.type == "random":
         if len(self.quirk["randomlist"]) == 0:
             return string
         fr = self.quirk["from"]
         if not first and len(fr) > 0 and fr[0] == "^":
             return string
         if not last and len(fr) > 0 and fr[len(fr)-1] == "$":
             return string
         def randomrep(mo):
             choice = random.choice(self.quirk["randomlist"])
             pt = parseRegexpFunctions(choice)
             return pt.expand(mo)
         return re.sub(self.quirk["from"], randomrep, string)
     elif self.type == "spelling":
         percentage = self.quirk["percentage"]/100.0
         words = string.split(" ")
         newl = []
         ctag = re.compile("(</?c=?.*?>)", re.I)
         for w in words:
             p = random.random()
             if not ctag.search(w) and p < percentage:
                 newl.append(mispeller(w))
             elif p < percentage:
                 split = ctag.split(w)
                 tmp = []
                 for s in split:
                     if s and not ctag.search(s):
                         tmp.append(mispeller(s))
                     else:
                         tmp.append(s)
                 newl.append("".join(tmp))
             else:
                 newl.append(w)
         return " ".join(newl)
Ejemplo n.º 3
0
 def randomrep(mo):
     choice = random.choice(self.quirk["randomlist"])
     pt = parseRegexpFunctions(choice)
     return pt.expand(mo)
Ejemplo n.º 4
0
 def randomrep(mo):
     choice = random.choice(self.quirk["randomlist"])
     pt = parseRegexpFunctions(choice)
     return pt.expand(mo)