Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql.cursors
- from collections import namedtuple
- data_set = []
- #FB_subject, phrase, FB_object, relation
- data_set.append({"FB_subject_name":"Kevin Costner","phrase":"was awarded the","FB_object_name":"Honorary César",
- "relation":"award.award_winner.awards_won..award.award_honor.award"})
- data_set.append({"FB_subject_name":"Kevin Costner","phrase":"won the","FB_object_name":"Honorary César",
- "relation":"award.award_winner.awards_won..award.award_honor.award"})
- data_set.append({"FB_subject_name":"Leonardo di Caprio","phrase":"won the","FB_object_name":"Oscar",
- "relation":"award.award_winner.awards_won..award.award_honor.award"})
- def precision_recall_evaluation():
- #phraseInfo = namedtuple("phraseInfo", "phrase true_positives false_positives true_negatives false_negatives")
- true_positives = 0.0
- false_positives = 0.0
- true_negatives = 0.0
- false_negatives = 0.0
- #phrases = {}
- #Precision = true_positives/false_positives + true_positives
- #Recall = true_positives/ true_positives + false negatives
- connection = pymysql.connect(host= 'localhost',
- user = 'root',
- password = 'mysql',
- db = 'lector',
- charset ='utf8mb4',
- cursorclass = pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- sql = "SELECT FB_subject_name,phrase,FB_object_name,relation FROM extracted_facts WHERE FB_subject_name = %s AND FB_object_name = %s"
- for row in data_set:
- cursor.execute(sql,(row["FB_subject_name"],row["FB_object_name"]))
- result = cursor.fetchall()
- if result == (): #data set con recall max
- false_negatives += 1
- for element in result:
- if element["phrase"] == row["phrase"] and element["relation"] == row["relation"]:
- true_positives += 1
- elif element["relation"] != row["relation"]:
- false_positives += 1
- if element["phrase"] != row["phrase"] and element["relation"] == row["relation"]:
- false_negatives += 1
- elif element["relation"] != row["relation"]:
- true_negatives +=1
- precision = true_positives/(true_positives+false_positives)
- recall = true_positives/(true_positives+false_negatives)
- print "Precision = %f" %precision
- print "Recall = %f" %recall
- finally:
- connection.close()
- if __name__ == "__main__":
- precision_recall_evaluation()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement