【发布时间】:2021-09-21 06:24:06
【问题描述】:
我正在尝试使用 spaCy 将句子拆分为子句,以便使用 MLLib 进行分类。我已经搜索了两种我认为最好的解决方案之一,但运气不太好。
-
选项:将使用文档中的标记,即
token.pos_与SCONJ匹配并拆分为一个句子。 -
选项:将使用 spaCy 所拥有的任何值作为它标识为
SCONJ的值的字典来创建一个列表
1 的问题是我只有 .text、.i 和没有 .pos_ 作为自定义边界(据我所知,需要在解析器之前运行。
2 的问题是我似乎找不到字典。这也是一种非常 hacky 的方法。
import deplacy
from spacy.language import Language
# Uncomment to visualise how the tokens are labelled
# deplacy.render(doc)
custom_EOS = ['.', ',', '!', '!']
custom_conj = ['then', 'so']
@Language.component("set_custom_boundaries")
def set_custom_boundaries(doc):
for token in doc[:-1]:
if token.text in custom_EOS:
doc[token.i + 1].is_sent_start = True
if token.text in custom_conj:
doc[token.i].is_sent_start = True
return doc
def set_sentence_breaks(doc):
for token in doc:
if token == "SCONJ":
doc[token.i].is_sent_start = True
def main():
text = "In the add user use case, we need to consider speed and reliability " \
"so use of a relational DB would be better than using SQLite. Though " \
"it may take extra effort to convert @Bot"
nlp = spacy.load("en_core_web_sm")
nlp.add_pipe("set_custom_boundaries", before="parser")
doc = nlp(text)
# for token in doc:
# print(token.pos_)
print("Sentences:", [sent.text for sent in doc.sents])
if __name__ == "__main__":
main()
电流输出
句子:['在添加用户用例中,',
'我们需要考虑速度和可靠性,
'所以使用关系数据库会比使用 SQLite 更好。',
'虽然转换@Bot可能需要额外的努力']
【问题讨论】: