python - Spacy 将空格识别为实体

标签 python nlp spacy conll

我刚刚开始使用 Spacy,并输入了一段文本来测试它如何处理我使用 AntFileConverter 进行 OCR 的 pdf。

txt 文件(下面的示例 - 将附加但不确定如何附加)看起来不错,采用 UTF-8 格式。然而,当我以 CONLL 格式输出文件时,由于某种原因,存在各种明显的间隙,这些间隙没有原始单词,但似乎已被识别。这种情况发生在句子的末尾和句子内。

“北半球许多水域的物种。在 该地区大多数国家派克都拥有商业 和娱乐值(value)(Crossman & Casselman 1987; 拉特 1988)。派克是典型的坐等捕食者 通常通过伏击来捕食猎物(Webb & 斯卡德森 1980)。”

输出如下:

        GPE 24  
26  species specie  NNS     20  attr
27  in  in  IN      26  prep
28  many    many    JJ      29  amod
29  waters  water   NNS     27  pobj
30  in  in  IN      29  prep
31  the the DT      33  det
32  northern    northern    JJ      33  amod
33  hemisphere  hemisphere  NN      30  pobj
34  .   .   .       20  punct
1   In  in  IN      9   prep
2   

        GPE 1   
3   most    most    JJS     4   amod
4   countries   country NNS     9   nsubj
5   in  in  IN      4   prep
6   the the DT      8   det
7   region  region  NN      8   compound
8   pike    pike    NN      5   pobj
9   has have    VBZ     0   ROOT
10  both    both    DT      11  preconj
11  commercial  commercial  JJ      9   dobj
12  

        GPE 11  
13  and and CC      11  cc
14  recreational    recreational    JJ      15  amod
15  value   value   NN      11  conj
16  (   (   -LRB-       15  punct
17  Crossman    crossman    NNP ORG 15  appos
18  &   &   CC  ORG 17  cc
19  Casselman   casselman   NNP ORG 17  conj
20  1987    1987    CD  DATE    17  nummod
21  ;   ;   :       15  punct
22  

        GPE 21  
23  Raat    raat    NNP     15  appos
24  1988    1988    CD  DATE    23  nummod
25  )   )   -RRB-       15  punct
26  .   .   .       9   punct
1   Pike    pike    NNP     2   nsubj
2   is  be  VBZ     0   ROOT
3   a   a   DT      10  det
4   typical typical JJ      10  amod
5   sit sit NN      10  nmod
6   -   -   HYPH        5   punct
7   and and CC      5   cc
8   -   -   HYPH        9   punct
9   wait    wait    VB      5   conj
10  predator    predator    NN      2   attr
11  

        GPE 10  
12  which   which   WDT     14  nsubj
13  usually usually RB      14  advmod
14  hunts   hunt    VBZ     10  relcl
15  prey    prey    NN      14  dobj
16  by  by  IN      14  prep
17  ambushing   ambush  VBG     16  pcomp
18  (   (   -LRB-       17  punct
19  Webb    webb    NNP     17  conj
20  &   &   CC      19  cc
21  

我也尝试过不打印 NER,但这些间隙仍然被标记。我认为这可能与换行符有关,因此我也尝试了 Linux 风格的 EOL,但这没有任何区别。

这是我正在使用的代码:

import spacy
import en_core_web_sm
nlp_en = en_core_web_sm.load()
input = open('./input/55_linux.txt', 'r').read()
doc = nlp_en(input)
for sent in doc.sents:
        for i, word in enumerate(sent):
              if word.head == word:
                 head_idx = 0
              else:
                 head_idx = word.head.i - sent[0].i + 1
              output = open('CONLL_output.txt', 'a')
              output.write("%d\t%s\t%s\t%s\t%s\t%s\t%s\n"%(
                 i+1, # There's a word.i attr that's position in *doc*
                  word,
                  word.lemma_,
                  word.tag_, # Fine-grained tag
                  word.ent_type_,
                  str(head_idx),
                  word.dep_ # Relation
                 ))

还有其他人遇到过这个问题吗?如果是这样,你知道我该如何解决吗?

最佳答案

这是一个known bugspaCy中。

在修复之前,您将必须进行一些后处理以消除那些“空白”实体。幸运的是,这很简单,这个片段由 author 发布。库的说明如何:

def remove_whitespace_entities(doc):
    doc.ents = [e for e in doc.ents if not e.text.isspace()]
    return doc

nlp_en.add_pipe(remove_whitespace_entities, after='ner')

因此,您首先定义一个后处理管道,用于过滤具有仅由空格字符组成的文本的所有实体(使用 isspace() )。

然后将此管道添加到 NLP 管道,设置为在 NER 之后运行。此后任何时候您使用 nlp_en 时,它都不会返回这些实体。

关于python - Spacy 将空格识别为实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52859211/

相关文章:

python - 有没有办法在 Matplotlib 中制作不连续的轴?

python - 如何使用 SpaCy 从句子列表中获取名词短语

python - 使用python midi从midi中提取鼓音符

python - 如何使用 python pandas 找到 Shapiro-Wilk?

python - 将 JSON 插入 MySQL

java - Google NLP api 提供找不到 TLS ALPN 提供程序;没有可用的 netty-tcnative、Conscrypt 或 Jetty NPN/ALPN

java - 使用斯坦福类型的解析器从文本文件中提取名词短语

machine-learning - CRF++/Wapiti 包括整个句子的类别作为特征

python - 带有 Jupyter Notebook 的 Textacy : How to suppress multiple error warnings?

python - Spacy - 使用 PhraseMatcher 创建嵌套分类法