##############################################################################
#
# Copyright (c) 2006-2007 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import csv, os.path, unicodedata, string
from cStringIO import StringIO

allowed_characters = string.ascii_letters + string.digits + ' .,?!'
def normalize(data):
    return ''.join(x for x in unicodedata.normalize( \
                   'NFKD', str(data).decode('utf-8')) if x in \
                   allowed_characters).lower()

def setRFODTRecommendationDict(self, recommendation_file):
  memcached_key_prefix = 'correction-recommendation-rf-odt'
  
  portal_object = self.getPortalObject()
  recommendation_dict = portal_object.portal_memcached.getMemcachedDict(
                    key_prefix=memcached_key_prefix,
                    plugin_path='portal_memcached/persistent_memcached_plugin')

  for row in csv.reader(recommendation_file, delimiter=';'):
    answer_key = str(hash(row[0] + row[1]))
    recommendation_list = row[2:]
    scored_correction_list = []

    for recommendation in recommendation_list:
      correction, score = recommendation.split(':True(')
      score = float(score[:-1])
      scored_correction_list.append((correction, score))
 
    recommendation_dict[answer_key] = scored_correction_list

def exportRFODTTrainFile(self,
                    valid_validation_state_tuple = ('archived', 'released'),
                    valid_causality_state_tuple = ('validated'),
                    language_tuple = ('en',), question_id_list = []):
  answer_set_module = self.getPortalObject().answer_set_module
  table = answer_set_module.AnswerSetModule_constructAnswerCorrectionTableToExport(
                        valid_validation_state_tuple = valid_validation_state_tuple,
                        valid_causality_state_tuple = valid_causality_state_tuple,
                        language_tuple = language_tuple,
                        question_id_list = question_id_list )
  train_strio = StringIO()
  train_writer = csv.writer(train_strio, delimiter=';', quoting=csv.QUOTE_NONE)
  train_writer.writerow(table['header_row'][1:])
  for row in table['row_list']:
    row = map(normalize, [row['question'], row['answer']] + row['correction_occurence_list'])
    try:
      train_writer.writerow(row)
    except csv.Error:
      return row
  return train_strio.getvalue()

def exportRapidTrainFile(self,
                    valid_validation_state_tuple = ('archived', 'released'),
                    valid_causality_state_tuple = ('validated'),
                    language_tuple = ('en',), question_id_listi = []):
  answer_set_module = self.getPortalObject().answer_set_module
  table = answer_set_module.AnswerSetModule_constructAnswerCorrectionTableToExport(
                        valid_validation_state_tuple = valid_validation_state_tuple,
                        valid_causality_state_tuple = valid_causality_state_tuple,
                        language_tuple = language_tuple,
                        question_id_list = question_id_list )
  train_strio = StringIO()
  train_writer = csv.writer(train_strio, delimiter=';', quoting=csv.QUOTE_NONE)
  train_writer.writerow(table['header_row'][1:])
  for row in table['row_list']:
    row = map(normalize, [row['question_id'], row['answer']] + row['correction_occurence_list'])
    train_writer.writerow(row)
  return train_strio.getvalue()

def exportRFODTTestFile(self,
                    valid_validation_state_tuple = ('submitted',),
                    valid_causality_state_tuple = ('draft', 'validated','invalidated'),
                    language_tuple = ('en',), question_id_list = []):
  answer_set_module = self.getPortalObject().answer_set_module
  table = answer_set_module.AnswerSetModule_constructAnswerCorrectionTableToExport(
                        valid_validation_state_tuple = valid_validation_state_tuple,
                        valid_causality_state_tuple = valid_causality_state_tuple,
                        language_tuple = language_tuple,
                        question_id_list = question_id_list )
  test_strio = StringIO()
  test_writer = csv.writer(test_strio, delimiter=';', quoting=csv.QUOTE_NONE)
  for row in table['row_list']:
    test_writer.writerow( map(normalize, [row['question'], row['answer']]) )
  return test_strio.getvalue()

def exportRapidTestFile(self,
                    valid_validation_state_tuple = ('submitted',),
                    valid_causality_state_tuple = ('draft', 'validated','invalidated'),
                    language_tuple = ('en',), question_id_list = []):
  answer_set_module = self.getPortalObject().answer_set_module
  table = answer_set_module.AnswerSetModule_constructAnswerCorrectionTableToExport(
                        valid_validation_state_tuple = valid_validation_state_tuple,
                        valid_causality_state_tuple = valid_causality_state_tuple,
                        language_tuple = language_tuple,
                        question_id_list = question_id_list )
  test_strio = StringIO()
  test_writer = csv.writer(test_strio, delimiter=';', quoting=csv.QUOTE_NONE)
  for row in table['row_list']:
    test_writer.writerow( map(normalize, [row['answer_id'], row['question_id'], row['answer']]) )
  return test_strio.getvalue()
