Spacy 将空格识别为实体
Spacy identifying blank spaces as entities
我刚刚开始使用 Spacy 并通过文本测试它如何处理我使用 AntFileConverter 进行 OCR 的 pdf。
txt 文件(下面的示例 - 会附加但不确定如何附加)看起来不错,是 UTF-8 格式的。但是当我以CONLL格式输出文件时,不知为何出现了各种明显的空隙,这些空隙没有原词,但似乎已经被识别出来了。这发生在句末和句内。
”北半球许多水域的物种。在
该地区大多数国家的梭子鱼都有商业用途
和娱乐价值(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
))
还有其他人遇到过这个问题吗?如果是这样,你知道我该如何解决吗?
这是 spaCy 中的 known bug。
在修复之前,您将不得不进行一些 post 处理以摆脱那些 "blank" 实体。幸运的是,这很容易,由库的 author 编辑的这个片段 post 展示了如何:
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')
因此,您首先定义一个 post 处理管道,用于过滤所有具有 text
且仅包含空白字符(使用 isspace()
)的实体。
然后你将这个管道添加到NLP管道中,在NER之后设置为运行。然后任何时候你使用 nlp_en
之后,它都不会 return 那些实体。
我刚刚开始使用 Spacy 并通过文本测试它如何处理我使用 AntFileConverter 进行 OCR 的 pdf。
txt 文件(下面的示例 - 会附加但不确定如何附加)看起来不错,是 UTF-8 格式的。但是当我以CONLL格式输出文件时,不知为何出现了各种明显的空隙,这些空隙没有原词,但似乎已经被识别出来了。这发生在句末和句内。
”北半球许多水域的物种。在 该地区大多数国家的梭子鱼都有商业用途 和娱乐价值(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
))
还有其他人遇到过这个问题吗?如果是这样,你知道我该如何解决吗?
这是 spaCy 中的 known bug。
在修复之前,您将不得不进行一些 post 处理以摆脱那些 "blank" 实体。幸运的是,这很容易,由库的 author 编辑的这个片段 post 展示了如何:
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')
因此,您首先定义一个 post 处理管道,用于过滤所有具有 text
且仅包含空白字符(使用 isspace()
)的实体。
然后你将这个管道添加到NLP管道中,在NER之后设置为运行。然后任何时候你使用 nlp_en
之后,它都不会 return 那些实体。