Reputation: 21
from collections import OrderedDict import json
def read_classification_from_file(dict_file1,dict_file2): with open(dict_file1,'r') as f: dict1 = json.load(f, object_pairs_hook=OrderedDict) with open(dict_file2,'r') as f: data2 = json.load(f, object_pairs_hook=OrderedDict)
# Creates list of lists pairing each value in
# dict1 with each value in dict2
return [[value1,value2]
for value1 in dict1.values()
for value2 in dict2.values()]
Upvotes: 0
Views: 127
Reputation: 91
Use json if your data is being created by or shared with other sources (use only double quotes!). May want to use OrderedDict to ensure proper order, or better yet, re-organize your data to be a simple list of values, since the keys don't appear to be all that important:
from collections import OrderedDict
import json
def read_classification_from_file(dict_file1,dict_file2):
with open(dict_file1,'r') as f:
dict1 = json.load(f, object_pairs_hook=OrderedDict)
with open(dict_file2,'r') as f:
data2 = json.load(f, object_pairs_hook=OrderedDict)
# Creates list of lists pairing each value in
# dict1 with each value in dict2
return [[value1,value2]
for value1 in dict1.values()
for value2 in dict2.values()]
Upvotes: 0
Reputation: 96277
It's not entirely clear what you want, and if I have understood you correctly then the ideal way to accomplish what you are trying to do but would be to take a different approach, but you want to stick with this approach, you can use the eval() function. You read in the the strings from your .txt files, but to use that string you need to pass it as an argument to the eval() function for it to be parsed and evaluated as a Python expression:
with open('example1.txt') as f:
call = f.readline()
truth_dict = eval(call)
So this might work:
from collections import namedtuple
def compute_confusion_matrix(file1, file2, pos_tag=True, neg_tag=False):
TP=0
FP=0
TN=0
FN=0
with open(file1) as f:
call = f.readline()
truth_dict = eval(call)
with open(file2) as f:
call - f.readline()
pred_dict = eval(call)
for key,value in truth_dict.items():
if truth_dict[key]==pred_dict[key] == neg_tag:
TP +=1
if pred_dict[key]==truth_dict[key] == pos_tag:
FP +=1
if truth_dict[key]==pred_dict[key] != neg_tag :
TN +=1
if truth_dict[key]==pred_dict[key] != pos_tag:
FN +=1
ConfMat = namedtuple('ConfMat', 'tp tn fp fn')
p=ConfMat(TP, FP, TN, FN)
return p
Upvotes: 0