<commit_before><?php

require_once "lib_constants.inc";
require_once "AcademicCalendar.php";
require_once "DiskCache.inc";
require_once 'html2text.php';

define('CATEGORY_QUERY_BASE', 'fq_dept_area_category=dept_area_category:"');
define('TERM_QUERY','&fq_coordinated_semester_yr=coordinated_semester_yr:"Sep+to+Dec+2010+(Fall+Term)"&');
define('TERM', 'Fall2010');
define('SCHOOL_QUERY_BASE', '&fq_school_nm=school_nm:"');

function compare_courseNumber($a, $b)
{
  return strnatcmp($a['name'], $b['name']);
}


class MeetingTime {
  const SUN = 1;
  const MON = 2;
  const TUES = 3;
  const WED = 4;
  const THURS = 5;
  const FRI = 6;
  const SAT = 7;
  
  private $days;
  private $startTime;
  private $endTime;
  private $location = NULL;

  function __construct($daysArr, $startTime, $endTime, $location) {
    $this->days = $daysArr;
    $this->startTime = $startTime;
    $this->endTime = $endTime;
    $this->location = $location;
  }

  public function isLocationKnown() {
    return !is_null($this->location);
  }
  
  public function daysText() {
    // For use when we have multiple days for the same lecture
    $shortVersions = array(MeetingTime::SUN => "Su", MeetingTime::MON => "M",
                           MeetingTime::TUES => "Tu", MeetingTime::WED => "W",
                           MeetingTime::THURS => "Th", MeetingTime::FRI => "F",
                           MeetingTime::SAT => "Sa");
    // For use when we have one day for a given lecture
    $longerVersions = array(MeetingTime::SUN => "Sun", MeetingTime::MON => "Mon",
                            MeetingTime::TUES => "Tue", MeetingTime::WED => "Wed",
                            MeetingTime::THURS => "Thu", MeetingTime::FRI => "Fri",
                            MeetingTime::SAT => "Sat");

    $textMapping = (count($this->days) > 1) ? $shortVersions : $longerVersions;
    $daysTextArr = array();
    foreach ($this->days as $day) {
      $daysTextArr[] = $textMapping[$day];
    }
    
    return implode(" ", $daysTextArr);
  }
  
  public function timeText() {
    // If they're both AM or PM, the start time doesn't need it's own "am"/"pm"
    if (strftime("%p", $this->startTime) == strftime("%p", $this->endTime)) {
      $startTimeFormat = "%l:%M";
    }
    else {
      $startTimeFormat = "%l:%M%p";
    }

    // strftime is adding a trailing space.  I have no idea why.  But we trim.
    $text = trim(strftime($startTimeFormat, $this->startTime)) . "-" .
            trim(strftime("%l:%M%p", $this->endTime));

    // I know, %P should return lowercase... but it's returning "A" or "P"
    return strtolower($text);
  }
  
  public function locationText() {
    return ($this->location == null) ? "TBA" : $this->location;
  }
}


/* Scenarios we've seen:
 * 
 * 1. Single time and location.  Days often come as one concatanated word...
 *    Ex: MondayWednesday 1:00 p.m. - 2:30 p.m.
 *
 * 2. Multiple times and locations:
 *    MondayTuesdayWednesdayThursday Monday Tuesday Wednesday Thursday 9:00 
 *    a.m. -10:00 a.m.; Monday Tuesday Wednesday Thursday 11:00 a.m. -12:00 
 *    p.m.; Monday Tuesday Wednesday Thursday 10:00 a.m. -11:00 a.m.
 * 
 */

class MeetingTimes {
  // If we run into errors while parsing, we'll fall back to just echoing this.
  private $rawTimesText;
  private $rawLocationsText;

  private $parseSucceeded = false;
  private $meetingTimes = array();
  
  function __construct($timesText, $locationsText) {
    $this->rawTimesText = $timesText;
    $this->rawLocationsText = $locationsText;
    $this->parse();
  }
  
  public function all() {
    return $this->meetingTimes;
  }
  
  private function parse() {
    $rawTimesArr = explode(";", $this->rawTimesText);
    $rawLocationsArr = explode(",", $this->rawLocationsText);

    //if (true || count($rawTimesArr) != count($rawLocationsArr)) {
    //  return; // Something's gone south here, handle it semi-gracefully.
    //}

    $i = 0;
    foreach ($rawTimesArr as $timesText) {
      $days = $this->parseDaysFromStr($timesText);
      $startTime = $this->parseStartTimeFromStr($timesText);
      $endTime = $this->parseEndTimeFromStr($timesText);
      $location = $this->parseLocationFromStr($rawLocationsArr[$i]);
      
      $this->meetingTimes[] = new MeetingTime($days, $startTime, $endTime, $location);
    }
    $this->parseSucceeded = true;
  }
  
