Skip to content

Instantly share code, notes, and snippets.

# Install: !pip install next_word_prediction
# curText = doc_set[1135957]
from next_word_prediction import GPT2
gpt2 = GPT2()
# Predict the next word after 'vegetarian'
gpt2.predict_next(curText.replace('[MASK]', ''), 5)
# Replace with the correct words
from functools import reduce
reduce(lambda x, y: x.replace(*y), [curText, *list(correctDict.items())])
# Install: !pip install pyspellchecker
from spellchecker import SpellChecker
spell = SpellChecker()
# Text from 122001 of the wikiText data; modified to inculde typos:
# 'commentary' -> 'commentyra', 'gimmick' -> 'gimimick';
# curText = doc_set[122001]
# Use the spellchecker to identify and correct the typos
correctDict = {}
for val in re.split(r'[^\w]', curText):
import re
# curText = doc_set[122001]
curText = '''
six months ago we thought it would be a fun idea to release our album on election day but this is not the election to be cute .
we felt as though rather than making a commentary we were only riding the wave of the election . this seemed less and less like what we
intended to do and more of a gimmick .
'''
## Replace the words 'day', 'we', 'is'
print(' '.join(re.sub(r'|'.join(map(re.escape, ['day', 'we', 'is'])), ' ', curText).split()))
### Install: !pip install textstate
import textstat
# curText = doc_set[27310]
# (1) Flesch readability score
print(textstat.flesch_reading_ease(curText))
68.94 ## indicating Standard
# (2) Reading time, assuming 15 ms/character
print(textstat.reading_time(curText, ms_per_char=15))
3.8 ## 3.8s to read
# (3) Grade level: Intended for text written for children up to grade four
import streamlit as st
st.title("Online Retail")
## !!! date options
date = st.sidebar.selectbox(
"Select a Invoice Date Range",
[
"'2010-12-01' and '2011-01-01'",
"'2010-12-01' and '2010-12-15'"
])
## !!! product options
## Query with placeholders
timeSeries_query = f"""
SELECT
InvoiceDate,
Description,
ROUND(SUM(Quantity * UnitPrice), 2) AS Total_Sale_Amt
FROM
OnlineRetail
WHERE InvoiceDate BETWEEN {date} ## !!! Placeholder for date
AND Description IN ('{product}') ## !!! Placeholder for product
## Read in external SQL file
sqlFile = open(r'...\OnlineRetailPull.sql', 'r')
myQuery = sqlFile.read()
OnlineRetailData = pd.read_sql_query(myQuery, engine)
@YiLi225
YiLi225 / pythonSQL.py
Last active April 5, 2022 15:04
Query to pull data into Python
## Query to pull data
timeSeries_query = """
SELECT
InvoiceDate,
Description,
ROUND(SUM(Quantity * UnitPrice), 2) AS Total_Sale_Amt
FROM
OnlineRetail
WHERE InvoiceDate BETWEEN '2010-12-01' AND '2011-01-01'
AND Description IN ('CHOCOLATE HOT WATER BOTTLE', 'GREY HEART HOT WATER BOTTLE')
import sqlalchemy as sa
import urllib
params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"
"SERVER=localhost\SQLEXPRESS;"
"DATABASE=master;"
"Trusted_Connection=yes")
## Connect using the specified parameters
engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))