Created
September 2, 2019 19:00
-
-
Save zhudotexe/26def884feaa37c04b96a45834f17175 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import requests | |
| WORD_CACHE = {} | |
| API_KEY = os.environ.get("API_KEY") | |
| API_BASE = f"https://dictionaryapi.com/api/v3/references/collegiate/json/{{}}?key={API_KEY}" | |
| def get_ipa(word): | |
| if word in WORD_CACHE: | |
| return WORD_CACHE[word] | |
| response = requests.get(API_BASE.format(word)).json() | |
| print(f"Getting {word}...") | |
| if not len(response): | |
| print(f"I could not find {word} in the dictionary!") | |
| WORD_CACHE[word] = word | |
| return word | |
| word_0 = response[0] | |
| if not isinstance(word_0, dict): | |
| print(f"I could not find {word} in the dictionary!") | |
| WORD_CACHE[word] = word | |
| return word | |
| pronounciation = word_0['hwi'].get('prs', [{}])[0].get('mw', word) | |
| WORD_CACHE[word] = pronounciation | |
| return pronounciation | |
| def process_chunk(chunk): | |
| times = chunk.split('\n')[0] | |
| words = '\n'.join(chunk.split('\n')[1:]) | |
| for word in words.split(): | |
| words = words.replace(word, get_ipa(word), 1) | |
| return f"{times}\n{words}" | |
| def run(): | |
| fp = input("Input: ") | |
| with open(fp) as f: | |
| chunks = f.read().split('\n\n') | |
| out = [] | |
| for chunk in chunks: | |
| out.append(process_chunk(chunk)) | |
| with open("captions-out.sbv", 'w') as f: | |
| f.write('\n\n'.join(out)) | |
| print("Done!") | |
| if __name__ == '__main__': | |
| run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment