public
Description: Bookmarklet to make any page pirate speak
Homepage:
Clone URL: git://github.com/bgolub/piratize.git
Benjamin Golub (author)
Fri Oct 24 13:32:47 -0700 2008
piratize / pirate.py
100644 164 lines (152 sloc) 5.005 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
Talk like a Pirate.
 
Originally in PHP as part of Dougal Campbell's Text Filter Suite
(http://dougal.gunters.org/blog/2004/08/30/text-filter-suite), and ported to
Python by Jacob Kaplan-Moss (jacob@jacobian.org). You'll be able to find
updates, if there are any, at http://toys.jacobian.org/misc/pirate.py.txt.
 
Licensed, as the original is, under the GPL:
http://www.fsf.org/licensing/licenses/gpl.html. Hooray for license lock-in.
"""
 
import re
import random
from functools import partial
 
# Public API ##################################################################
 
def pirate(content):
    return CDATA_PATTERN.sub(pirate_filter, content)
 
# The rest be private-ish. Arr. ###############################################
    
def pirate_filter(m):
    bit = m.group(0)
    for pat, repl in PIRATE_PATTERNS:
        bit = pat.sub(repl, bit)
    return bit
 
def avast(m, chance):
    if random.randint(0, chance) == chance:
        return random.choice(SHOUTS) % {"stub": m.group(0)}
    else:
        return m.group(0)
 
CDATA_PATTERN = re.compile("(?:(?<=>)|\A)([^<>]+)(?:(?=<)|\Z)")
PIRATE_PATTERNS = [(re.compile(p, re.I), r) for (p,r) in [
    (r'\bmy\b', 'me'),
    (r'\bboss\b', 'admiral'),
    (r'\bmanager\b', 'admiral'),
    (r'\b[Cc]aptain\b', "Cap'n"),
    (r'\bmyself\b', 'meself'),
    (r'\byour\b', 'yer'),
    (r'\byou\b', 'ye'),
    (r'\bfriend\b', 'matey'),
    (r'\bfriends\b', 'maties'),
    (r'\bco[-]?worker\b', 'shipmate'),
    (r'\bco[-]?workers\b', 'shipmates'),
    (r'\bpeople\b', 'scallywags'),
    (r'\bearlier\b', 'afore'),
    (r'\bold\b', 'auld'),
    (r'\bthe\b', "th'"),
    (r'\bof\b', "o'"),
    (r"\bdon't\b%", "dern't"),
    (r'\bdo not\b', "dern't"),
    (r'\bnever\b', "ne'er"),
    (r'\bever\b', "e'er"),
    (r'\bover\b', "o'er"),
    (r'\bYes\b', 'Aye'),
    (r'\bNo\b', 'Nay'),
    (r'\bYeah\b', 'Aye'),
    (r'\byeah\b', 'aye'),
    (r"\bdon't know\b%", "dinna"),
    (r"\bdidn't know\b%", "did nay know"),
    (r"\bhadn't\b%", "ha'nae"),
    (r"\bdidn't\b%", "di'nae"),
    (r"\bwasn't\b%", "weren't"),
    (r"\bhaven't\b%", "ha'nae"),
    (r'\bfor\b', 'fer'),
    (r'\bbetween\b', 'betwixt'),
    (r'\baround\b', "aroun'"),
    (r'\bto\b', "t'"),
    (r"\bit's\b%", "'tis"),
    (r'\bwoman\b', 'wench'),
    (r'\bwomen\b', 'wenches'),
    (r'\blady\b', 'wench'),
    (r'\bwife\b', 'lady'),
    (r'\bgirl\b', 'lass'),
    (r'\bgirls\b', 'lassies'),
    (r'\bguy\b', 'lubber'),
    (r'\bman\b', 'lubber'),
    (r'\bfellow\b', 'lubber'),
    (r'\bdude\b', 'lubber'),
    (r'\bboy\b', 'lad'),
    (r'\bboys\b', 'laddies'),
    (r'\bchildren\b', 'little sandcrabs'),
    (r'\bkids\b', 'minnows'),
    (r'\bhim\b', 'that scurvey dog'),
    (r'\bher\b', 'that comely wench'),
    (r'\bhim\.\b', 'that drunken sailor'),
    (r'\bHe\b', 'The ornery cuss'),
    (r'\bShe\b', 'The winsome lass'),
    (r"\bhe's\b%", 'he be'),
    (r"\bshe's\b%", 'she be'),
    (r'\bwas\b', "were bein'"),
    (r'\bHey\b', 'Avast'),
    (r'\bher\.\b', 'that lovely lass'),
    (r'\bfood\b', 'chow'),
    (r'\bmoney\b', 'dubloons'),
    (r'\bdollars\b', 'pieces of eight'),
    (r'\bcents\b', 'shillings'),
    (r'\broad\b', 'sea'),
    (r'\broads\b', 'seas'),
    (r'\bstreet\b', 'river'),
    (r'\bstreets\b', 'rivers'),
    (r'\bhighway\b', 'ocean'),
    (r'\bhighways\b', 'oceans'),
    (r'\binterstate\b', 'high sea'),
    (r'\bprobably\b', 'likely'),
    (r'\bidea\b', 'notion'),
    (r'\bcar\b', 'boat'),
    (r'\bcars\b', 'boats'),
    (r'\btruck\b', 'schooner'),
    (r'\btrucks\b', 'schooners'),
    (r'\bSUV\b', 'ship'),
    (r'\bairplane\b', 'flying machine'),
    (r'\bjet\b', 'flying machine'),
    (r'\bmachine\b', 'contraption'),
    (r'\bdriving\b', 'sailing'),
    (r'\bunderstand\b', 'reckon'),
    (r'\bdrive\b', 'sail'),
    (r'\bdied\b', 'snuffed it'),
    (r'ing\b/', "in'"),
    (r'ings\b/', "in's"),
    (r'(\.\s)', partial(avast, chance=1)),
    (r'([!\?]\s)', partial(avast, chance=1)),
]]
 
SHOUTS = [
    ", avast%(stub)s",
    "%(stub)s Ahoy!",
    ", and a bottle of rum!",
    ", by Blackbeard's sword%(stub)s",
    ", by Davy Jones' locker%(stub)s",
    "%(stub)s Walk the plank!",
    "%(stub)s Aarrr!",
    "%(stub)s Yaaarrrrr!",
    ", pass the grog!",
    ", and dinna spare the whip!",
    ", with a chest full of booty%(stub)s",
    ", and a bucket o' chum%(stub)s",
    ", we'll keel-haul ye!",
    "%(stub)s Shiver me timbers!",
    "%(stub)s And hoist the mainsail!",
    "%(stub)s And swab the deck!",
    ", ye scurvey dog%(stub)s",
    "%(stub)s Fire the cannons!",
    ", to be sure%(stub)s",
    ", I'll warrant ye%(stub)s",
    ", on a dead man's chest!",
    "%(stub)s Load the cannons!",
    "%(stub)s Prepare to be boarded!",
    ", I'll warrant ye%(stub)s",
    "%(stub)s Ye'll be sleepin' with the fishes!",
    "%(stub)s The sharks will eat well tonight!",
    "%(stub)s Oho!",
    "%(stub)s Fetch me spyglass!",
]
 
if __name__ == '__main__':
    import sys
    for line in sys.stdin:
        sys.stdout.write(pirate(line))