Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
ner
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hxw
ner
Commits
fd1fe9bd
Commit
fd1fe9bd
authored
May 04, 2018
by
hxw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ner
parent
3f98cdd8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
100 additions
and
0 deletions
+100
-0
filt_sents.py
filt_sents.py
+100
-0
No files found.
filt_sents.py
0 → 100644
View file @
fd1fe9bd
import
os
import
random
from
collections
import
OrderedDict
import
pickle
import
json
import
click
T_Q_MAP
=
{
"PER"
:
"人名 是 什么"
,
"LOC"
:
"地址 是 什么"
,
"ORG"
:
"机构 是 什么"
}
T_T2_MAP
=
{
"PER"
:
"nr"
,
"LOC"
:
"ns"
,
"ORG"
:
"nt"
}
@click.command
()
@click.option
(
'--inputfile'
,
default
=
'../baike_sents_full.jsonl'
)
@click.option
(
'--outputfile'
,
default
=
"../train_stage1.jsonl"
)
@click.option
(
"--triefile"
)
@click.option
(
'--option'
)
def
get_ner
(
inputfile
,
outputfile
,
triefile
,
option
):
trie
=
pickle
.
load
(
open
(
triefile
,
'rb'
))
write_file
=
open
(
outputfile
,
'w'
)
root
=
{}
with
open
(
inputfile
,
'r'
)
as
fn
:
for
line
in
fn
:
content
=
json
.
loads
(
line
)
sentence
=
''
.
join
(
content
[
"sentence"
]
.
strip
()
.
split
())
sentId
=
content
[
"sentId"
]
sent_len
=
len
(
sentence
)
if
len
(
sentence
)
>
100
:
continue
used_list
=
[]
wordtype
=
{}
in_flag
=
True
for
Type
in
[
"PER"
,
"LOC"
,
"ORG"
]:
idx
=
0
;
rev_idx
=
sent_len
-
1
i_e_list
=
[]
while
idx
<
sent_len
and
in_flag
:
idx
,
entity_span
=
get_entity
(
sentence
,
idx
,
sent_len
,
Type
,
trie
)
if
(
len
(
entity_span
)
>
1
):
i_e_list
.
append
(
entity_span
)
used_list
.
append
(
entity_span
)
in_flag
=
False
while
rev_idx
>=
0
and
in_flag
:
rev_idx
,
entity_span
=
rev_get_entity
(
sentence
,
rev_idx
,
sent_len
,
Type
,
trie
)
if
(
len
(
entity_span
)
>
1
):
i_e_list
.
append
(
entity_span
)
in_flag
=
False
for
entity_span
in
i_e_list
:
flag
=
1
for
s
in
used_list
:
jiaoji
=
entity_span
.
intersection
(
s
)
if
len
(
jiaoji
)
>
1
:
if
len
(
entity_span
)
<
len
(
s
)
or
jiaoji
==
s
:
flag
=
0
else
:
used_list
.
remove
(
s
)
if
flag
:
used_list
.
append
(
entity_span
)
wordtype
[
tuple
(
entity_span
)]
=
Type
break
if
(
len
(
used_list
)
==
0
and
option
==
'leave_ne'
):
write_file
.
write
(
json
.
dumps
({
"sentence"
:
sentence
,
"sentId"
:
sentId
})
+
'
\n
'
)
elif
(
len
(
used_list
)
!=
0
and
option
==
'save_ne'
):
write_file
.
write
(
json
.
dumps
({
"sentence"
:
sentence
,
"sentId"
:
sentId
})
+
'
\n
'
)
write_file
.
close
()
def
get_entity
(
sentence
,
idx
,
sent_len
,
Type
,
trie
):
origin_idx
=
idx
root_node
=
trie
[
Type
]
node
=
root_node
while
(
idx
<
sent_len
and
sentence
[
idx
]
in
node
):
node
=
node
[
sentence
[
idx
]]
idx
+=
1
if
node
!=
root_node
and
'/-/-'
in
node
:
return
idx
,
set
(
range
(
origin_idx
,
idx
))
else
:
return
origin_idx
+
1
,
set
()
def
rev_get_entity
(
sentence
,
idx
,
sent_len
,
Type
,
trie
):
origin_idx
=
idx
root_node
=
trie
[
"rev_"
+
Type
]
node
=
root_node
while
(
idx
>=
0
and
sentence
[
idx
]
in
node
):
node
=
node
[
sentence
[
idx
]]
idx
-=
1
if
(
node
!=
root_node
and
'/-/-'
in
node
):
return
idx
,
set
(
range
(
idx
+
1
,
origin_idx
+
1
))
return
origin_idx
-
1
,
set
()
if
__name__
==
"__main__"
:
get_ner
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment