@pythonb5/

periodicpuns

Python

Spell anything with elements and subatomic particles!

fork
loading
Files
  • main.py
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# periodicpuns
# by python-b5
#
# Use periodicpuns to spell anything with elements and
# subatomic particles!
#
# Based on chemspeller.com, but coded from scratch in
# Python.

def look_ahead(iterable):
    it = iter(iterable)
    last = next(it)
    for val in it:
        yield last, True
        last = val
    yield last, False

elements = {
    'H': ('hydrogen', 1),
    'D': ('deuterium', 1),
    'He': ('helium', 2),
    'Li': ('lithium', 3),
    'Be': ('beryllium', 4),
    'B': ('boron', 5),
    'C': ('carbon', 6),
    'N': ('nitrogen', 7),
    'O': ('oxygen', 8),
    'F': ('fluorine', 9),
    'Ne': ('neon', 10),
    'Na': ('sodium', 11),
    'Mg': ('magnesium', 12),
    'Al': ('aluminum', 13),
    'Si': ('silicon', 14),
    'P': ('phosphorus', 15),
    'S': ('sulfur', 16),
    'Cl': ('chlorine', 17),
    'Ar': ('argon', 18),
    'K': ('potassium', 19),
    'Ca': ('calcium', 20),
    'Sc': ('scandium', 21),
    'Ti': ('titanium', 22),
    'V': ('vanadium', 23),
    'Cr': ('chromium', 24),
    'Mn': ('manganese', 25),
    'Fe': ('iron', 26),
    'Co': ('cobalt', 27),
    'Ni': ('nickel', 28),
    'Cu': ('copper', 29),
    'Zn': ('zinc', 30),
    'Ga': ('gallium', 31),
    'Ge': ('germanium', 32),
    'As': ('arsenic', 33),
    'Se': ('selenium', 34),
    'Br': ('bromine', 35),
    'Kr': ('krypton', 36),
    'Rb': ('rubidium', 37),
    'Sr': ('strontium', 38),
    'Y': ('yttrium', 39),
    'Zr': ('zirconium', 40),
    'Nb': ('niobium', 41),
    'Mo': ('molybdenum', 42),
    'Tc': ('technetium', 43),
    'Ru': ('ruthenium', 44),
    'Rh': ('rhodium', 45),
    'Pd': ('palladium', 46),
    'Ag': ('silver', 47),
    'Cd': ('cadmium', 48),
    'In': ('indium', 49),
    'Sn': ('tin', 50),
    'Sb': ('antimony', 51),
    'Te': ('tellurium', 52),
    'I': ('iodine', 53),
    'Xe': ('xenon', 54),
    'Cs': ('cesium', 55),
    'Ba': ('barium', 56),
    'La': ('lanthanum', 57),
    'Ce': ('cerium', 58),
    'Pr': ('praseodymium', 59),
    'Nd': ('neodymium', 60),
    'Pm': ('promethium', 61),
    'Sm': ('samarium', 62),
    'Eu': ('europium', 63),
    'Gd': ('gadolinium', 64),
    'Tb': ('terbium', 65),
    'Dy': ('dysprosium', 66),
    'Ho': ('holmium', 67),
    'Er': ('erbium', 68),
    'Tm': ('thulium', 69),
    'Yb': ('ytterbium', 70),
    'Lu': ('lutetium', 71),
    'Hf': ('hafnium', 72),
    'Ta': ('tantalum', 73),
    'W': ('tungsten', 74),
    'Re': ('rhenium', 75),
    'Os': ('osmium', 76),
    'Ir': ('iridium', 77),
    'Pt': ('platinum', 78),
    'Au': ('gold', 79),
    'Hg': ('mercury', 80),
    'Tl': ('thallium', 81),
    'Pb': ('lead', 82),
    'Bi': ('bismuth', 83),
    'Po': ('polonium', 84),
    'At': ('astatine', 85),
    'Rn': ('radon', 86),
    'Fr': ('francium', 87),
    'Ra': ('radium', 88),
    'Ac': ('actinium', 89),
    'Th': ('thorium', 90),
    'Pa': ('protactinium', 91),
    'U': ('uranium', 92),
    'Np': ('neptunium', 93),
    'Pu': ('plutonium', 94),
    'Am': ('americium', 95),
    'Cm': ('curium', 96),
    'Bk': ('berkelium', 97),
    'Cf': ('californium', 98),
    'Es': ('einsteinium', 99),
    'Fm': ('fermium', 100),
    'Md': ('mendelevium', 101),
    'No': ('nobelium', 102),
    'Lr': ('lawrencium', 103),
    'Rf': ('rutherfordium', 104),
    'Db': ('dubnium', 105),
    'Sg': ('seaborgium', 106),
    'Bh': ('bohrium', 107),
    'Hs': ('hassium', 108),
    'Mt': ('meitnerium', 109),
    'Ds': ('darmstadtium', 110),
    'Rg': ('roentgenium', 111),
    'Cn': ('copernicium', 112),
    'Nh': ('nihonium', 113),
    'Fl': ('flerovium', 114),
    'Mc': ('moscovium', 115),
    'Lv': ('livermorium', 116),
    'Ts': ('tennessine', 117),
    'Og': ('oganesson', 118)
}

