Skip to content

Instantly share code, notes, and snippets.

View acrymble's full-sized avatar

Adam Crymble acrymble

  • London
View GitHub Profile
@acrymble
acrymble / webpage-to-text.py
Created July 5, 2011 19:49
Python Webpage to Text
# Given a URL, return string of lowercase text from page.
def webPageToText(url):
import urllib2
response = urllib2.urlopen(url)
html = response.read()
text = stripTags(html).lower()
return text
@acrymble
acrymble / wrap-string-in-html.py
Created July 5, 2011 19:50
Python wrap string in HTML
# Given name of calling program, a url and a string to wrap,
# output string in html body with basic metadata
# and open in Firefox tab.
def wrapStringInHTML(program, url, body):
import datetime
from webbrowser import open_new_tab
now = datetime.datetime.today().strftime("%Y%m%d-%H%M%S")
filename = program + '.html'
f = open(filename,'w')
@acrymble
acrymble / html-to-freq-3.py
Created July 5, 2011 19:51
Python HTML to Frequency Pairs
# html-to-freq-3.py
import obo
# create sorted dictionary of word-frequency pairs
url = 'http://www.oldbaileyonline.org/print.jsp?div=t17800628-33'
text = obo.webPageToText(url)
fullwordlist = obo.stripNonAlphaNum(text)
wordlist = obo.removeStopwords(fullwordlist, obo.stopwords)
dictionary = obo.wordListToFreqDict(wordlist)
@acrymble
acrymble / kwic.py
Created July 5, 2011 20:00
Python KWIC
wordstring = 'it was the best of times it was the worst of times '
wordstring += 'it was the age of wisdom it was the age of foolishness'
wordlist = wordstring.split()
print wordlist[0:4]
-> ['it', 'was', 'the', 'best']
print wordlist[0:6]
-> ['it', 'was', 'the', 'best', 'of', 'times']
print wordlist[6:10]
@acrymble
acrymble / ngram.py
Created July 5, 2011 20:01
Python ngram
i = 0
for items in wordlist:
print wordlist[i: i+5]
i += 1
@acrymble
acrymble / get-ngrams.py
Created July 5, 2011 20:02
Python get Ngrams
# Given a list of words and a number n, return a list
# of n-grams.
def getNGrams(wordlist, n):
return [wordlist[i:i+n] for i in range(len(wordlist)-(n-1))]
@acrymble
acrymble / useGetNgrams.py
Created July 5, 2011 20:03
Python ngram
#useGetNGrams.py
import obo
wordstring = 'it was the best of times it was the worst of times '
wordstring += 'it was the age of wisdom it was the age of foolishness'
allMyWords = wordstring.split()
print obo.getNGrams(allMyWords, 5)
@acrymble
acrymble / ngram-result-2.py
Created July 5, 2011 20:05
Python Ngram result 2
test1 = 'here are four words'
test2 = 'this test sentence has eight words in it'
getNGrams(test1.split(), 5)
-> []
getNGrams(test2.split(), 5)
-> [['this', 'test', 'sentence', 'has', 'eight'],
['test', 'sentence', 'has', 'eight', 'words'],
['sentence', 'has', 'eight', 'words', 'in'],
['has', 'eight', 'words', 'in', 'it']]
@acrymble
acrymble / floor-division.py
Created July 5, 2011 20:06
Python floor division
print (7 // 2)
print (5 // 2)
print (3 // 2)
@acrymble
acrymble / ngrams-to-kwic-dict.py
Created July 5, 2011 20:07
Python ngrams to KWIC dictionary
# Given a list of n-grams, return a dictionary of KWICs,
# indexed by keyword.
def nGramsToKWICDict(ngrams):
kwicdict = {}
keyindex = len(ngrams[0]) // 2