SHARE
TWEET

Untitled

a guest Dec 11th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. oceniPoKorisnici = {
  2.     'Lisa Rose': {'Catch Me If You Can': 3.0, 'Snakes on a Plane': 3.5, 'Superman Returns': 3.5,
  3.                   'You, Me and Dupree': 2.5, 'The Night Listener': 3.0, 'Snitch': 3.0},
  4.     'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, 'Just My Luck': 1.5, 'The Night Listener': 3.0,
  5.                      'You, Me and Dupree': 3.5},
  6.     'Michael Phillips': {'Catch Me If You Can': 2.5, 'Lady in the Water': 2.5, 'Superman Returns': 3.5,
  7.                          'The Night Listener': 4.0, 'Snitch': 2.0},
  8.     'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'The Night Listener': 4.5, 'Superman Returns': 4.0,
  9.                      'You, Me and Dupree': 2.5},
  10.     'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'Just My Luck': 2.0, 'Superman Returns': 3.0,
  11.                      'You, Me and Dupree': 2.0},
  12.     'Jack Matthews': {'Catch Me If You Can': 4.5, 'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
  13.                       'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5, 'Snitch': 4.5},
  14.     'Toby': {'Snakes on a Plane': 4.5, 'Snitch': 5.0},
  15.     'Michelle Nichols': {'Just My Luck': 1.0, 'The Night Listener': 4.5, 'You, Me and Dupree': 3.5,
  16.                          'Catch Me If You Can': 2.5, 'Snakes on a Plane': 3.0},
  17.     'Gary Coleman': {'Lady in the Water': 1.0, 'Catch Me If You Can': 1.5, 'Superman Returns': 1.5,
  18.                      'You, Me and Dupree': 2.0},
  19.     'Larry': {'Lady in the Water': 3.0, 'Just My Luck': 3.5, 'Snitch': 1.5, 'The Night Listener': 3.5}
  20. }
  21.  
  22.  
  23. from math import sqrt
  24. def sim_distance(oceni,person1,person2):
  25.     #lista na zaednicki predmeti
  26.     si={}
  27.     for item in oceni[person1]:
  28.         if item in oceni[person2]:
  29.             si[item]=1
  30.  
  31.     #ako nemaat zaednicki predmeti
  32.     if(len(si)==0): return 0
  33.     sum_of_squares = sum([pow(oceni[person1][item] - oceni[person2][item], 2)
  34.         for item in oceni[person1] if item in oceni[person2]])
  35.     return round(1 / (1 + sqrt(sum_of_squares)),3)
  36.  
  37. def sim_pearson(oceni,p1,p2):
  38.     # Se kreira recnik vo koj ke se cuvaat predmetite (filmovi) koi se oceneti od dvajcata
  39.     # Vo recnikot ni se vazni samo klucevite za da gi cuvame iminjata na filmovite koi se zaednicki, a vrednostite ne ni se vazni
  40.     zaednicki = {}
  41.     for item in oceni[p1]:
  42.         if item in oceni[p2]: zaednicki[item] = 1
  43.  
  44.     # Se presmetuva brojot na predmeti oceneti od dvajcata
  45.     n = len(zaednicki)
  46.  
  47.     # Ako nemaat zaednicki predmeti vrati korelacija 0
  48.     if n == 0: return 0
  49.  
  50.     # Soberi gi zaednickite oceni (rejtinzi) za  sekoja licnost posebno
  51.     sum1 = sum([oceni[p1][it] for it in zaednicki])
  52.     sum2 = sum([oceni[p2][it] for it in zaednicki])
  53.  
  54.     # Soberi gi kvadratite od zaednickite oceni (rejtinzi) za  sekoja licnost posebno
  55.     sum1Sq = sum([pow(oceni[p1][it], 2) for it in zaednicki])
  56.     sum2Sq = sum([pow(oceni[p2][it], 2) for it in zaednicki])
  57.  
  58.     # Soberi gi proizvodite od ocenite na dvete licnosti
  59.     pSum = sum([oceni[p1][it] * oceni[p2][it] for it in zaednicki])
  60.  
  61.     # Presmetaj go koeficientot na korelacija
  62.     num = pSum - (sum1 * sum2 / n)
  63.     den = sqrt((sum1Sq - pow(sum1, 2) / n) * (sum2Sq - pow(sum2, 2) / n))
  64.     if den == 0: return 0
  65.     r = num / den
  66.     return round(r,3)
  67. def zaednickiOcenki(oceni,person1,person2):
  68.     broj=0
  69.     for item in oceni[person1]:
  70.         if item in oceni[person2]:
  71.             broj+=1
  72.     return broj
  73.  
  74.  
  75. def TabelaNaSlicniKorisnici(oceni):
  76.     slicnosti = {}
  77.     for person1 in oceni:
  78.         for person2 in oceni:
  79.             slicnosti.setdefault(person1,{})
  80.            
  81.             slicnosti[person1][person2]=(sim_distance(oceni,person1,person2),sim_pearson(oceni,person1,person2),zaednickiOcenki(oceni,person1,person2))
  82.  
  83.  
  84.     return slicnosti
  85.  
  86.  
  87. if __name__ == "__main__":
  88.     korisnik1 = input()
  89.     korisnik2 = input()
  90.     # korisnik1='Mick LaSalle'
  91.     # korisnik2='Lisa Rose'
  92.     # print oceniPoKorisnici
  93.     tabela = TabelaNaSlicniKorisnici(oceniPoKorisnici)
  94.     # print tabela
  95.     print (tabela[korisnik1][korisnik2])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top