我想使用 stanford corenlp 来获取句子的依存解析器。为了在Python中使用stanford corenlp,我们需要执行以下步骤:
- 安装java
- 下载 stanford-corenlp-full-2018-10-05 并解压。
- 使用“cd”命令将目录更改为 stanford-corenlp-full-2018-10-05 文件夹。
- 在当前目录中运行此命令:
"java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 75000" .
之后,stanford-corenlp 服务器将运行在 ' http://localhost:9000 '。 最后我们可以在 python 脚本中调用 CoreNLPDependencyParser(),如下所示:
dependency_parser = CoreNLPDependencyParser(url='http://localhost:9000')
现在,我想在 google colab 上运行 stanford-corenlp 服务器。我将 stanford-corenlp-full-2018-10-05 文件夹上传到谷歌驱动器并在谷歌colab上安装谷歌驱动器。然后我安装了具有以下功能的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()
现在,我不知道如何运行上述java命令并获取本地主机地址。
有什么办法可以做到这一点吗?
最佳答案
要从远程计算机连接到在 Google Colab 上运行的服务器,您需要使用 ngrok .
假设您的服务器在现有笔记本上运行,请创建一个新笔记本并运行以下代码(我从 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
关于java - 如何在 google colab 上运行 stanford corenlp 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56001048/