letters = ['H', 'D', 'B', 'C', 'N', 'O', 'F', 'P', 'S', 'K', 'V', 'Y', 'I', 'W', 'U']

other = ['\n', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '`', '-', '=', '[', ']', '\\', ';', "'", ',', '.', '/', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '~', '_', '+', '{', '}', '|', ':', '"', '<', '>', '?']

sounds = ['He', 'Li', 'Be', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'Cl', 'Ar', 'Ca', 'Sc', 'Ti', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og']

particles = {
    "A": ("α", "alpha particle"),
    "E": ("e−", "electron", -1),
    "G": ("g", "gluon", 0),
    "L": ("l", "lepton"),
    "M": ("m", "mass"),
    "Q": ("q", "quark"),
    "T": ("τ", "tau"),
    "J": ("J", "joule"),
    "R": ("R", "roentgen"),
    "X": ("X", "X boson"),
    "Z": ("Z", "Z boson")
}

print("periodicpuns")
print("by python-b5\n\n--------------------\n")

while True:
    input_string = input("Enter something to be pun-ified: ")

    string = [list(i) for i in input_string.upper().split(" ")]
    characters_used = []
    word_count = -1
    sound = False

    print("\nPun-ification:")
    for i, more_i in look_ahead(string):
        char_count = 0
        word_output = []
        for j, more_j in look_ahead(i):
            skip = sound
            sound = False
            for l in sounds:
                if j == l[0] and not skip:
                    try:
                        if i[char_count + 1].lower() == l[1]:
                            word_output.append(l)
                            sound = True
                    except IndexError:
                        pass
            if sound or skip:
                pass
            elif j in letters or j in other:
                word_output.append(j)
            elif j in particles:
                word_output.append(particles[j][0])
            char_count += 1
        for j, more_j in look_ahead(word_output):
            if j not in characters_used and j not in other:
                characters_used.append(j)
            print(j, end="")
            if more_j and j != "\n":
                print(end=" ")
        if more_i and i[-1] != "\n":
            print(end="  ")
        word_count += 1

    print("\n")
    print("Symbols used:")

    if characters_used:
        for i, more_i in look_ahead(characters_used):
            print(i, "-", end=" ")
            if i in elements:
                print(elements[i][0], "-", elements[i][1], end="")
            elif i in particles:
                print(particles[i][1], end="")
                if len(particles[i]) > 2:
                    print(" -", particles[i][2], end="")
            else:
                for j in particles:
                    if i == particles[j][0]:
                        print(particles[j][1], end="")
                        if len(particles[j]) > 2:
                            print(" -", particles[j][2], end="")
            if more_i:
                print(end="\n")
    else:
        print("None", end="")

    print("\n")
    print("Amount of symbols used:", str(len(characters_used)) + "/131\n\n--------------------\n")