我正在尝试使用斯坦福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"]
我做了什么:
- NLTK sent_tokenizer((; 不拆分换行符,似乎不如 stanfordnlp 准确
- 斯坦福NLP拆分很棒,但示例输出不在句子列表中
我听说有一个使用斯坦福nlp库的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
我会使用普通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
也许在源代码中,它可以找到如何将文本拆分为句子并使用它。