python空间向后查找块(在引用之前)

2024-04-18 07:42:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个NLP项目的空间。 使用Spacy创建文档时,可以通过以下方式找到文本中的名词块(也称为“名词短语”):

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"The companies building cars do not want to spend more money in improving diesel engines because the government will not subsidise such engines anymore.")
for chunk in doc.noun_chunks:
    print(chunk.text)

这将给出一个名词短语的列表。你知道吗

例如,在本例中,第一个名词短语是“公司”。你知道吗

假设您有一个文本,其中名词块引用了一个数字。你知道吗

比如:

doc=nlp(the Window (23) is closed because the wall (34) of the beautiful building (45) is not covered by the insurance (45))

假设我有代码来识别引用,例如标记它们:

myprocessedtext=the Window <ref>(23)</ref> is closed because the wall <ref>(34)</ref> of the beautiful building <ref>(45)</ref> is not covered by the insurance <ref>(45)</ref>

我怎样才能得到紧挨着引用前面的名词块(名词短语)?你知道吗

我的想法是:将每个引用之前的10个单词传递给一个spacy doc对象,提取名词块并得到最后一个。这是非常低效的,因为创建doc对象非常耗时。你知道吗

不需要创建额外的nlp对象,还有其他想法吗?你知道吗

谢谢。你知道吗


Tags: the对象in文本refdocnlpspacy
1条回答
网友
1楼 · 发布于 2024-04-18 07:42:55

您可以分析整个文档,然后根据标记位置或字符偏移量在每个引用之前找到名词块。名词块中最后一个标记的标记偏移量是noun_chunk[-1].i,最后一个标记开始的字符偏移量是noun_chunk[-1].idx。(检查分析是否不受引用字符串的影响;示例(1)样式的引用似乎被分析为同位语,这很好。)

如果分析受引用字符串的影响,请将它们从文档中删除,同时跟踪它们的字符偏移量,分析整个文档,然后找到保存位置之前的名词块。你知道吗

相关问题 更多 >