Skip to content

Instantly share code, notes, and snippets.

View ShairozS's full-sized avatar

Shairoz Sohail ShairozS

View GitHub Profile
@ShairozS
ShairozS / Text Analysis with Python.ipynb
Created February 19, 2018 16:01
Understanding and coding the basics of text analysis and natural language processing with Python. Aimed at data science beginners with little to no exposure to text data.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ShairozS
ShairozS / load_nlp_data.py
Last active April 24, 2020 22:30
load_nlp_data.py
from arcgis.learn import prepare_data
from arcgis.learn import EntityRecognizer
import re
import os
import pandas as pd
from arcgis.gis import GIS
from arcgis.raster.functions import colormap
from arcgis.geocoding import batch_geocode
import zipfile,unicodedata
from itertools import repeat
@ShairozS
ShairozS / train_entityrecognizer.py
Created April 27, 2020 14:45
Train EntityRecognizer
# Initialize the EntityRecognizer model
ner = EntityRecognizer(data)
# Find an optimal learning rate
lr=ner.lr_find()
# Set how many epochs (passes over training data) we'd like the model to train for
epochs = 50
# Fit the model using the found learning rate and our specified epochs
@ShairozS
ShairozS / resize_images.py
Created August 3, 2020 21:22
Resize Document Scans for OCR
from PIL import Image
img_folder = r'F:\Data\Imagery\OCR' # Folder containing topic folders (i.e "News", "Letters" ..etc.)
for subfol in os.listdir(img_folder): # For each of the topic folders
sfpath = os.path.join(img_folder, subfol)
for imgfile in os.listdir(sfpath): # Get all images in the topic
imgpath = os.path.join(sfpath, imgfile)
img = Image.open(imgpath) # Read in the image with Pillow
img = img.resize((600,800)) # Resize the image
@ShairozS
ShairozS / image_to_text.py
Created August 3, 2020 21:38
Run OCR on Images
def image_to_text(imglist, ndocs=10):
'''
Take in a list of PIL images and return a list of extracted text using OCR
'''
headers = {
# Request headers
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': 'YOUR_KEY_HERE',
}
'''
Read in a list of scanned images (as .png files > 50x50px) and output a set of .txt files containing the text content of these scans
'''
from functions import preprocess, image_to_text
from PIL import Image
import os
from spellchecker import SpellChecker
@ShairozS
ShairozS / read_and_return_text.py
Created August 3, 2020 23:12
Read a folder of text documents
def read_and_return(foldername, fileext='.txt'):
'''
Read all text files with fileext from foldername, and place them into a list of tuples as
[(filename, text), ... , (filename, text)]
'''
allfiles = os.listdir(foldername)
allfiles = [os.path.join(foldername, f) for f in allfiles if f.endswith(fileext)]
alltext = []
for filename in allfiles:
with open(filename, 'r') as f:
@ShairozS
ShairozS / preprocess_for_lda.py
Created August 3, 2020 23:24
Preprocess Text for LDA Modeling
from gensim import corpora, models, similarities
from gensim.parsing.preprocessing import remove_stopwords, preprocess_string
def preprocess(document):
clean = remove_stopwords(document)
clean = preprocess_string(document)
return(clean)
def run_lda(textlist,
num_topics=10,
@ShairozS
ShairozS / find_document_topic.py
Created August 4, 2020 00:47
Use a trained Gensim LDA model to classify the topics in a list of text
def find_topic(textlist, dictionary, lda):
'''
https://stackoverflow.com/questions/16262016/how-to-predict-the-topic-of-a-new-query-using-a-trained-lda-model-using-gensim
For each query ( document in the test file) , tokenize the
query, create a feature vector just like how it was done while training
and create text_corpus
'''
text_corpus = []
@ShairozS
ShairozS / get_topic_label.py
Created August 4, 2020 00:50
Retrieve the label of a topic on a trained Gensim LDA model
def topic_label(ldamodel, topicnum):
alltopics = ldamodel.show_topics(formatted=False)
topic = alltopics[topicnum]
topic = dict(topic[1])
return(max(topic, key=lambda key: topic[key]))