-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-gram.py
32 lines (29 loc) · 976 Bytes
/
2-gram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import string
def cleanInput(input):
input = re.sub('\n+', ' ', input)
input = re.sub('\[[0-9]*\]', '', input)
input = re.sub(' +', ' ', input)
input = bytes(input, 'utf-8')
input = input.decode('ascii', 'ignore')
cleanInput=[]
input = input.split(' ')
for item in input:
item = item.strip(string.punctuation)
if len(item) > 1 or (item.lower() == 'a' or item.lower() == 'i'):
cleanInput.append(item)
return cleanInput
def ngrams(input, n):
input = cleanInput(input)
output = []
for i in range(len(input)-n+1):
output.append(input[i:i+n])
return output
html = urlopen("http://en.wikipedia.org/wiki/Python_(programming_language)")
soup = BeautifulSoup(html)
content = soup.find('div', {"id": "mw-content-text"}).get_text()
ngrams = ngrams(content, 2)
print(ngrams)
print("2-grams count is: " + str(len(ngrams)))