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)
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)
def apply(self, string, first=False, last=False): if 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 def regexprep(mo): to = self.quirk["to"] def upperrep(m): return mo.expand(m.group(1)).upper() to = _upperre.sub(upperrep, to) return mo.expand(to) return re.sub(fr, regexprep, 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"]) def upperrep(m): return mo.expand(m.group(1)).upper() choice = _upperre.sub(upperrep, choice) return mo.expand(choice) return re.sub(self.quirk["from"], randomrep, string) elif self.type == "spelling": percentage = self.quirk["percentage"] / 100.0 words = string.split(" ") newl = [] for w in words: p = random.random() if p < percentage: newl.append(mispeller(w)) else: newl.append(w) return " ".join(newl)
def apply(self, string, first=False, last=False): if 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 def regexprep(mo): to = self.quirk["to"] def upperrep(m): return mo.expand(m.group(1)).upper() to = _upperre.sub(upperrep, to) return mo.expand(to) return re.sub(fr, regexprep, 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"]) def upperrep(m): return mo.expand(m.group(1)).upper() choice = _upperre.sub(upperrep, choice) return mo.expand(choice) return re.sub(self.quirk["from"], randomrep, string) elif self.type == "spelling": percentage = self.quirk["percentage"]/100.0 words = string.split(" ") newl = [] for w in words: p = random.random() if p < percentage: newl.append(mispeller(w)) else: newl.append(w) return " ".join(newl)