PK c,O*=ppspooner/__init__.py"""spooner""" __version__ = "0.4.1" import spooner.spooner as sp # noqa #from .cli import main # noqaPKK,OuDspooner/spooner.pyimport nltk import pronouncing import itertools import random try: nltk.data.find("corpora\\cmudict") except LookupError: # pragma: no cover nltk.download("cmudict") arpabet = nltk.corpus.cmudict.dict() def phonemes(word): """ breaks a word into syllables """ return arpabet[word][0] def spoon(text): """ switches the first sound in two words and checks for valid results credits: https://stackoverflow.com/questions/405161/detecting-syllables-in-a-word/4103234#4103234 https://stackoverflow.com/questions/33666557/get-phonemes-from-any-word-in-python-nltk-or-other-modules :param text: :return a list of matches for each word: """ dic = {} rhymesdict = {} spoons0, spoons1 = [], [] for word in text.split(): # ['trail', 'snacks'] rhymesdict[word] = pronouncing.rhymes(word) # try: phones = phonemes([word][0]) for phone in phones: # ['T', 'R', 'EY1', 'L'] if any(char.isdigit() for char in phone): firstStress = phones.index(phone) # 'EY1' dic[word] = firstStress # {'trail': 2, 'snacks': 2} break # stop after first stressed vowel # except KeyError: # # print(word, "not in CMU Pronunciation Dictionary") # # break # pass for word in text.split(): try: prefix0 = arpabet[text.split()[0]][0][:dic[text.split()[0]]] # ['T', 'R'] suffix0 = arpabet[text.split()[0]][0][dic[text.split()[0]]:] # ['EY1', 'L'] prefix1 = arpabet[text.split()[1]][0][:dic[text.split()[1]]] # ['S', 'N'] suffix1 = arpabet[text.split()[1]][0][dic[text.split()[1]]:] # ['AE1', 'K', 'S'] except KeyError: # pragma: no cover prefix0 = [] suffix0 = [] prefix1 = [] suffix1 = [] for rhyme in rhymesdict[word]: if ( r"'s" not in rhyme ): # exclude possessive nouns try: rhymePhones = phonemes([rhyme][0]) except KeyError: rhymePhones = [] # print("word is missing from the pronouncing dictionary") # print(rhymePhones) if prefix0 != prefix1: if ( rhymePhones == prefix0 + suffix1 ): # ['T', 'R'] + ['AE1', 'K', 'S'] spoons0.append(rhyme) # break # stop after first match if ( rhymePhones == prefix1 + suffix0 ): # ['S', 'N'] + ['EY1', 'L'] spoons1.append(rhyme) # break # stop after first match if spoons0 and spoons1: return [spoons1, spoons0] else: return def spoonsentence(sentence): for pair in list(itertools.combinations(sentence.split(), 2)): results = spoon(" ".join(map(str, pair))) if results: subbedSentence0 = " ".join( [ random.choice(results[0]) if x == pair[0] else x for x in sentence.split() ] ) subbedSentence1 = " ".join( [ random.choice(results[1]) if x == pair[1] else x for x in subbedSentence0.split() ] ) return subbedSentence1 # scan longer text file for spoonerisms # def readtext(source): # text = "" # with open(source) as corpus: # for line in corpus: # if ( # len(line.split()) > 2 # ): # skip very short lines # text += line # elif ( # "." in line # ): # include very short lines that include "." # text += line # else: # pass # # print skipped lines i.e. "Chapter 1" & "Introduction" # # print(line.replace('\n', '')) # sentences = text.replace("\n", "").split(".") # return sentences # # here's a chapter of a book # hhgttg = r"C:\Users\dann7982\Google Drive\The Hitch Hiker's Guide to the Galaxy\Chapter 1.txt" # for sentence in readtext(hhgttg): # spoonsentence(sentence) # break # stop after first sentence PK\*Og`.AAspooner-0.4.1.dist-info/LICENSEMIT License Copyright (c) 2019 Danny McVey 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. PK!HMuSaspooner-0.4.1.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UD"PK!H@F spooner-0.4.1.dist-info/METADATAV]S6}ׯPklaRhS0L][ K>vO{%ޅ%Lyst=8slZNԶZ+0d֨C[G'ЉW!Ze X}s&i l&uB) q]Yqج(u$m&H}=)7Sv-4!&K49 U1SMd5k;ZK@/Zi#ld7mX$6!m;[^ cժFG.${Y }JKqxҚE9NWTFo@J8eFC$<ćOxUz z ޛlhpQuwqtLh%XH{⬂؈@"npqs%H`I/0 $V0?DZ]l f$-6Ù'Ӥ& [K+V}Pfr+ G^1ɮ}FmäD3cgzEɍxِpZce:3,<(+|B##g