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
9a0b4264
Commit
9a0b4264
authored
Apr 26, 2018
by
hxw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
94e1ce5f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
17 deletions
+88
-17
dict2Trie.py
dict2Trie.py
+88
-17
No files found.
dict2Trie.py
View file @
9a0b4264
#conding:utf-8
#conding:utf-8
import
os
import
os
import
click
import
random
import
json
import
pickle
import
pickle
from
collections
import
defaultdict
import
click
type_dict
=
{
"地名"
:
"LOC"
,
"人名"
:
"PER"
,
"机构名称"
:
"ORG"
}
TYPE_DICT
=
{
"地名"
:
"LOC"
,
"人名"
:
"PER"
,
"机构名称"
:
"ORG"
}
word_dict
=
{}
TYPE_NUM_LIMIT
=
{
"LOC"
:
1000000
,
"PER"
:
1000000
,
"ORG"
:
1000000
}
word_first_char
=
defaultdict
(
list
)
WORD_DICT
=
{}
Trie
=
{}
'''
get ner from word vocabulary directories
'''
@click.command
()
@click.command
()
@click.option
(
'--dict_dir'
,
default
=
"词库"
)
@click.option
(
'--dict_dir'
,
default
=
"词库"
)
def
get_dict
(
dict_dir
):
def
get_dict
(
dict_dir
):
Tries
=
Trie
()
trie
=
Trie
()
for
dir_name
in
os
.
listdir
(
dict_dir
):
for
dir_name
in
os
.
listdir
(
dict_dir
):
print
(
dir_name
)
print
(
dir_name
)
for
file_name
in
os
.
listdir
(
os
.
path
.
join
(
dict_dir
,
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
:
with
open
(
os
.
path
.
join
(
dict_dir
,
dir_name
,
file_name
),
'r'
,
encoding
=
'utf-8'
)
as
fn
:
for
line
in
fn
:
for
line
in
fn
:
text
=
line
.
strip
()
text
=
line
.
strip
()
if
text
==
''
:
if
text
==
''
:
continue
continue
Tries
.
add
(
type_dict
[
dir_name
],
text
)
trie
.
add
(
TYPE_DICT
[
dir_name
],
text
)
Tries
.
add
(
"rev_"
+
type_dict
[
dir_name
],
text
[::
-
1
])
trie
.
add
(
"rev_"
+
TYPE_DICT
[
dir_name
],
text
[::
-
1
])
pickle
.
dump
(
Trie
.
root
,
open
(
"Trie.data"
,
'wb'
))
pickle
.
dump
(
trie
.
root
,
open
(
"Trie.data"
,
'wb'
))
'''
get ner from jsonl file
'''
@click.command
()
@click.option
(
'--dict_dir'
,
default
=
"../data"
)
def
get_dict_from_json
(
dict_dir
):
trie
=
Trie
()
for
file_name
in
os
.
listdir
(
dict_dir
):
if
'.jsonl'
in
file_name
:
print
(
file_name
)
with
open
(
os
.
path
.
join
(
dict_dir
,
file_name
),
'r'
,
encoding
=
'utf-8'
)
as
fn
:
for
line
in
fn
:
content
=
json
.
loads
(
line
.
strip
())
word
=
content
[
"word"
]
.
strip
()
label
=
content
[
"label"
]
.
strip
()
if
word
==
''
:
continue
trie
.
add
(
TYPE_DICT
[
label
],
word
)
trie
.
add
(
'rev_'
+
TYPE_DICT
[
label
],
word
[::
1
])
pickle
.
dump
(
trie
.
root
,
open
(
"Trie.data"
,
'wb'
))
def
into_trie
(
trie
,
word_list
,
Type
,
span
):
i
=
span
[
0
]
while
i
<
span
[
1
]:
trie
.
add
(
Type
,
word_list
[
i
])
trie
.
add
(
"rev_"
+
Type
,
word_list
[
i
][::
1
])
i
+=
1
'''
seperate vocabulary for train and test set
'''
@click.command
()
@click.option
(
'--dict_dir'
,
default
=
"../data"
)
def
train_and_test
(
dict_dir
):
trie1
=
Trie
()
trie2
=
Trie
()
type2word_list
=
{
"PER"
:
set
(),
"ORG"
:
set
(),
"LOC"
:
set
()}
for
file_name
in
os
.
listdir
(
dict_dir
):
if
'.jsonl'
in
file_name
:
print
(
file_name
)
with
open
(
os
.
path
.
join
(
dict_dir
,
file_name
),
'r'
,
encoding
=
'utf-8'
)
as
fn
:
for
line
in
fn
:
content
=
json
.
loads
(
line
.
strip
())
word
=
content
[
'word'
]
.
strip
()
label
=
content
[
"label"
]
type2word_list
[
TYPE_DICT
[
label
]]
.
add
(
word
)
for
Type
in
[
"PER"
,
"LOC"
,
"ORG"
]:
print
(
"processing "
+
Type
)
word_list
=
list
(
type2word_list
[
Type
])
length
=
len
(
word_list
)
length
=
length
if
length
<=
TYPE_NUM_LIMIT
[
Type
]
else
TYPE_NUM_LIMIT
[
Type
]
print
(
Type
,
length
)
into_trie
(
trie1
,
word_list
,
Type
,[
0
,
int
(
length
/
2
)])
into_trie
(
trie2
,
word_list
,
Type
,[
0
,
length
])
type2word_list
[
Type
]
=
[]
print
(
"finally1"
)
pickle
.
dump
(
trie1
.
root
,
open
(
"../data/trie4train.data"
,
'wb'
))
print
(
"finally2"
)
pickle
.
dump
(
trie2
.
root
,
open
(
"../data/trie4test.data"
,
'wb'
))
print
(
"finally3"
)
return
class
Trie
:
class
Trie
:
root
=
{
"LOC"
:{},
"PER"
:{},
"ORG"
:{},
"rev_LOC"
:{},
"rev_PER"
:{},
"rev_ORG"
:{}}
def
__init__
(
self
):
END
=
'/-/-/-'
self
.
root
=
{
"LOC"
:{},
"PER"
:{},
"ORG"
:{},
"rev_LOC"
:{},
"rev_PER"
:{},
"rev_ORG"
:{}}
self
.
END
=
'/-/-'
def
add
(
self
,
ner_type
,
text
):
def
add
(
self
,
ner_type
,
text
):
node
=
self
.
root
[
ner_type
]
node
=
self
.
root
[
ner_type
]
if
(
len
(
text
)
<
2
):
if
(
len
(
text
)
<
2
):
text
=
""
text
=
""
for
word
in
text
:
for
word
in
text
:
node
.
setdefault
(
word
,{})
node
.
setdefault
(
word
,
{})
node
=
node
[
word
]
node
=
node
[
word
]
node
[
self
.
END
]
=
None
node
[
self
.
END
]
=
None
...
@@ -51,7 +122,7 @@ class Trie:
...
@@ -51,7 +122,7 @@ class Trie:
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
get_dic
t
()
train_and_tes
t
()
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