Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import pymysql
- # Connect to the database
- prob = 1
- sentence = input("Please, enter a sentence : ")
- words = sentence.split()
- length = len(words)
- print("--------------------------------")
- print("Length of the sentece : ",length)
- print("--------------------------------")
- print("Unigram : ",words)
- print("--------------------------------")
- def retrieve_ngrams(txt, n):
- return [txt[i:i+n] for i in range(len(txt)-(n-1))]
- teGrams = retrieve_ngrams(words, 2)
- print("Bigram : ",teGrams)
- print("--------------------------------")
- def hesapla(bigram):
- connection = pymysql.connect(host='localhost',
- user='root',
- password='ruveyda123',
- db='dbhw2',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- try:
- ilk= "%"+bigram[0] + "%"
- ikinci = "%"+bigram[1] + "%"
- with connection.cursor() as cursor:
- sql1 = "Select count(*) from table_db where IG like %s";
- cursor.execute(sql1, (ilk))
- result = cursor.fetchone()
- sayi1 = result['count(*)']
- # print(result['count(*)'])
- #print("--------------------------------")
- sql3 = "Select count(*) from table_db as R " \
- "INNER JOIN (select file_no, S_no, IX, IG from table_db where IG like %s) AS T " \
- "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;"
- cursor.execute(sql3,(ilk, ikinci))
- result2 = cursor.fetchone()
- sayi2 = result2['count(*)']
- # print(result2['count(*)'])
- #print("--------------------------------")
- if(sayi1==0&sayi2!=0):
- sayi1=sayi1+(1/11909) # add-one(1/corpusSize) smoothing
- pr = sayi2 / sayi1
- elif (sayi2==0&sayi1!=0):
- sayi2 = sayi2 + (1 / 11909)
- pr = sayi2 / sayi1
- elif (sayi2 != 0 & sayi2 != 0):
- pr = sayi2/sayi1
- else :
- pr = math.pow((1/11909), length)
- return pr
- finally:
- connection.close()
- for bigram in teGrams:
- prob = prob*hesapla(bigram)
- print(bigram,prob)
- print("Probability of the sentence is : ",prob)
- print("--------------------------------")
Add Comment
Please, Sign In to add comment