Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import numpy as np
- import math
- input_file=open("cats.txt",'r')
- senten=list()
- dictionary=dict()
- i=0
- def blua(x):
- global i
- if x not in dictionary:
- dictionary[x]=i
- i+=1
- return x
- for line in input_file:
- lst=list(filter(None,re.split('[^a-z]',line.lower())))
- senten.append(lst)
- map(blua,lst);
- matrix=np.zeros((len(senten),i));
- y=0;
- for line in senten:
- for word in line:
- matrix[y,dictionary[word]]+=1
- y+=1
- first=[-2,0];
- second=[-2,0];
- for number in range(1,y-1):
- cos_angle = np.dot(matrix[0], matrix[number]) / np.linalg.norm(matrix[0]) / np.linalg.norm(matrix[number])
- print(cos_angle)
- if cos_angle>first[0]:
- second[0]=first[0]
- second[1]=first[1]
- first[0]=cos_angle
- first[1]=number
- elif cos_angle>second[0]:
- second[0]=cos_angle
- second[1]=number
- print(first[1])
- print(second[1])
- input_file.close
Advertisement
Add Comment
Please, Sign In to add comment