Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Toponym Geocoding
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jacques Fize
Toponym Geocoding
Commits
5c0d03fa
Commit
5c0d03fa
authored
4 years ago
by
Fize Jacques
Browse files
Options
Downloads
Patches
Plain Diff
Optimisation on ngram embedding
parent
d21f632d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/ngram_index.py
+31
-2
31 additions, 2 deletions
lib/ngram_index.py
with
31 additions
and
2 deletions
lib/ngram_index.py
+
31
−
2
View file @
5c0d03fa
...
...
@@ -73,7 +73,7 @@ class NgramIndex():
self
.
index_ngram
[
self
.
cpt
]
=
ngram
def
encode
(
self
,
word
):
def
encode
(
self
,
word
,
complete
=
True
):
"""
Return a ngram representation of a word
...
...
@@ -93,6 +93,8 @@ class NgramIndex():
ngrams
=
[
ng
for
ng
in
ngrams
if
ng
.
count
(
self
.
empty_char
)
<
2
]
if
not
self
.
loaded
:
[
self
.
add
(
ng
)
for
ng
in
ngrams
if
not
ng
in
self
.
ngram_index
]
if
not
complete
:
return
[
self
.
ngram_index
[
ng
]
for
ng
in
ngrams
if
ng
in
self
.
ngram_index
]
return
self
.
complete
([
self
.
ngram_index
[
ng
]
for
ng
in
ngrams
if
ng
in
self
.
ngram_index
],
self
.
max_len
)
def
complete
(
self
,
ngram_encoding
,
MAX_LEN
,
filling_item
=
0
):
...
...
@@ -136,7 +138,8 @@ class NgramIndex():
np.array
embedding matrix
"""
model
=
Word2Vec
([[
str
(
w
)
for
w
in
t
]
for
t
in
texts
],
size
=
dim
,
window
=
5
,
min_count
=
1
,
workers
=
4
,
**
kwargs
)
sentences
=
SentenceIterator
(
self
,
texts
,
True
)
model
=
Word2Vec
(
sentences
,
size
=
dim
,
window
=
5
,
min_count
=
1
,
workers
=
4
,
**
kwargs
)
N
=
len
(
self
.
ngram_index
)
embedding_matrix
=
np
.
zeros
((
N
,
dim
))
for
i
in
range
(
N
):
...
...
@@ -227,3 +230,29 @@ class NgramIndex():
new_obj
.
max_len
=
data
[
"
max_len_state
"
]
return
new_obj
class
SentenceIterator
:
"""
Iterator that counts upward forever.
"""
def
__init__
(
self
,
ng_encoder
,
input_
,
cast_str
=
False
):
self
.
ng_encoder
=
ng_encoder
self
.
input
=
input_
self
.
i
=
-
1
self
.
cast_str
=
cast_str
def
__iter__
(
self
):
self
.
i
=
-
1
return
self
def
__len__
(
self
):
return
len
(
self
.
input
)
def
__next__
(
self
):
try
:
self
.
i
+=
1
if
not
self
.
cast_str
:
return
self
.
ng_encoder
.
encode
(
self
.
input
[
self
.
i
])
else
:
return
list
(
map
(
str
,
self
.
ng_encoder
.
encode
(
self
.
input
[
self
.
i
])))
except
:
raise
StopIteration
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment