python nlp 切分句子
时间: 2023-06-22 11:29:59 浏览: 53
在Python中,可以使用NLTK(自然语言工具包)和Spacy等库来进行句子切分。以下是一个 NLTK 的示例:
```python
import nltk
nltk.download('punkt') # 下载必要数据
text = "This is the first sentence. This is the second sentence. This is the third sentence."
sentences = nltk.sent_tokenize(text)
print(sentences)
```
这将输出:
```
['This is the first sentence.', 'This is the second sentence.', 'This is the third sentence.']
```
Spacy也提供了类似的功能:
```python
import spacy
nlp = spacy.load('en_core_web_sm')
text = "This is the first sentence. This is the second sentence. This is the third sentence."
doc = nlp(text)
sentences = [sent.string.strip() for sent in doc.sents]
print(sentences)
```
这将输出:
```
['This is the first sentence.', 'This is the second sentence.', 'This is the third sentence.']
```