-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patharrr.py
executable file
·181 lines (156 loc) · 5.34 KB
/
arrr.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A module for turning plain English into Pirate speak. Arrr.
Copyright (c) 2017 Nicholas H.Tollervey.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import random
#: The help text to be shown when requested.
_HELP_TEXT = """
Take English words and turn them into something Pirate-ish.
Documentation here: https://arrr.readthedocs.io/en/latest/
"""
#: MAJOR, MINOR, RELEASE, STATUS [alpha, beta, final], VERSION
_VERSION = (1, 0, 5)
#: Defines English to Pirate-ish word substitutions.
_PIRATE_WORDS = {
"hello": "ahoy",
"hi": "arrr",
"my": "me",
"friend": "m'hearty",
"boy": "laddy",
"girl": "lassie",
"sir": "matey",
"miss": "proud beauty",
"stranger": "scurvy dog",
"boss": "foul blaggart",
"where": "whar",
"is": "be",
"the": "th'",
"you": "ye",
"old": "barnacle covered",
"happy": "grog-filled",
"nearby": "broadside",
"bathroom": "head",
"kitchen": "galley",
"pub": "fleabag inn",
"stop": "avast",
"yes": "aye",
"no": "nay",
"yay": "yo-ho-ho",
"money": "doubloons",
"treasure": "booty",
"strong": "heave-ho",
"take": "pillage",
"drink": "grog",
"idiot": "scallywag",
"sea": "briney deep",
"vote": "mutiny",
"song": "shanty",
"drunk": "three sheets to the wind",
"lol": "yo ho ho",
"talk": "parley",
"fail": "scupper",
"quickly": "smartly",
"captain": "cap'n",
"meeting": "parley with rum and cap'n",
"there": "thar",
"are": "be",
}
#: A list of Pirate phrases to randomly insert before or after sentences.
_PIRATE_PHRASES = [
"batten down the hatches!",
"splice the mainbrace!",
"thar she blows!",
"arrr!",
"weigh anchor and hoist the mizzen!",
"savvy?",
"dead men tell no tales.",
"cleave him to the brisket!",
"blimey!",
"blow me down!",
"avast ye!",
"yo ho ho.",
"shiver me timbers!",
"blisterin' barnacles!",
"ye flounderin' nincompoop.",
"thundering typhoons!",
"sling yer hook!",
]
def get_version():
"""
Returns a string representation of the version information of this project.
"""
return ".".join([str(i) for i in _VERSION])
def capitalized(word):
"""
A MicroPython compatible means of capitalizing a string.
"""
if word:
return word[0].upper() + word[1:]
def translate(english):
"""
Take some English text and return a Pirate-ish version thereof.
"""
# Normalise a list of words (remove whitespace and make lowercase)
original = english.split()
words = [w.lower() for w in original]
# Substitute some English words with Pirate equivalents.
result = []
for count, word in enumerate(words):
if word in _PIRATE_WORDS:
result.append(_PIRATE_WORDS.get(word, word))
else:
result.append(original[count])
# Capitalize words that begin a sentence and potentially insert a pirate
# phrase with a chance of 1 in 5.
capitalize = True
for i, word in enumerate(result):
if capitalize:
result[i] = capitalized(word)
capitalize = False
if word[-1] in (".", "!", "?", ":",):
# It's a word that ends with a sentence ending character.
capitalize = True
if random.randint(0, 5) == 0:
result.insert(i + 1, random.choice(_PIRATE_PHRASES))
return " ".join(result)
def main(arrrgv=None):
"""
Entry point for the command line tool 'pirate'.
Will print help text if the optional first argument is "help". Otherwise,
takes the text passed into the command and prints a pirate version of it.
"""
# These imports are here for MicroPython compatibility related reasons.
import argparse as arrrgparse # Geddit..? ;-)
import sys
if not arrrgv:
arrrgv = sys.argv[1:]
parser = arrrgparse.ArgumentParser(description=_HELP_TEXT)
parser.add_argument("english", nargs="*", default="")
arrrgs = parser.parse_args(arrrgv)
if arrrgs.english:
try:
plain_english = " ".join(arrrgs.english)
print(translate(plain_english))
except Exception:
print(
"Error processing English. The pirates replied:\n\n"
"Shiver me timbers. We're fish bait. "
"Summat went awry, me lovely! Arrr..."
)
sys.exit(1)