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
2e063177
Commit
2e063177
authored
Apr 20, 2018
by
hxw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test
parents
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
149 additions
and
0 deletions
+149
-0
README.md
README.md
+0
-0
Trie.data
Trie.data
+0
-0
dict2Trie.py
dict2Trie.py
+57
-0
get_ner4test.py
get_ner4test.py
+92
-0
No files found.
README.md
0 → 100644
View file @
2e063177
Trie.data
0 → 100644
View file @
2e063177
File added
dict2Trie.py
0 → 100644
View file @
2e063177
#conding:utf-8
import
os
import
click
import
pickle
from
collections
import
defaultdict
type_dict
=
{
"地名"
:
"LOC"
,
"人名"
:
"PER"
,
"机构名称"
:
"ORG"
}
word_dict
=
{}
word_first_char
=
defaultdict
(
list
)
Trie
=
{}
@click.command
()
@click.option
(
'--dict_dir'
,
default
=
"词库"
)
def
get_dict
(
dict_dir
):
Tries
=
Trie
()
for
dir_name
in
os
.
listdir
(
dict_dir
):
print
(
dir_name
)
for
file_name
in
os
.
listdir
(
os
.
path
.
join
(
dict_dir
,
dir_name
)):
with
open
(
os
.
path
.
join
(
dict_dir
,
dir_name
,
file_name
),
'r'
,
encoding
=
'utf-8'
)
as
fn
:
for
line
in
fn
:
text
=
line
.
strip
()
if
text
==
''
:
continue
Tries
.
add
(
type_dict
[
dir_name
],
text
)
Tries
.
add
(
"rev_"
+
type_dict
[
dir_name
],
text
[::
-
1
])
pickle
.
dump
(
Trie
.
root
,
open
(
"Trie.data"
,
'wb'
))
class
Trie
:
root
=
{
"LOC"
:{},
"PER"
:{},
"ORG"
:{},
"rev_LOC"
:{},
"rev_PER"
:{},
"rev_ORG"
:{}}
END
=
'/-/-/-'
def
add
(
self
,
ner_type
,
text
):
node
=
self
.
root
[
ner_type
]
if
(
len
(
text
)
<
2
):
text
=
""
for
word
in
text
:
node
.
setdefault
(
word
,{})
node
=
node
[
word
]
node
[
self
.
END
]
=
None
if
__name__
==
'__main__'
:
get_dict
()
get_ner4test.py
0 → 100644
View file @
2e063177
import
pickle
import
click
import
json
import
tqdm
from
collections
import
OrderedDict
Trie
=
pickle
.
load
(
open
(
'Trie.data'
,
'rb'
))
@click.command
()
@click.option
(
'--file_name'
,
default
=
'baike_sents_full.jsonl'
)
def
get_ner
(
file_name
):
count1
=
0
count2
=
0
with
open
(
file_name
,
'r'
)
as
data
:
for
line
in
data
:
sentence
,
answer
=
line
.
strip
()
.
split
(
'|'
)
answers
=
answer
.
split
(
" "
)
sent_len
=
len
(
sentence
)
ner_list
=
[]
used_idx
=
set
()
used_list
=
[]
for
Type
in
[
"PER"
,
"LOC"
,
"ORG"
]:
idx
=
0
;
rev_idx
=
sent_len
-
1
i_w_list
=
[]
while
(
idx
<
sent_len
):
[
idx
,
word_used_idx
]
=
get_entity
(
sentence
,
idx
,
sent_len
,
Type
)
i_w_list
.
append
([
idx
,
word_used_idx
])
while
(
rev_idx
>=
0
):
[
rev_idx
,
word_used_idx
]
=
rev_get_entity
(
sentence
,
rev_idx
,
sent_len
,
Type
)
i_w_list
.
append
([
idx
,
word_used_idx
])
for
idx
,
word_used_idx
in
i_w_list
:
flag
=
1
if
(
len
(
word_used_idx
)
>
1
):
for
s
in
used_list
:
jiaoji
=
word_used_idx
.
intersection
(
s
)
if
(
jiaoji
!=
set
()):
if
(
len
(
word_used_idx
)
<
len
(
s
)
or
jiaoji
==
s
):
flag
=
0
else
:
used_list
.
remove
(
s
)
if
(
flag
):
used_list
.
append
(
word_used_idx
)
l
=
[]
for
s
in
used_list
:
begin
=
min
(
s
)
end
=
max
(
s
)
+
1
l
.
append
(
sentence
[
begin
:
end
]
)
print
(
set
(
l
))
print
(
set
(
answers
))
def
get_entity
(
sentence
,
idx
,
sent_len
,
Type
):
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
):
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
))]
else
:
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