  /*
   * Accepts: String like: MondayTuesdayWednesdayThursday Monday Tuesday <commit_msg>Sorted lecture time listing by time<commit_after><?php

require_once "lib_constants.inc";
require_once "AcademicCalendar.php";
require_once "DiskCache.inc";
require_once 'html2text.php';

define('CATEGORY_QUERY_BASE', 'fq_dept_area_category=dept_area_category:"');
define('TERM_QUERY','&fq_coordinated_semester_yr=coordinated_semester_yr:"Sep+to+Dec+2010+(Fall+Term)"&');
define('TERM', 'Fall2010');
define('SCHOOL_QUERY_BASE', '&fq_school_nm=school_nm:"');

function compare_courseNumber($a, $b)
{
  return strnatcmp($a['name'], $b['name']);
}

class MeetingTime {
  const SUN = 1;
  const MON = 2;
  const TUES = 3;
  const WED = 4;
  const THURS = 5;
  const FRI = 6;
  const SAT = 7;
  
  private $days;
  private $startTime;
  private $endTime;
  private $location = NULL;

  function __construct($daysArr, $startTime, $endTime, $location) {
    $this->days = $daysArr;
    $this->startTime = $startTime;
    $this->endTime = $endTime;
    $this->location = $location;
  }

  static function cmp($a, $b) {
    if ($a->startTime == $b->startTime) {
      return 0;
    }
    return ($a > $b) ? 1 : -1;
  }

  public function isLocationKnown() {
    return !is_null($this->location);
  }
  
  public function daysText() {
    // For use when we have multiple days for the same lecture
    $shortVersions = array(MeetingTime::SUN => "Su", MeetingTime::MON => "M",
                           MeetingTime::TUES => "Tu", MeetingTime::WED => "W",
                           MeetingTime::THURS => "Th", MeetingTime::FRI => "F",
                           MeetingTime::SAT => "Sa");
    // For use when we have one day for a given lecture
    $longerVersions = array(MeetingTime::SUN => "Sun", MeetingTime::MON => "Mon",
                            MeetingTime::TUES => "Tue", MeetingTime::WED => "Wed",
                            MeetingTime::THURS => "Thu", MeetingTime::FRI => "Fri",
                            MeetingTime::SAT => "Sat");

    $textMapping = (count($this->days) > 1) ? $shortVersions : $longerVersions;
    $daysTextArr = array();
    foreach ($this->days as $day) {
      $daysTextArr[] = $textMapping[$day];
    }
    
    return implode(" ", $daysTextArr);
  }
  
  public function timeText() {
    // If they're both AM or PM, the start time doesn't need it's own "am"/"pm"
    if (strftime("%p", $this->startTime) == strftime("%p", $this->endTime)) {
      $startTimeFormat = "%l:%M";
    }
    else {
      $startTimeFormat = "%l:%M%p";
    }

    // strftime is adding a trailing space.  I have no idea why.  But we trim.
    $text = trim(strftime($startTimeFormat, $this->startTime)) . "-" .
            trim(strftime("%l:%M%p", $this->endTime));

    // I know, %P should return lowercase... but it's returning "A" or "P"
    return strtolower($text);
  }
  
  public function locationText() {
    return ($this->location == null) ? "TBA" : $this->location;
  }
}


/* Scenarios we've seen:
 * 
 * 1. Single time and location.  Days often come as one concatanated word...
 *    Ex: MondayWednesday 1:00 p.m. - 2:30 p.m.
 *
 * 2. Multiple times and locations:
 *    MondayTuesdayWednesdayThursday Monday Tuesday Wednesday Thursday 9:00 
 *    a.m. -10:00 a.m.; Monday Tuesday Wednesday Thursday 11:00 a.m. -12:00 
 *    p.m.; Monday Tuesday Wednesday Thursday 10:00 a.m. -11:00 a.m.
 * 
 */

class MeetingTimes {
  // If we run into errors while parsing, we'll fall back to just echoing this.
  private $rawTimesText;
  private $rawLocationsText;

  private $parseSucceeded = false;
  private $meetingTimes = array();
  
  function __construct($timesText, $locationsText) {
    $this->rawTimesText = $timesText;
    $this->rawLocationsText = $locationsText;
    $this->parse();
    usort($this->meetingTimes, array("MeetingTime", "cmp"));
  }
  
  public function all() {
    return $this->meetingTimes;
  }
  
  private function parse() {
    $rawTimesArr = explode(";", $this->rawTimesText);
    $rawLocationsArr = explode(",", $this->rawLocationsText);

    //if (true || count($rawTimesArr) != count($rawLocationsArr)) {
    //  return; // Something's gone south here, handle it semi-gracefully.
    //}

    $i = 0;
    foreach ($rawTimesArr as $timesText) {
      $days = $this->parseDaysFromStr($timesText);
      $startTime = $this->parseStartTimeFromStr($timesText);
      $endTime = $this->parseEndTimeFromStr($timesText);
      $location = $this->parseLocationFromStr($rawLocationsArr[$i]);
      
      $this->meetingTimes[] = new MeetingTime($days, $startTime, $endTime, $location);
    }
    $this->parseSucceeded = true;
  }
  
  /*
   * Accepts: String like: MondayTuesdayWednesdayThursday Monday Tuesday 
{'fraction_non_alphanumeric': 0.15561674008810572, 'fraction_numerical': 0.011123348017621146, 'mean_word_length': 3.0886987843313825, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2098676', 'n_tokens_mistral': 3139, 'n_tokens_neox': 2972, 'n_words': 722}