ivolff

Cats

Aug 15th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. import re
  2. import numpy as np
  3. import math
  4. input_file=open("cats.txt",'r')
  5. senten=list()
  6. dictionary=dict()
  7. i=0
  8.  
  9. def blua(x):
  10.     global i
  11.     if x not in dictionary:
  12.         dictionary[x]=i
  13.         i+=1
  14.     return x
  15. for line in input_file:
  16.     lst=list(filter(None,re.split('[^a-z]',line.lower())))
  17.     senten.append(lst)
  18.     map(blua,lst);
  19. matrix=np.zeros((len(senten),i));
  20. y=0;
  21. for line in senten:
  22.     for word in line:
  23.         matrix[y,dictionary[word]]+=1
  24.     y+=1
  25. first=[-2,0];
  26. second=[-2,0];
  27. for number in range(1,y-1):
  28.     cos_angle = np.dot(matrix[0], matrix[number]) / np.linalg.norm(matrix[0]) / np.linalg.norm(matrix[number])
  29.     print(cos_angle)
  30.     if cos_angle>first[0]:
  31.         second[0]=first[0]
  32.         second[1]=first[1]
  33.         first[0]=cos_angle
  34.         first[1]=number
  35.     elif cos_angle>second[0]:
  36.         second[0]=cos_angle
  37.         second[1]=number
  38. print(first[1])
  39. print(second[1])
  40. input_file.close
Advertisement
Add Comment
Please, Sign In to add comment