使用spaCy NLP执行句子标记生成器并将其写入Pandas Dataframe。
# -*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import unicode_literals
# Extraction
import spacy,en_core_web_sm
import pandas as pd
# Read the text file
nlp = en_core_web_sm.load()
doc = nlp(unicode(open('o.txt').read().decode('utf8')) )
for idno, sentence in enumerate(doc.sents):
print 'Sentence {}:'.format(idno + 1), sentence
Sentences = list(doc.sents)
df = pd.DataFrame(Sentences)
print df
输出:
Sentence 1: This is a sample sentence.
Sentence 2: This is a second sample sentence.
Sentence 3: This is a third sample sentence.
0 1 2 3 4 5 6
0 This is a sample sentence . None
1 This is a second sample sentence .
2 This is a third sample sentence .
Pandas的预期输出
0
0 This is a sample sentence.
1 This is a second sample sentence.
2 This is a third sample sentence.
如何实现预期的产出?
答案 0 :(得分:3)
您应该能够使用<body><div id='final'><div><button id="EL2">OK</button></div></div></body>
并调整参数以将文本导入到单列,我们将其称为df ['text']。
然后尝试:
pd.read_table(input_file_path)
您将在新列中列出句子标记。
祝你好运!
答案 1 :(得分:2)
您可以做的是构建一个列表,然后将其转换为Dataframe
# -*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import unicode_literals
# Extraction
import spacy,en_core_web_sm
import pandas as pd
# Read the text file
nlp = en_core_web_sm.load()
doc = nlp(unicode(open('o.txt').read().decode('utf8')) )
d = []
for idno, sentence in enumerate(doc.sents):
d.append({"id": idno, "sentence":str(sentence)})
print 'Sentence {}:'.format(idno + 1), sentence
df = pd.DataFrame(d)
df.set_index('id', inplace=True)
print df