【问题标题】:Need help Piping Python twitter script to NLP Bash script (Sed, grep etc....)需要帮助将 Python twitter 脚本连接到 NLP Bash 脚本(Sed、grep 等......)
【发布时间】:2015-10-12 05:38:12
【问题描述】:

您好,我是编程新手,几周前才开始。如果我能得到一些帮助,将不胜感激。提前致谢!

  1. 我的 python 脚本 (stream_tweets.py) 每分钟从 twitter 传输 200 - 300 条推文。该脚本位于'/home/computer/Twitter/examples/stream_tweets.py'

  2. 我有一个 NLP(自然语言处理)bash 脚本,可以分析句子并在 bash 上打印出来。 NLP 脚本 (corenlp.sh) 位于 '/home/computer/Standford/corenlp.sh'

  3. 如果我创建一个新的 bash 脚本,如何将推文通过管道传输到 NLP?这个脚本会是什么样子?

  4. 我的 python 推文脚本 (stream_tweets.py) 需要输出 utf-8 格式的文本,我该如何更改脚本。

  5. NLP 需要一段时间才能加载,如果推文涌入尚未加载的 NLP,会影响我的脚本吗?如果可以,我该怎么做?如何做?

  6. 看看 stream_twitter.py 脚本

     from TwitterAPI import TwitterAPI
     TRACK_TERM = ‘keyword1,keyword2,keyword3'
     CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
     CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
     ACCESS_TOKEN_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
     ACCESS_TOKEN_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
     api = TwitterAPI(CONSUMER_KEY,
                      CONSUMER_SECRET,
                      ACCESS_TOKEN_KEY,
                      ACCESS_TOKEN_SECRET)
     r = api.request('statuses/filter', {'track': TRACK_TERM})
     for item in r:
         print(item['text'] if 'text' in item else item)
    
  7. 看看 NLP 脚本

     OS=`uname`
     # Macs (BSD) don't support readlink -e
     if [ "$OS" == "Darwin" ]; then
         scriptdir=`dirname $0`
     else
         scriptpath=$(readlink -e "$0") || scriptpath=$0
         scriptdir=$(dirname "$scriptpath")
     fi
     echo java -mx3g -cp \"$scriptdir/*\"            edu.stanford.nlp.pipeline.StanfordCoreNLP $*
     java -mx3g -cp "$scriptdir/*" edu.stanford.nlp.pipeline.StanfordCoreNLP $*
    

【问题讨论】:

    标签: python twitter stream pipe nlp


    【解决方案1】:

    您可以将一个命令的输出作为输入传递给 Linux shell 上的另一个命令,如下所示:

    $ program_a | program_b
    

    所以在你的情况下,这看起来像这样:

    $ python /home/computer/Twitter/examples/stream_tweets.py | /home/computer/Standford/corenlp.sh
    

    但为了完成这项工作,您可能需要更改 corenlp.sh,以便在这种情况下调用 java StanfordCoreNLP 程序的最后一个命令从管道 (/dev/stdin) 读取输入。所以把最后一行改成:

    java -mx3g -cp "$scriptdir/*" edu.stanford.nlp.pipeline.StanfordCoreNLP $* < /dev/stdin
    

    为了让你的python脚本打印UTF-8编码的字符串,你需要把你的python脚本最后改成:

    import sys
    for item in r:
        text = item['text'] if 'text' in item else item
        sys.stdout.buffer.write(text.encode('utf-8'))
        sys.stdout.buffer.write('\n')
    

    当Java程序需要一些时间启动时我认为不会有问题,我认为python脚本会被阻塞直到管道缓冲区被清空。

    【讨论】:

    • 您只需使用脚本的完整路径。我已经更新了答案。
    • 如果这可以解决您的问题,请投票或接受答案:)
    • 哦,对不起,伯恩哈德,我正在尝试。你很聪明,我在你的个人资料上,我试图给你一个很好的评价,但我在任何地方都找不到!谢谢你一百万次xoxo
    • 它说我不能投票,因为我需要更多的声誉:(
    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    相关资源
    最近更新 更多