您如何确保节 CoreNLPClient 的可行端点?

How can you ensure a viable endpoint for a stanza CoreNLPClient?

我想使用节 CoreNLPClient 来提取名词短语,类似于

但是,我似乎找不到启动服务器的好端口。默认为9000,但是这个经常被占用,如错误信息所示:

PermanentlyFailedException: Error: unable to start the CoreNLP server on port 9000 (possibly something is already running there)

编辑:端口 9000 正在被 python.exe 使用,这就是为什么我不能关闭进程为 CoreNLPClient 创建 space 的原因。

然后,当我select其他端口如7999、8000、8080时,服务器一直在监听,不执行连续的代码行,只显示如下:

2021-07-19 12:05:55 INFO: Starting server with command: java -Xmx8G -cp C:\Users\timjo\stanza_corenlp* edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 7998 -timeout 60000 -threads 5 -maxCharLength 100000 -quiet True -serverProperties corenlp_server-2e15724b8064491b.props -preload -outputFormat serialized

我安装了最新版本的节,并且 运行正在 VS 代码中从 .ipynb 文件中获取以下代码:

# sample sentence
sentence = "Albert Einstein was a German-born theoretical physicist." 

# start the client as indicated in the docs
with CoreNLPClient(properties='corenlp_server-2e15724b8064491b.props', endpoint='https://localhost:7998', memory='8G', be_quiet=True) as client:
     matches = client.tregex(text=sentence, pattern = 'NP')

# extract the noun phrases and their indices
noun_phrases = [[text, begin, end] for text, begin, end in
     zip([sentence[match_id]['spanString'] for sentence in matches['sentences'] for match_id in sentence],
         [sentence[match_id]['characterOffsetBegin'] for sentence in matches['sentences'] for match_id in sentence],
         [sentence[match_id]['characterOffsetEnd'] for sentence in matches['sentences'] for match_id in sentence])]

主要问题:如何确保服务器在打开的端口上启动,然后关闭?我更喜欢使用半自动方式来查找打开/关闭客户端占用的端口为运行 on.

经过 2 小时的研究,我现在知道以下内容:

  1. 考虑到 python 使用端口 9000 不是一个选项。非正式证据表明这必须使用 jupyter notebook 而不是 'regular' python .py 文件。

  2. 关于客户端在使用其他端点时不关闭:我应该简单地使用 http://localhost:port' 而不是 https://...

希望这可以帮助其他正在努力解决这个问题的人。我想这是我非计算机科学背景的渗透。

(已编辑以解决拼写错误)

一般来说,选择另一个没有其他人使用的号码就足够了——也许是 9017?有很多号码可供选择!但是更谨慎的选择是使用 try/catch 在 while 循环中创建 CoreNLPClient 并增加端口号直到找到一个打开的端口号。