Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
strategy_embedding
Project
Project
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
rank
strategy_embedding
Commits
27eeac2a
Commit
27eeac2a
authored
Nov 25, 2020
by
赵威
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
write data to diary
parent
6cd1c997
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
26 deletions
+25
-26
diary_similarity.py
doc_similarity/diary_similarity.py
+25
-26
No files found.
doc_similarity/diary_similarity.py
View file @
27eeac2a
...
@@ -10,7 +10,7 @@ import numpy as np
...
@@ -10,7 +10,7 @@ import numpy as np
from
bert_serving.client
import
BertClient
from
bert_serving.client
import
BertClient
from
utils.cache
import
redis_client_db
from
utils.cache
import
redis_client_db
from
utils.es
import
get_diary_info_from_es
from
utils.es
import
get_diary_info_from_es
from
utils.files
import
MODEL_PATH
,
DATA
_PATH
from
utils.files
import
DATA_PATH
,
MODEL
_PATH
def
save_result
():
def
save_result
():
...
@@ -18,31 +18,30 @@ def save_result():
...
@@ -18,31 +18,30 @@ def save_result():
index_path
=
os
.
path
.
join
(
MODEL_PATH
,
"faiss_diary_similarity.index"
)
index_path
=
os
.
path
.
join
(
MODEL_PATH
,
"faiss_diary_similarity.index"
)
faiss_index
=
faiss
.
read_index
(
index_path
)
faiss_index
=
faiss
.
read_index
(
index_path
)
print
(
faiss_index
)
count
=
0
with
open
(
os
.
path
.
join
(
DATA_PATH
,
"diary_similarity.log"
),
"w"
)
as
f
:
for
item
in
get_diary_info_from_es
([
"id"
,
"answer"
,
"content_level"
]):
count
=
0
count
+=
1
for
item
in
get_diary_info_from_es
([
"id"
,
"answer"
,
"content_level"
]):
id
=
int
(
item
[
"_id"
])
count
+=
1
content
=
item
[
"_source"
][
"answer"
]
id
=
int
(
item
[
"_id"
])
content_level
=
str
(
item
[
"_source"
][
"content_level"
])
content
=
item
[
"_source"
][
"answer"
]
try
:
content_level
=
str
(
item
[
"_source"
][
"content_level"
])
emb
=
np
.
array
([
bc
.
encode
([
content
])
.
tolist
()[
0
]])
.
astype
(
"float32"
)
try
:
D
,
I
=
faiss_index
.
search
(
emb
,
10
)
emb
=
np
.
array
([
bc
.
encode
([
content
])
.
tolist
()[
0
]])
.
astype
(
"float32"
)
distances
=
D
.
tolist
()[
0
]
D
,
I
=
faiss_index
.
search
(
emb
,
10
)
ids
=
I
.
tolist
()[
0
]
distances
=
D
.
tolist
()[
0
]
res
=
[]
ids
=
I
.
tolist
()[
0
]
for
(
index
,
i
)
in
enumerate
(
distances
):
res
=
[]
tmp_id
=
ids
[
index
]
for
(
index
,
i
)
in
enumerate
(
distances
):
if
i
<=
1.0
and
tmp_id
!=
id
:
tmp_id
=
ids
[
index
]
res
.
append
(
str
(
tmp_id
))
if
i
<=
1.0
and
tmp_id
!=
id
:
if
res
:
res
.
append
(
str
(
tmp_id
))
data
=
"{}:{}:{}"
.
format
(
content_level
,
str
(
id
),
","
.
join
(
res
))
if
res
:
print
(
data
)
data
=
"{}:{}:{}"
.
format
(
content_level
,
str
(
id
),
","
.
join
(
res
))
except
Exception
as
e
:
print
(
data
)
print
(
e
)
f
.
write
(
data
)
print
(
"done"
)
except
Exception
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
# bc = BertClient("172.16.44.82", check_length=False)
# bc = BertClient("172.16.44.82", check_length=False)
...
...
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