简体   繁体   English

如何在谷歌colab上运行stanford corenlp服务器?

[英]how to run stanford corenlp server on google colab?

I want to use stanford corenlp for obtaining dependency parser of sentences. 我想使用stanford corenlp获取句子的依赖解析器。 In order to using stanford corenlp in python, we need to do the below steps: 为了在python中使用stanford corenlp,我们需要执行以下步骤:

  1. Install java 安装java
  2. Download stanford-corenlp-full-2018-10-05 and extract it. 下载stanford-corenlp-full-2018-10-05并解压缩。
  3. Change directory to stanford-corenlp-full-2018-10-05 folder with "cd" command. 使用“cd”命令将目录更改为stanford-corenlp-full-2018-10-05文件夹。
  4. Run this command in the current directory: 在当前目录中运行此命令:

    "java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 75000" . “java -mx4g -cp”*“edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 75000”。

After that, stanford-corenlp server will run at ' http://localhost:9000 ' . 之后,stanford-corenlp服务器将运行在' http:// localhost:9000 '。 Finally we can call CoreNLPDependencyParser() in python script like this: 最后我们可以在python脚本中调用CoreNLPDependencyParser(),如下所示:

dependency_parser = CoreNLPDependencyParser(url='http://localhost:9000')

Now , i want to run stanford-corenlp server on google colab. 现在,我想在谷歌colab上运行stanford-corenlp服务器。 I uplaoded stanford-corenlp-full-2018-10-05 folder to google drive and mount google drive on google colab. 我上传stanford-corenlp-full-2018-10-05文件夹到google驱动器并在google colab上安装google驱动器。 Then I installed java with below function : 然后我用以下函数安装了java:

import os       
def install_java():
  !apt-get install -y openjdk-8-jdk-headless -qq > /dev/null     
  os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"     
  !java -version    
install_java()

Now, i don't know how run aforementioned java command and gain localhost address. 现在,我不知道如何运行前面提到的java命令并获取localhost地址。

Is there any way to do that? 有没有办法做到这一点?

To connect from a remote machine to a server running on Google Colab, you need to use ngrok . 要从远程计算机连接到在Google Colab上运行的服务器,您需要使用ngrok

Assuming your server is running on an existing notebook, create a new notebook and run the following code (which I found from here ): 假设您的服务器在现有笔记本上运行,请创建一个新笔记本并运行以下代码(我从这里找到):

import os
import subprocess
import json
import time
import requests


def _get_ngrok_tunnel():
    while True:
        try:
            tunnels_json = requests.get("http://localhost:4040/api/tunnels").content
            public_url = json.loads(tunnels_json)['tunnels'][0]['public_url']
            return public_url
        except Exception:
            print("Can't get public url, retrying...")
            time.sleep(2)


def _warmup_ngrok_tunnel(public_url):
    while requests.get(public_url).status_code >= 500:
        print("Tunnel is not ready, retrying...")
        time.sleep(2)


def expose_port_on_colab(port):
    os.system("apt-get install net-tools")
    # check that port is open
    while not (":{} ".format(port) in str(subprocess.check_output("netstat -vatn", shell=True))):
        print("Port {} is closed, retrying...".format(port))
        time.sleep(2)

    # run ngrok
    os.system("wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip")
    os.system("unzip ngrok-stable-linux-amd64.zip")
    os.system("./ngrok http {0} &".format(port))
    public_url = _get_ngrok_tunnel()
    _warmup_ngrok_tunnel(public_url)

    print("Open {0} to access your {1} port".format(public_url, port))

Then invoke expose_port_on_colab function with the port that the server is listening to, this function will give you a URL that you can use to connect to the server 然后使用服务器正在侦听的端口调用expose_port_on_colab函数,此函数将为您提供可用于连接到服务器的URL

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM