2017-02-22 69 views
2

我遇到了AWS Lambda中NLTK包的問題。不過,我認爲這個問題與Lambda中的路徑配置有關的問題更多。 NLTK無法找到本地存儲的數據庫,而不是模塊安裝的一部分。許多對SO列出的解決方案是簡單的路徑CONFIGS作爲可以在這裏找到,但我認爲這個問題在LAMBDA有關尋路:AWS lambda與Python的路徑NLTK

How to config nltk data directory from code?

What to download in order to make nltk.tokenize.word_tokenize work?

還應該提到這還涉及到以前的我在這裏發佈的問題 Using NLTK corpora with AWS Lambda functions in Python

但這個問題似乎更普遍,所以我選擇重新定義問題,因爲它涉及如何正確配置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 

從下面的網站,似乎無功/任務/文件夾內將拉姆達函數執行,我有嘗試包括這條道路無濟於事。 https://alestic.com/2014/11/aws-lambda-environment/

從它似乎也有一些可以使用但我不知道如何將它們納入一個python腳本(從窗戶進來,而不是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'' 
********************************************************************** 
+0

現在,這是一個更好的問題=) – alvas

+0

問你,你爲什麼要使用lambda實例與Windows?爲lambda實例部署Linux服務器不是更容易嗎? – alvas

+0

順便說一句,amazon lambda允許你部署一個Windows實例嗎? – alvas

回答

2

所以我找到了這個問題的答案。幾天後,我終於搞清楚了。 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') 
    ] 
+1

這不是更清潔:'sys.path.append(os.path.abspath('/ var/task/nltk_data /'))' –

4

似乎您的當前Python代碼從/var/task運行。我會建議嘗試(還沒有嘗試過自己):

nltk.data.path.append("/var/task/nltk_data") 
+1

這對我有用!對於試圖解決這個問題的任何人,請記得導入nltk,並且需要將nltk語料庫下載到nltk_data /目錄中的項目中。 – Brooks

0

有點晚於這個黨,但如果你看看剛纔上面代碼片段您粘貼時,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 一些Python應用程序,我部署到LAMBDA重溫這一點,我用馬特上面提供的解決方案,它爲我工作。

nltk.data.path.append("/var/task/nltk_data")

之前要調用需要NLTK全集任何功能,你需要記住

import nltk

此外,語料庫需要在項目中被下載並安裝(每以上.append方法)在nltk_data子目錄中。

如果使用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 
    ...