Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic modeling #37

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ python:
install:
- pip install ".[testing]"
- pip install ".[nlp]"
- pip install ".[topic_modeling]"
- python -m nltk.downloader punkt stopwords wordnet
script: pytest
deploy:
Expand Down
26 changes: 26 additions & 0 deletions quantgov/estimator/candidate_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@
import sklearn.multioutput
import sklearn.pipeline
import sklearn.feature_extraction
from . import structures

try:
import gensim
except ImportError:
gensim = None
try:
import gensim
import spacy
except ImportError:
spacy = None
gensim = None

import quantgov.estimator


classification = [
quantgov.estimator.CandidateModel(
name="Random Forests",
Expand Down Expand Up @@ -69,3 +82,16 @@
}
),
]

if gensim and spacy:
topic_modeling = [
quantgov.estimator.CandidateModel(
name="LDA",
model=structures.QGLdaModel(),
parameters={
'eta': [0.1, 0.05, 0.01],
'passes': [1, 2, 3],
'num_topics': [10, 50, 100]
}
),
]
66 changes: 66 additions & 0 deletions quantgov/estimator/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@
"""
import collections
import joblib as jl
from sklearn.base import BaseEstimator, TransformerMixin
from six import iteritems
from decorator import decorator
import re

try:
from spacy.lang.en.stop_words import STOP_WORDS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're literally only using spacy here for the stopwords, can't we somehow find the sklearn stopwords used in the CountVectorizer? That's got to be importable from somewhere.

from gensim.corpora import Dictionary
from gensim import sklearn_api
import gensim
spacy = True
except ImportError:
spacy = None
gensim = None


@decorator
def check_spacy(func, *args, **kwargs):
if spacy is None:
raise RuntimeError('Must install spacy to use {}'.format(func))
return func(*args, **kwargs)


@decorator
def check_gensim(func, *args, **kwargs):
if gensim is None:
raise RuntimeError('Must install gensim to use {}'.format(func))
return func(*args, **kwargs)


class _PersistanceMixin(object):
Expand Down Expand Up @@ -85,3 +113,41 @@ class CandidateModel(
parameter values to test as values
"""
pass


class QGLdaModel(BaseEstimator, TransformerMixin):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like either the prefix or the Model specifier. I'd call this GensimLDA or something like that.

@check_gensim
@check_spacy
def __init__(self, word_regex=r'\b[A-z]{2,}\b', stop_words=STOP_WORDS):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think the options for stop_words should be:

  • None (default): No stop words
  • True: use built-in stop words
  • A sequence: user-specified stop words.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not having any stop words seems to output a pretty unusable model - my thinking is it's best to have some default, and if the user chooses to override that default with None they can, but the defaults should be able to produce something usable - we could include some output if they don't provide any (e.g. "INFO: No stop words provided, using sklearn builtins"), and potentially a warning if None is passed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

word_regex should be word_pattern to match what already exists in SKL.

self.stop_words = stop_words
self.word_regex = re.compile(word_regex)

def transform(self, driver):
self.test_corpus = self.create_corpus(driver)
return self.model.transform(self.test_corpus)

def create_corpus(self, driver):
return [self.dictionary.doc2bow([i.group(0).lower()
for i in self.word_regex.finditer(doc.text)])
for doc in driver.stream()]

def fit(self, driver, alpha=None, eta=None, num_topics=1, passes=1):
self.dictionary = Dictionary([[i.group(0).lower()
for i in self.word_regex
.finditer(doc.text)]
for doc in driver.stream()])
stop_ids = [self.dictionary.token2id[stopword] for stopword
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to only pass the dictionary words that aren't in stop_words?

in self.stop_words if stopword in self.dictionary.token2id]
once_ids = [tokenid for tokenid, docfreq in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering out words that only occur once was recommended in the Gensim documentation - beyond that, I don't know if it actually improves the performance of the model.

iteritems(self.dictionary.dfs) if docfreq == 1]
self.dictionary.filter_tokens(stop_ids + once_ids)
self.corpus = self.create_corpus(driver)
self.model = sklearn_api.ldamodel.LdaTransformer(
alpha=alpha,
eta=eta,
num_topics=num_topics,
passes=passes,
id2word=self.dictionary
)
self.model.fit(self.corpus)
return self
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def find_version(*file_paths):
'nlp': [
'textblob',
'nltk',
],
'topic_modeling': [
'gensim',
'spacy'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need a spacy corpus as well?

]
},
entry_points={
Expand Down
22 changes: 22 additions & 0 deletions tests/test_estimators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# import pytest
import subprocess
import quantgov.estimator
import quantgov

from pathlib import Path

PSEUDO_CORPUS_PATH = Path(__file__).resolve().parent.joinpath('pseudo_corpus')
driver = quantgov.load_driver(PSEUDO_CORPUS_PATH)


def test_topic_model():
sample = quantgov.estimator.structures.QGLdaModel()
sample.fit(driver, num_topics=2)
sample.transform(driver)


def check_output(cmd):
return (
subprocess.check_output(cmd, universal_newlines=True)
.replace('\n\n', '\n')
)