Mephistopheles_

Comparison of sentences

Sep 3rd, 2021 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import pandas as pd
  4. import re
  5. from scipy.spatial.distance import cosine as cos
  6. import collections
  7. l=[]
  8. with open(r'Myfile.txt') as fl:
  9.     l=fl.readlines()
  10.  
  11. data=pd.DataFrame()
  12. for i in l:
  13.     i=i.lower()
  14.     sen=re.split('[^a-z]',i)
  15.     ma= {}
  16.     for j in sen:
  17.         if len(j)>=2:
  18.             if not ma.get(j):
  19.                 ma[j]=1
  20.             else:
  21.                 ma[j]+=1
  22.     data=data.append(ma, ignore_index=True)
  23. data.fillna(0,inplace=True)
  24. data=data.apply(np.intc)
  25. res= [-1,-1]
  26. x=[2,2]
  27. for i in range(1,data.shape[0]):
  28.     q=cos(data.iloc[0,:],data.iloc[i,:])
  29.     if(q<=x[0]):
  30.         x[1]=x[0]
  31.         x[0]=q
  32.         res[1]=res[0]
  33.         res[0]=i
  34.     elif q<x[1]:
  35.         x[1]=q
  36.         res[1]=i
  37. print(res,l[0],l[6],l[4])
Add Comment
Please, Sign In to add comment