python - 如何枚举用SpaCy一一分隔的段落的句子
问题描述
我想阅读一段用 SpaCy 分隔的段落的句子。然而,当我试图列举句子时,我列举的是单词而不是句子。的确,
text = predicted.iloc[0,5]
sentences = spacy_nlp(text)
print(sentences)
for i,sent in enumerate(sentences):
print("---",i,"---")
print(sent)
首先给出 SpaCy 的句子:
['Beyoncé Giselle Knowles-Carter (/biːˈjɒnseɪ/ bee-YON-say) (born September 4, 1981) is an American singer, songwriter, record producer and actress.', "Born and raised in Houston, Texas, she performed in various singing and dancing competitions as a child, and rose to fame in the late 1990s as lead singer of R&B girl-group Destiny's Child.", "Managed by her father, Mathew Knowles, the group became one of the world's best-selling girl groups of all time.", 'Their hiatus saw the release of Beyoncé\'s debut album, Dangerously in Love (2003), which established her as a solo artist worldwide, earned five Grammy Awards and featured the Billboard Hot 100 number-one singles "Crazy in Love" and "Baby Boy".']
但随后它列举了单词而不是句子:
--- 0 ---
[
--- 1 ---
'
--- 2 ---
Beyoncé
--- 3 ---
Giselle
--- 4 ---
Knowles
--- 5 ---
-
--- 6 ---
Carter
...
PS:
感谢 X 将其转换为列表的想法使我能够逐句编写它。
然而,整个想法是让它发挥nltk_spacy_tree()
作用,它似乎只接受类型的对象,spacy.tokens.doc.Doc
所以我做了以下似乎不太适应的事情。看起来太复杂了:
text = predicted.iloc[0,5]
sentences = list(spacy_nlp(text))
sentences = en_nlp(predicted["context"][0].lower()).sents
#print(type(en_nlp(sentences)))
for i,sent in enumerate(sentences):
print("---",i,"---")
print(en_nlp(str(sent)))
sent = en_nlp(str(sent))
tree = nltk_spacy_tree(sent)
print(tree)
解决方案
推荐阅读
- python - 将连接查询的结果聚合到列表中
- javascript - PDF 表格大写
- javascript - 如何保留我的网页的按钮选择状态?
- apache-spark - 当指定存储级别时,在 pyspark2 中持久化数据帧不起作用。我究竟做错了什么?
- python - 如何在自训练的 word2vec 模型中删除单词
- laravel - vue中如何使用axios获取id选择的数据
- c - 如何读取文本文件并将其内容存储到另一个字符串中?C
- javascript - Object.keys(myObject).forEach() 会同步执行吗?
- powerbi - 如何在 Power BI 查询编辑器中为最大日期添加自定义列?
- javascript - 使用 Puppeteer,如何在无需实际单击链接并加载页面的情况下获取最终的重定向 URI?