首页 > 解决方案 > Stanfordnlp python - 句子拆分和其他简单功能

问题描述

我正在尝试使用斯坦福 NLP 解析器将字符串拆分为句子,我使用了斯坦福 NLP 提供的示例代码,但它给了我单词而不是句子。

这是示例输入:

"this is sample input. I want to split this text into a list of sentences. Please help"

这是我想要的输出:

["this is sample input.", "I want to split this text into a list of sentences.", "Please help"]

我做了什么:

我听说有一个使用 stanfordnlp 库的 nltk 解析器,但我无法获得任何示例指南。

在这一点上,我很困惑,因为斯坦福 NLP 几乎没有详尽的 Python 指南。这个任务必须使用 python,因为我研究中的其他组件使用 python 来处理数据。请帮忙!谢谢你。

示例代码:

import stanfordnlp

nlp = stanfordnlp.Pipeline(processors='tokenize', lang='en')
doc = nlp(a)
for i, sentence in enumerate(doc.sentences):
    print(f"====== Sentence {i+1} tokens =======")
    print(*[f"index: {token.index.rjust(3)}\ttoken: {token.text}" for token in sentence.tokens], sep='\n')
print(doc.sentences.tokens.text[2])

输出:

====== Sentence 84 tokens =======
index:   1  token: Retweet
index:   2  token: 10
index:   3  token: Like
index:   4  token: 83
index:   5  token: End
index:   6  token: of
index:   7  token: conversation
index:   8  token: ©
index:   9  token: 2019
index:  10  token: Twitter
index:  11  token: About
index:  12  token: Help
index:  13  token: Center
index:  14  token: Terms
index:  15  token: Privacy
index:  16  token: policy
====== Sentence 85 tokens =======
index:   1  token: Cookies
index:   2  token: Ads
index:   3  token: info

来源:https ://stanfordnlp.github.io/stanfordnlp/pipeline.html

标签: pythonstanford-nlp

解决方案


我会使用 normal但如果句子以or等split('.')​​结尾它将不起作用。它需要但它仍然可以将句子内部视为三个句子的结尾。?!regex...


stanfordnlp我只能在句子中连接单词,因此它将句子作为一个字符串给出,但是这种简单的方法在之前添加了空格等,.?!

import stanfordnlp

text = "this is ... sample input. I want to split this text into a list of sentences. Can you? Please help"

nlp = stanfordnlp.Pipeline(processors='tokenize', lang='en')
doc = nlp(text)

for i, sentence in enumerate(doc.sentences):
    sent = ' '.join(word.text for word in sentence.words)
    print(sent)

结果

this is ... sample input .
I want to split this text into a list of sentences .
Can you ?
Please help

也许在源代码中它可以找到如何将文本拆分为句子并使用它。


推荐阅读