Guest User

Untitled

a guest
Mar 31st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. import math
  2. import pymysql
  3. # Connect to the database
  4.  
  5. prob = 1
  6.  
  7. sentence = input("Please, enter a sentence : ")
  8. words = sentence.split()
  9.  
  10. length = len(words)
  11. print("--------------------------------")
  12. print("Length of the sentece : ",length)
  13. print("--------------------------------")
  14. print("Unigram : ",words)
  15. print("--------------------------------")
  16.  
  17. def retrieve_ngrams(txt, n):
  18. return [txt[i:i+n] for i in range(len(txt)-(n-1))]
  19.  
  20. teGrams = retrieve_ngrams(words, 2)
  21.  
  22. print("Bigram : ",teGrams)
  23. print("--------------------------------")
  24.  
  25. def hesapla(bigram):
  26. connection = pymysql.connect(host='localhost',
  27. user='root',
  28. password='ruveyda123',
  29. db='dbhw2',
  30. charset='utf8mb4',
  31. cursorclass=pymysql.cursors.DictCursor)
  32. try:
  33. ilk= "%"+bigram[0] + "%"
  34. ikinci = "%"+bigram[1] + "%"
  35. with connection.cursor() as cursor:
  36. sql1 = "Select count(*) from table_db where IG like %s";
  37.  
  38. cursor.execute(sql1, (ilk))
  39.  
  40. result = cursor.fetchone()
  41. sayi1 = result['count(*)']
  42.  
  43.  
  44. # print(result['count(*)'])
  45. #print("--------------------------------")
  46.  
  47. sql3 = "Select count(*) from table_db as R " \
  48. "INNER JOIN (select file_no, S_no, IX, IG from table_db where IG like %s) AS T " \
  49. "where R.file_no = T.file_no AND R.S_no = T.S_no AND R.IX = T.IX + 1 and R.IG like %s;"
  50.  
  51. cursor.execute(sql3,(ilk, ikinci))
  52.  
  53. result2 = cursor.fetchone()
  54. sayi2 = result2['count(*)']
  55.  
  56. # print(result2['count(*)'])
  57. #print("--------------------------------")
  58.  
  59. if(sayi1==0&sayi2!=0):
  60. sayi1=sayi1+(1/11909) # add-one(1/corpusSize) smoothing
  61. pr = sayi2 / sayi1
  62.  
  63. elif (sayi2==0&sayi1!=0):
  64. sayi2 = sayi2 + (1 / 11909)
  65. pr = sayi2 / sayi1
  66. elif (sayi2 != 0 & sayi2 != 0):
  67. pr = sayi2/sayi1
  68. else :
  69. pr = math.pow((1/11909), length)
  70.  
  71. return pr
  72.  
  73. finally:
  74. connection.close()
  75.  
  76.  
  77. for bigram in teGrams:
  78. prob = prob*hesapla(bigram)
  79. print(bigram,prob)
  80.  
  81.  
  82. print("Probability of the sentence is : ",prob)
  83. print("--------------------------------")
Add Comment
Please, Sign In to add comment