Python 将空白空间标识为实体的空间
我刚刚开始使用Spacy,并通过一个文本来测试它是如何使用AntFileConverter处理pdf的 txt文件(下面的示例-将附加,但不确定如何附加)看起来很好,是UTF-8格式的。但是,当我以CONLL格式输出文件时,由于某种原因,存在各种明显的间隙,这些间隙没有原始单词,但似乎已被识别。这发生在句末和句内 北半球许多水域的物种。在 该地区的大多数国家派克既有商业用途,也有商业用途 和娱乐价值(Crossman&Casselman 1987; 派克是一种典型的坐等捕食者 通常通过伏击捕猎猎物(韦伯& 斯卡森1980年) 输出结果如下所示:Python 将空白空间标识为实体的空间,python,nlp,spacy,conll,Python,Nlp,Spacy,Conll,我刚刚开始使用Spacy,并通过一个文本来测试它是如何使用AntFileConverter处理pdf的 txt文件(下面的示例-将附加,但不确定如何附加)看起来很好,是UTF-8格式的。但是,当我以CONLL格式输出文件时,由于某种原因,存在各种明显的间隙,这些间隙没有原始单词,但似乎已被识别。这发生在句末和句内 北半球许多水域的物种。在 该地区的大多数国家派克既有商业用途,也有商业用途 和娱乐价值(Crossman&Casselman 1987; 派克是一种典型的坐等捕食者 通常通过伏击捕猎猎
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
))
还有其他人有这个问题吗?如果是的话,你知道我怎么解决吗 这是一个空白
在修复之前,您必须进行一些后处理以除去那些“空白”实体。幸运的是,这很简单,库的管理员发布的这段代码片段展示了如何:
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')
因此,首先定义一个后处理管道,该管道过滤所有实体,其中文本
仅由空白字符组成(使用)
然后将此管道添加到NLP管道,设置为在NER之后运行。然后,在此之后,无论何时使用nlp_en
,它都不会返回这些实体