如何在Google Colab上运行斯坦福大学Corenlp服务器? [英] how to run stanford corenlp server on google colab?

查看:345
本文介绍了如何在Google Colab上运行斯坦福大学Corenlp服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用stanford corenlp来获取句子的依赖解析器.为了在python中使用stanford corenlp,我们需要执行以下步骤:

I want to use stanford corenlp for obtaining dependency parser of sentences. In order to using stanford corenlp in python, we need to do the below steps:

  1. 安装Java
  2. 下载stanford-corenlp-full-2018-10-05并将其解压缩.
  3. 使用"cd"命令将目录更改为stanford-corenlp-full-2018-10-05文件夹.
  4. 在当前目录中运行以下命令:
  1. Install java
  2. Download stanford-corenlp-full-2018-10-05 and extract it.
  3. Change directory to stanford-corenlp-full-2018-10-05 folder with "cd" command.
  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" .

之后,stanford-corenlp服务器将在' http://localhost:9000 '下运行. 最后,我们可以像这样在python脚本中调用CoreNLPDependencyParser():

After that, stanford-corenlp server will run at 'http://localhost:9000' . Finally we can call CoreNLPDependencyParser() in python script like this:

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

现在,我想在google colab上运行stanford-corenlp服务器.我将stanford-corenlp-full-2018-10-05文件夹升级为Google驱动器并将Google驱动器安装在Google Colab上.然后我用以下功能安装了Java:

Now , i want to run stanford-corenlp server on google colab. I uplaoded stanford-corenlp-full-2018-10-05 folder to google drive and mount google drive on google colab. Then I installed java with below function :

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()

现在,我不知道如何运行上述java命令并获取本地主机地址.

Now, i don't know how run aforementioned java command and gain localhost address.

有什么办法吗?

推荐答案

要从远程计算机连接到在Google Colab上运行的服务器,您需要使用 ngrok .

To connect from a remote machine to a server running on Google Colab, you need to use 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))

然后使用服务器正在侦听的端口调用expose_port_on_colab函数,该函数将为您提供一个可用于连接到服务器的URL

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

这篇关于如何在Google Colab上运行斯坦福大学Corenlp服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