from Evaluation import GeoJsonReader
import csv


if __name__ == '__main__':
    csv_output = open('narges_original_text.csv', 'wt');
    csv_writer = csv.writer(csv_output)

    count = 0
    for i in range(1, 11):
        result_file_name = './narges result/1920-' + str(i) + '.png_EdditedByPixels.txt'
        result_obj = GeoJsonReader.load_json_file(result_file_name)


        for feature in result_obj['features']:
            text = feature['NameBeforeDictionary']
            text.replace(',', '')
            text.replace('\n', ' ')
            csv_writer.writerow([text])
            count += 1
    proceed_sets = set()

    score_writer = csv.DictWriter(scorefile, fieldnames=fieldnames2)
    score_writer.writeheader()

    rect_writer = csv.DictWriter(rect_precision_file, fieldnames=fieldnames)
    rect_writer.writeheader()

    # Read Ground Truth.
    map_gt_list = [];
    for m in range(1, 11):
        map_name = ['1920-1.png', '1920-2.png', '1920-3.png', '1920-4.png', '1920-5.png',
                    '1920-6.png', '1920-7.png', '1920-8.png', '1920-9.png', '1920-10.png']

        groundTruthFile = './GroundTruths/' + map_name[m - 1].split('.')[0] + '.geojson'
        gt_obj = GeoJsonReader.load_json_file(groundTruthFile)
        gt_list = GeoJsonReader.get_ground_truth_list(gt_obj, 1, 1)
        map_gt_list.append(gt_list)

    candidate_file = './narges result/candidates'
    cf = open(candidate_file, 'r')

    map_feature_list = []
    map_result_obj_features = []
    map_result_feature_count = []
    map_result_rects = []

    # Read result files.
    count = 0
    for i in range(1, MAP_NUM):
        result_file_name = './narges result/1920-' + str(i) + '.png_EdditedByPixels.txt'
def evaluate_result(gt_obj, result_obj, group_para=0, scale_w=1, scale_h=1):
    gt_list = GeoJsonReader.get_ground_truth_list(gt_obj, scale_w, scale_h)
    result_rect_list = GeoJsonReader.get_result_rectangles(result_obj)
    return ResultEvaluation.evaluation_simple(result_rect_list, gt_list, area_threshold=0.7, group_para=group_para)
    gt_list = []
    map_name = ['1920-1.png', '1920-2.png', '1920-3.png', '1920-4.png', '1920-5.png',
                '1920-6.png', '1920-7.png', '1920-8.png', '1920-9.png', '1920-10.png']


    found_gt_num = 0
    correct_num = 0
    discard_num = 0
    strabo_num = 0

    for m in range(1, 11):
        groundTruthFile = './GroundTruths/' + map_name[m-1].split('.')[0] + '.geojson'
        result_file = './rsh_rs/' + map_name[m-1] + 'ByPixels.txt'

        result_obj = GeoJsonReader.load_json_file(result_file)
        result_rect_list = GeoJsonReader.get_result_rectangles(result_obj)

        gt_obj = GeoJsonReader.load_json_file(groundTruthFile)
        gt_list = GeoJsonReader.get_ground_truth_list(gt_obj, 1, 1)

        word_locations = map_words_locations[m]
        for idw, rect in enumerate(word_locations):
            word_list = map_aligned_list[m][idw]
            discard_list = map_discard_list[m][idw]

            result_text = ''
            rect_idx = 0
            for result_rect in result_rect_list:
                if result_rect.is_the_same_rect(rect):
                    result_text = result_rect.text
        resolutions[2] = float(500) / 1512
        resolutions[3] = float(2000) / 1512
        resolutions[4] = float(2500) / 1512

        precisions = [ [[] for x in resolution_name] for y in range(1, 4) ]
        recalls = [ [[] for x in resolution_name] for y in range(1, 4) ]

        for r in range(1, 5):
            for i in range(0, len(file_list)):
                for u in range(1, 4):
                    groundTruthFile = './GroundTruths/' + file_list[i] + '.geojson'
                    resultFile = './Results/res' + str(r) + '/' + file_list[i] + '.pngByPixels.txt'
                    print("Ground Truth: %s\nResult:       %s" % (groundTruthFile, resultFile))
                    print("Scaling: %f" % (resolutions[r]))

                    result_obj = GeoJsonReader.load_json_file(resultFile)
                    gt_obj = GeoJsonReader.load_json_file(groundTruthFile)

                    overlap_json, non_overlap_json, precision_list, recall_list, extracted_num_list, gt_num_list = \
                    evaluate_result(gt_obj, result_obj, group_para=u, scale_w=resolutions[r], scale_h=resolutions[r])

                    gt_num = gt_num_list[0]
                    extracted_num = extracted_num_list[0]

                    precision = precision_list[0]
                    recall = recall_list[0]

                    precisions[u - 1][r - 1].append(precision)
                    recalls[u - 1][r - 1].append(recall)

                    f_score = 0 if precision + recall == 0 else 2 * precision * recall / (precision + recall)