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

download nltk data only when not present #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions rake_nltk/rake.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def __init__(
# If stopwords not provided we use language stopwords by default.
self.stopwords = stopwords
if self.stopwords is None:
self.stopwords = nltk.corpus.stopwords.words(language)
try:
self.stopwords = nltk.corpus.stopwords.words(language)
except LookupError:
nltk.download('stopwords')
self.stopwords = nltk.corpus.stopwords.words(language)

# If punctuations are not provided we ignore all punctuation symbols.
self.punctuations = punctuations
Expand All @@ -78,7 +82,11 @@ def extract_keywords_from_text(self, text):

:param text: Text to extract keywords from, provided as a string.
"""
sentences = nltk.tokenize.sent_tokenize(text)
try:
sentences = nltk.tokenize.sent_tokenize(text)
except LookupError:
nltk.download('punkt')
sentences = nltk.tokenize.sent_tokenize(text)
self.extract_keywords_from_sentences(sentences)

def extract_keywords_from_sentences(self, sentences):
Expand Down
27 changes: 1 addition & 26 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,6 @@
long_description = f.read()


def _post_install():
"""Post installation nltk corpus downloads."""
import nltk

nltk.download("punkt")
nltk.download("stopwords")


class PostDevelop(develop):
"""Post-installation for development mode."""

def run(self):
develop.run(self)
self.execute(_post_install, [], msg="Running post installation tasks")


class PostInstall(install):
"""Post-installation for production mode."""

def run(self):
install.run(self)
self.execute(_post_install, [], msg="Running post installation tasks")


# Get package and author details.
about = {}
with open(path.join(here, "rake_nltk", "__version__.py")) as f:
Expand Down Expand Up @@ -77,6 +53,5 @@ def run(self):
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules",
],
install_requires=["nltk"],
cmdclass={"develop": PostDevelop, "install": PostInstall},
install_requires=["nltk"]
)