Created
July 10, 2022 11:01
-
-
Save SKaplanOfficial/73abc9b9174e0f578440a3c8858170d4 to your computer and use it in GitHub Desktop.
Using PyXA to extract paragraphs and sentences from a webpage, then create randomized flashcards from the webpage content
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 | |
from pprint import pprint | |
import PyXA | |
import random | |
from time import sleep | |
textedit = PyXA.application("TextEdit") | |
# Open a URL and wait for it to load | |
safari = PyXA.application("Safari") | |
safari.open("https://en.wikipedia.org/wiki/Computer") | |
sleep(1) | |
# Get the visible text of the document, then close the tab | |
doc_text = safari.current_document.text | |
safari.front_window().current_tab.close() | |
# Create folder path if it doesn't already exist | |
folder_path = "/Users/exampleuser/Documents/articles/" | |
os.makedirs(folder_path, exist_ok=True) | |
# Save the document text to a file on the disk | |
file_path = folder_path + "Wikipedia-Computer.txt" | |
with open(file_path, "w") as file: | |
file.write(doc_text) | |
# Create 5 random (sentence, paragraph) 'flashcards' | |
paragraphs = textedit.open(file_path).paragraphs() | |
paragraphs = random.choices(paragraphs, k=5) | |
flashcards = [(random.choice(paragraph.sentences()), paragraph) for paragraph in paragraphs] | |
pprint(flashcards) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of output: