我在 AWS Lambda 中遇到 NLTK 包的问题。不过我认为这个问题更多地与 Lambda 中的路径配置不正确有关。 NLTK 无法找到本地存储且不属于模块安装一部分的数据库。 SO 上列出的许多解决方案都是简单的路径配置,可以在此处找到,但我认为这个问题与 Lambda 中的路径相关:
要下载什么才能使 nltk.tokenize.word_tokenize 工作?
还应该提到这也与我在这里发布的先前问题有关 在 Python 中将 NLTK 语料库与 AWS Lambda 函数结合使用
但这个问题似乎更普遍,所以我选择重新定义这个问题,因为它涉及如何在 Lambda 中正确配置路径环境以使用需要 NLTK 等外部库的模块。 NLTK 将大量数据存储在本地的 nltk_data 文件夹中,但是将该文件夹包含在 lambda zip 中进行上传,但似乎找不到它。
Lambda func zip 文件中还包含以下文件和目录:
\nltk_data\taggers\averaged_perceptron_tagger\averaged_perceptron_tagger.pickle
\nltk_data\tokenizers\punkt\english.pickle
\nltk_data\tokenizers\punkt\PY3\english.pickle
从以下站点来看,var/task/ 似乎是 lambda 函数执行的文件夹,我尝试包含此路径但无济于事。 https://alestic.com/2014/11/aws-lambda-environment/
从文档来看,似乎还有许多可以使用的环境变量,但是我不确定如何将它们包含在 python 脚本中(来自 Windows,而不是 Linux)http://docs.aws。 amazon.com/lambda/latest/dg/current-supported-versions.html
希望在这里提出这个,以防有人有配置 Lambda 路径的经验。尽管进行了搜索,但我还没有看到很多与此特定问题相关的问题,因此希望它对解决此问题有用
代码在这里
import nltk
import pymysql.cursors
import re
import rds_config
import logging
from boto_conn import botoConn
from warnings import filterwarnings
from nltk import word_tokenize
nltk.data.path.append("/nltk_data/tokenizers/punkt")
nltk.data.path.append("/nltk_data/taggers/averaged_perceptron_tagger")
logger = logging.getLogger()
logger.setLevel(logging.INFO)
rds_host = "nodexrd2.cw7jbiq3uokf.ap-southeast-2.rds.amazonaws.com"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name
filterwarnings("ignore", category=pymysql.Warning)
def parse():
tknzr = word_tokenize
stopwords = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself','yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself',
'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that','these','those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do',
'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of','at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above',
'below','to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then','once', 'here','there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other',
'some', 'such','no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will','just', 'don', 'should','now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', 'couldn', 'didn', 'doesn', 'hadn', 'hasn',
'haven', 'isn', 'ma','mightn', 'mustn', 'needn', 'shan', 'shouldn', 'wasn', 'weren', 'won', 'wouldn']
s3file = botoConn(None, 1).getvalue()
db = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
lines = s3file.split('\n')
for line in lines:
tkn = tknzr(line)
tagged = nltk.pos_tag(tkn)
excl = ['the', 'and', 'of', 'at', 'what', 'to', 'it', 'a', 'of', 'i', 's', 't', 'is', 'I\'m', 'Im', 'U', 'RT', 'RTs', 'its'] # Arg
x = [i for i in tagged if i[0] not in stopwords]
x = [i for i in x if i[0] not in excl]
x = [i for i in x if len(i[0]) > 1]
x = [i for i in x if 'https' not in i[0]]
x = [i for i in x if i[1] == 'NNP' or i[1] == 'VB' or i[1] == 'NN']
x = [(re.sub(r'[^A-Za-z0-9]+' + '()', r'', i[0])) for i in x]
sql_dat_a, sql_dat = [], []
输出日志在这里:
**********************************************************************
Resource u'tokenizers/punkt/english.pickle' not found. Please
use the NLTK Downloader to obtain the resource: >>>
nltk.download()
Searched in:
- '/home/sbx_user1067/nltk_data'
- '/usr/share/nltk_data'
- '/usr/local/share/nltk_data'
- '/usr/lib/nltk_data'
- '/usr/local/lib/nltk_data'
- '/nltk_data/tokenizers/punkt'
- '/nltk_data/taggers/averaged_perceptron_tagger'
- u''
**********************************************************************: LookupError
Traceback (most recent call last):
File "/var/task/Tweetscrape_Timer.py", line 27, in schedule
server()
File "/var/task/Tweetscrape_Timer.py", line 14, in server
parse()
File "/var/task/parse_to_SQL.py", line 91, in parse
tkn = tknzr(line)
File "/var/task/nltk/tokenize/__init__.py", line 109, in word_tokenize
return [token for sent in sent_tokenize(text, language)
File "/var/task/nltk/tokenize/__init__.py", line 93, in sent_tokenize
tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language))
File "/var/task/nltk/data.py", line 808, in load
opened_resource = _open(resource_url)
File "/var/task/nltk/data.py", line 926, in _open
return find(path_, path + ['']).open()
File "/var/task/nltk/data.py", line 648, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource u'tokenizers/punkt/english.pickle' not found. Please
use the NLTK Downloader to obtain the resource: >>>
nltk.download()
Searched in:
- '/home/sbx_user1067/nltk_data'
- '/usr/share/nltk_data'
- '/usr/local/share/nltk_data'
- '/usr/lib/nltk_data'
- '/usr/local/lib/nltk_data'
- '/nltk_data/tokenizers/punkt'
- '/nltk_data/taggers/averaged_perceptron_tagger'
- u''
**********************************************************************
看来您当前的 Python 代码是从
/var/task
运行的。我建议尝试一下(我自己没有尝试过):
nltk.data.path.append("/var/task/nltk_data")
所以我找到了这个问题的答案。经过几天的折腾,我终于弄清楚了。 nltk文件夹下的data.py文件需要修改如下。基本上删除 /usr/... 路径并添加 Lambda 从 /var/task/ 执行的文件夹,并确保您的 nltk_data 文件夹位于执行 zip 的根目录中。
不知道为什么,但使用内联 nltk.data.path.append() 方法不适用于 AWS Lambda,需要直接修改 data.py 文件。
else:
# Common locations on UNIX & OS X:
path += [
str('/var/task/nltk_data')
#str('/usr/share/nltk_data'),
#str('/usr/local/share/nltk_data'),
#str('/usr/lib/nltk_data'),
#str('/usr/local/lib/nltk_data')
]
参加这个聚会有点晚了,但是如果您查看粘贴的代码片段上方,NLTK 库(v.3.2.2)使您能够将自己的自定义路径添加到搜索的路径数组中。
# User-specified locations:
_paths_from_env = os.environ.get('NLTK_DATA', str('')).split(os.pathsep)
path += [d for d in _paths_from_env if d]
因此,现在 Lambda 允许您添加自己的环境变量,您可以在部署函数时将 NLTK_DATA 环境变量设置为
/var/task/nltk_data
,它应该可以工作。不过我还没有在 Lambda 上测试过。
当您发布此问题时,我不确定 Lambda 是否允许环境变量,但现在应该是可行的。
编辑1 使用我部署到 Lambda 的一些 Python 应用程序重新审视这一点,我使用了上面 Matt 提供的解决方案,它对我有用。
nltk.data.path.append("/var/task/nltk_data")
在调用任何需要 NLTK 语料库的函数之前,您需要记住
import nltk
此外,需要下载语料库并将其安装在项目中的 nltk_data 子目录中(按照上述 .append 方法)。
如果在 AWS Codebuild 中使用 virtualenv,则 buildspec.yml 片段将如下所示:
pre_build:
commands:
...
- export HOME_DIR=`pwd`
- mkdir $HOME_DIR/nltk_data/
- export NLTK_DATA=$HOME_DIR/nltk_data
- $VIRTUAL_ENV/bin/python2.7 -m nltk.downloader -d $NLTK_DATA punkt
...
看起来
nltk
源代码正在检查是否存在并且可从nltk.data.path
写入的目录,否则它默认为用户主目录的子目录。
在 lambda 中,虽然您可以写入
/tmp
,但子目录最初不会存在(与 /var/task 相同)。因此,即使它可以正常工作,检查也会失败并绕过目录。
您可以更改
nltk
源代码,正如@Praxis提到的那样,还有另一种解决方案不需要这个。
nltk.download
采用可选的命名参数 download_dir
,只需将其设置为 /tmp/nltk_data
,下载就可以正常工作。
示例:
nltk.download('stopwords', download_dir='/tmp/nltk_data')