<filename>similarity/lsi_similarity.py
"""gensim docsim"""

from gensim.models import LsiModel
from gensim.corpora import Dictionary
from gensim.similarities import MatrixSimilarity

from urduhack.preprocessing import remove_punctuation
from urduhack.tokenization.words import fix_join_words
from urduhack import normalize

import pandas as pd

documents = []
df = pd.read_csv("/Users/muhammadfahid/PycharmProjects/Islam-360/ahsanulbayan.db.csv")
for index, row in df.iterrows():
    aya_no = row["Unnamed: 0"]
    if isinstance(row['translation'], str):
        translation = remove_punctuation(fix_join_words(normalize(row["translation"])))
        documents.append(translation.split())

model_path = "/Users/muhammadfahid/PycharmProjects/Islam-360/rel_model-0.9.w2v"
dictionary = Dictionary(documents)
corpus = [dictionary.doc2bow(document) for document in documents]
lsi = LsiModel(corpus=corpus, id2word=dictionary, num_topics=50)

q1 = "کیا سود حرام ہے"
q2 = "بنی اسرائیل کے لیے دعوت"
q3 = "منافقانہ کردار کیا ہے"

vec_bow = dictionary.doc2bow(q2.split())
vec_lsi = lsi[vec_bow]


similarity_indexes = MatrixSimilarity(lsi[corpus])

sims = similarity_indexes[vec_lsi]


sims = sorted(enumerate(sims), key=lambda item: -item[1])
for i, s in enumerate(sims):
    if i < 10:
        print(s, " ".join(documents[i]))



<filename>similarity/lsi_similarity.py
"""gensim docsim"""

from gensim.models import LsiModel
from gensim.corpora import Dictionary
from gensim.similarities import MatrixSimilarity

from urduhack.preprocessing import remove_punctuation
from urduhack.tokenization.words import fix_join_words
from urduhack import normalize

import pandas as pd

documents = []
df = pd.read_csv("/Users/muhammadfahid/PycharmProjects/Islam-360/ahsanulbayan.db.csv")
for index, row in df.iterrows():
    aya_no = row["Unnamed: 0"]
    if isinstance(row['translation'], str):
        translation = remove_punctuation(fix_join_words(normalize(row["translation"])))
        documents.append(translation.split())

model_path = "/Users/muhammadfahid/PycharmProjects/Islam-360/rel_model-0.9.w2v"
dictionary = Dictionary(documents)
corpus = [dictionary.doc2bow(document) for document in documents]
lsi = LsiModel(corpus=corpus, id2word=dictionary, num_topics=50)

q1 = "کیا سود حرام ہے"
q2 = "بنی اسرائیل کے لیے دعوت"
q3 = "منافقانہ کردار کیا ہے"

vec_bow = dictionary.doc2bow(q2.split())
vec_lsi = lsi[vec_bow]


similarity_indexes = MatrixSimilarity(lsi[corpus])

sims = similarity_indexes[vec_lsi]


sims = sorted(enumerate(sims), key=lambda item: -item[1])
for i, s in enumerate(sims):
    if i < 10:
        print(s, " ".join(documents[i]))



gensim docsim