Web mining module for Python, with tools for scraping, natural language processing, machine learning, network analysis and visualization.
Pattern is a web mining module for Python. It has tools for:
It is well documented, thoroughly tested with 350+ unit tests and comes bundled with 50+ examples. The source code is licensed under BSD.
This example trains a classifier on adjectives mined from Twitter using Python 3. First, tweets that contain hashtag #win or #fail are collected. For example: “$20 tip off a sweet little old lady today #win”. The word part-of-speech tags are then parsed, keeping only adjectives. Each tweet is transformed to a vector, a dictionary of adjective → count items, labeled WIN
or FAIL
. The classifier uses the vectors to learn which other tweets look more like WIN
or more like FAIL
.
from pattern.web import Twitter
from pattern.en import tag
from pattern.vector import KNN, count
twitter, knn = Twitter(), KNN()
for i in range(1, 3):
for tweet in twitter.search('#win OR #fail', start=i, count=100):
s = tweet.text.lower()
p = '#win' in s and 'WIN' or 'FAIL'
v = tag(s)
v = [word for word, pos in v if pos == 'JJ'] # JJ = adjective
v = count(v) # {'sweet': 1}
if v:
knn.train(v, type=p)
print(knn.classify('sweet potato burger'))
print(knn.classify('stupid autocorrect'))
Pattern supports Python 2.7 and Python 3.6. To install Pattern so that it is available in all your scripts, unzip the download and from the command line do:
cd pattern-3.6
python setup.py install
If you have pip, you can automatically download and install from the PyPI repository:
pip install pattern
If none of the above works, you can make Python aware of the module in three ways:
c:\python36\Lib\site-packages\
(Windows),/Library/Python/3.6/site-packages/
(Mac OS X),/usr/lib/python3.6/site-packages/
(Unix).sys.path
in your script, before importing it:MODULE = '/users/tom/desktop/pattern'
import sys; if MODULE not in sys.path: sys.path.append(MODULE)
from pattern.en import parsetree
For documentation and examples see the user documentation.
3.6
BSD, see LICENSE.txt
for further details.
De Smedt, T., Daelemans, W. (2012). Pattern for Python. Journal of Machine Learning Research, 13, 2031–2035.
The source code is hosted on GitHub and contributions or donations are welcomed.
Pattern is bundled with the following data sets, algorithms and Python packages:
Authors:
Contributors (chronological):