mirror of
https://github.com/RustyRaptor/learnopencv.git
synced 2026-12-03 22:15:16 -07:00
Quora Dataset Duplicate Search
This commit is contained in:
1 parent
673e596fe0
commit
bb0772c328
7 files changed
+406685
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
@@ -0,0 +1,91 @@
|
||||
import tensorflow as tf
|
||||
import tensorflow_hub as hub
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import seaborn as sns
|
||||
|
||||
csv_fname = "q_quora.csv"
|
||||
|
||||
question1 = {}
|
||||
question2 = {}
|
||||
|
||||
print("Loading data from {}".format(csv_fname))
|
||||
|
||||
numLines = int(input("Enter number of lines to read: "))
|
||||
|
||||
with open(csv_fname,'r') as f:
|
||||
if numLines == -1:
|
||||
totalLines = f.readlines()[1:]
|
||||
else:
|
||||
totalLines = f.readlines()[1:numLines]
|
||||
for line in totalLines:
|
||||
try:
|
||||
qid1, qid2, q1, q2 = line.strip().split(',')[1:5]
|
||||
question1[qid1] = q1
|
||||
question2[qid2] = q2
|
||||
except:
|
||||
continue
|
||||
|
||||
print("Data loaded successfully")
|
||||
|
||||
module_url = "https://tfhub.dev/google/universal-sentence-encoder/2"
|
||||
|
||||
print("Loading model from {}".format(module_url))
|
||||
embed = hub.Module(module_url)
|
||||
print("Model loaded successfully")
|
||||
|
||||
def plot_similarity(labels1, labels2, features1, features2, rotation):
|
||||
corr = np.inner(features1, features2)
|
||||
corr2 = corr.copy()
|
||||
corr2[corr2<0.8]=0
|
||||
corr2[corr2>=0.8]=1
|
||||
#print(corr)
|
||||
sns.set(font_scale=0.6)
|
||||
#plt.figure(figsize=(100,100))
|
||||
g = sns.heatmap(corr,\
|
||||
#xticklabels=labels1,\
|
||||
#yticklabels=labels2,\
|
||||
vmin=0,\
|
||||
vmax=1,\
|
||||
#cmap="Greys")
|
||||
cmap="YlOrRd")
|
||||
#g.set_xticklabels(labels1, rotation=rotation)
|
||||
g.set_title("Semantic Textual Similarity")
|
||||
plt.tight_layout()
|
||||
plt.savefig("Quora.png")
|
||||
plt.show()
|
||||
#plt.figure(figsize=(100,100))
|
||||
#g.set_xticklabels(labels1, rotation=rotation)
|
||||
similar_qid = {}
|
||||
for i in range(len(labels1)):
|
||||
for j in range(len(labels2)):
|
||||
if corr2[i][j] == 1:
|
||||
similar_qid[labels1[i]]=labels2[j]
|
||||
return similar_qid
|
||||
|
||||
def run_and_plot(session_, input_tensor_, messages1_, messages2_, labels1_,labels2_, encoding_tensor):
|
||||
print("Embeddings questions 1")
|
||||
message_embeddings1_ = session_.run(encoding_tensor, feed_dict={input_tensor_: messages1_})
|
||||
print("Embeddings questions 2")
|
||||
message_embeddings2_ = session_.run(encoding_tensor, feed_dict={input_tensor_: messages2_})
|
||||
similar_qid = plot_similarity(labels1_,labels2_, \
|
||||
message_embeddings1_,\
|
||||
message_embeddings2_, 90)
|
||||
return similar_qid
|
||||
|
||||
similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
|
||||
similarity_message_encodings = embed(similarity_input_placeholder)
|
||||
with tf.Session() as session:
|
||||
session.run(tf.global_variables_initializer())
|
||||
session.run(tf.tables_initializer())
|
||||
similar_qid = run_and_plot(session, similarity_input_placeholder,\
|
||||
list(question1.values()),\
|
||||
list(question2.values()),\
|
||||
list(question1.keys()),\
|
||||
list(question2.keys()),\
|
||||
similarity_message_encodings)
|
||||
|
||||
with open("similarity-results.txt",'w') as f:
|
||||
for i in list(similar_qid.keys()):
|
||||
f.write("{},{}\n=======================\n".format(question1[i], question2[similar_qid[i]]))
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 468 KiB |
@@ -0,0 +1,16 @@
|
||||
# Quora Dataset Duplicate Search
|
||||
First make sure that the following python modules are installed:
|
||||
|
||||
```
|
||||
pip3 install --quiet "tensorflow>=1.7"
|
||||
pip3 install --quiet tensorflow-hub
|
||||
pip3 install --quiet seaborn
|
||||
```
|
||||
|
||||
Next, to run the **Semantic Similarity Analysis** using **Universal Sentence Encoder** use the following:
|
||||
|
||||
`python Quora-Duplicate-Search.py`
|
||||
|
||||
Enter the number of lines to read from the CSV file for analysis (for example, 1000).
|
||||
|
||||
The duplicate sentences found will be written in the file `similarity-results.txt`.
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -5,6 +5,7 @@ Learn OpenCV : C++ and Python Examples. You can find the details at [LearnOpenC
|
||||
|
||||
| Blog Post | |
|
||||
| ------------- |:-------------|
|
||||
|[Duplicate Search on Quora Dataset](https://www.learnopencv.com/duplicate-search-on-quora-dataset/) | [Code](https://github.com/spmallick/learnopencv/tree/master/Quora-Dataset-Duplicate-Search) |
|
||||
|[Shape Matching using Hu Moments (C++/Python)](https://www.learnopencv.com/shape-matching-using-hu-moments-c-python/) | [Code](https://github.com/spmallick/learnopencv/tree/master/HuMoments) |
|
||||
|[Install OpenCV 4 on CentOS (C++ and Python)](https://www.learnopencv.com/install-opencv-4-on-centos-7/) | [Code](https://github.com/spmallick/learnopencv/blob/master/InstallScripts/installOpenCV-3-on-centos.sh) |
|
||||
|[Install OpenCV 3.4.4 on CentOS (C++ and Python)](https://www.learnopencv.com/install-opencv-3-4-4-on-centos-7/) | [Code](https://github.com/spmallick/learnopencv/blob/master/InstallScripts/installOpenCV-3-on-centos.sh) |
|
||||
|
||||
Reference in new issue
Block a user