Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
freq_exporter.py
Go to the documentation of this file.
1 ## @package parsers.freq_exporter
2 
3 import logging
4 import csv
5 from utils.ui_utils import UIUtils
6 
7 ## This class provides the ability to export data for the WH-Frequency App.
8 # Note: 'Count Columns' are columns that the user has added to the main window. These columns count user-specified (regex) patterns.
9 class FreqExporter(object):
10  ## Constructor
11  # @param self
12  # @param filename (string) full path to the CSV spreadsheet file to write to
13  # @param trs_filename (string) full path to the trs file being used by the WH-Frequency App
14  def __init__(self, filename, trs_filename):
15  #log errors to log/
16  self.logger = logging.getLogger(__name__)
17 
18  #make sure file suffix is present
19  if not filename.lower().endswith('.csv'):
20  filename += '.csv'
21 
22  self.filename = filename
23  self.trs_filename = trs_filename
24 
25  #create a basic csv-writing object using the python csv library
26  self.csv_file = open(self.filename, 'wb')
27  self.csv_writer = csv.writer(self.csv_file, quoting=csv.QUOTE_ALL)
28 
29  ## Writes the column headings to the spreadsheet file.
30  # @param self
31  # @param count_col_headers (list) list of strings, each representing the headers for any 'count columns' the user has added
32  def write_header_row(self, count_col_headers):
33  self.csv_writer.writerow(['Date: %s' % UIUtils.get_cur_timestamp_str()])
34  self.csv_writer.writerow(['File: %s' % self.trs_filename])
35  self.csv_writer.writerow([''])
36  headers = ['Time', 'Phrase', 'Speakers', 'Target Listeners', 'WHQ Count']
37  headers.extend(count_col_headers)
38  self.csv_writer.writerow(headers)
39 
40  ## Writes a row of data to the spreadsheet file.
41  # This includes the values for the user-specified 'count columns'.
42  # @param self
43  # @param time_str (string) a string containing the start and end times of the utterance this row corresponds to (eg. '00:01:00 - 00:02:00')
44  # @param phrase (string) the transcription phrase for the utterance this row corresponds to
45  # @param speakers_str (string) a string containing a description of the speakers for the utterance this row corresponds to
46  # @param targets_str (string) a string containing a description of the target listenders for the utterance this row corresponds to
47  # @param num_whq (int) an integer representing the number of WH Questions conatined in the utterance this row corresponds to
48  # @param count_col_vals (list) list of the values for the user-defined 'count columns'
49  def write_count_row(self, time_str, phrase, speakers_str, targets_str, num_whq, count_col_vals):
50  row = [time_str, phrase, speakers_str, targets_str, num_whq]
51  row.extend(count_col_vals)
52  self.csv_writer.writerow(row)
53 
54  ## Writes a final 'Totals' row to the spreadsheet file.
55  # This row contains the sum of the 'WH count" column, in addition to the sums of the user-defined 'count columns'.
56  # @param self
57  # @param total_whq (int) the sum of the 'WH Counts' across all rows that have been written to the spreadsheet.
58  # @param count_col_totals (list) list of integers - each representing the sum of a particular 'count-column'.
59  def finish(self, total_whq, count_col_totals):
60  self.csv_writer.writerow([''])
61  row = ['', '', '', 'Totals:', total_whq]
62  row.extend(count_col_totals)
63  self.csv_writer.writerow(row)
64  self.csv_file.close()