pbnj

snippet #13

get_data.py




import urllib.request
import zipfile
import os

def download_quadgrams():
    url = "http://practicalcryptography.com/media/cryptanalysis/files/english_quadgrams.txt.zip"
    zip_path = "quadgrams.zip"
    
    print("[1/3] Downloading 389,000 English quadgrams from Practical Cryptography...")
    try:
        urllib.request.urlretrieve(url, zip_path)
        print("[2/3] Extracting file...")
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall(".")
        
        # Clean up the zip file
        os.remove(zip_path)
        print("[3/3] Success! 'english_quadgrams.txt' is now in your folder.")
        print("      You can now re-run s.py!")
        
    except Exception as e:
        print(f"Error downloading: {e}")
        print("Please manually download from: http://practicalcryptography.com/cryptanalysis/text-characterisation/quadgrams/")

if __name__ == "__main__":
    download_quadgrams()