<commit_before>var authConfig = require('../config/authConfig.js');
var yelp = require('./yelpApi.js');
var Heap = require('heap');
var utils = require('./utils.js');
var fs = require('fs');
/*
Creates an instance of Recommendation Session

@constructor
@this {RecSession}
@param {string} loc The user-input location (zip code, address, etc).
@param {string} uid The randomly generated UID for this session.
@param {number} numRecs number of recommendations to return.
*/

var RecSession = function(loc, uid, numRecs) {
  this.location = loc;
  this.uniqueID = uid;
  this.recommendations = {};
  this.vetoes = {};
  this.numRecs = numRecs;
  this.recQueue = [];
};

/*
Vetoes a restaurant or food category, removing it from the recommendations.

@param {string} foorOrRest Set to either "food" or "rest", this specifies what to veto.
@param {string} value The name of the food or restaurant to veto.
*/

RecSession.prototype.veto = function(foodOrRest, value) {
  if (foodOrRestaurant === 'food') {
    console.log('vetoing food: ', value);
  } else if (foodOrRestaurant === 'rest') {
    console.log('vetoing restaurant: ', value);
  } else {
    console.error('error: invalid argument');
  }
};

/*
Gets the restaurant data from Yelp and saves it in the instance of RecSession.

@this {RecSession}
@param {callback} cb The callback to invoke with (err, data, res)
*/

RecSession.prototype.getYelpData = function(cb) {
  var yelpClient = new yelp.YelpClient(authConfig);
  var searchParams = {
    sort: 1,
    radius_filter: 1000,
    location: this.location
  };

  yelpClient.searchRestaurants(searchParams, function(err, data, res) {
    if (err) {
      console.error('Error - Yelp API returned: ', err);
    }

    this.yelpData = data;
    cb(err, data, res);
  }.bind(this));
};
/*
Builds the recommendation queue.

@param {callback} cb The callback to invoke with (recommendations)
*/
RecSession.prototype.buildRecommendation = function(cb){
  var subCallback = function(err, data, res){
    if(!err){
      this.lat = data.region.center.latitude;
      this.longi = data.region.center.longitude;
      this.recQueue = data.businesses;
      this.recQueue.sort(function(a, b){
        return utils.calculateScore(this.lat, this.longi, a) - utils.calculateScore(this.lat, this.longi, b);
      }.bind(this));

      for(var i=0;i<this.numRecs && this.recQueue.length > 0;i++){
        var current = this.recQueue.pop();
        var vetoed = false;
        if(this.vetoes[current.id]){
          vetoed = true;
        };
        for(var j=0;j++;j<current.categories.length){
          if(this.vetoes[current.categories[i]]){
            vetoed = true;
          }
        };
        if(!vetoed){
          this.recommendations[current.id] = current;
        }else{
          i --;
        }
      }

      cb(this.recommendations);
    }
  };

  this.getYelpData(subCallback.bind(this));
};
/*
Returns recs

@returns {object} Object of recommendations keyed by id.
*/
RecSession.prototype.getRecs = function(){
  return this.recommendations;
};
/*
Adds veto to session

@param {object} Takes command object of form {key: 'id'|'object', val: valueToVeto}
*/
RecSession.prototype.veto = function(command) {
  // Refactor using lodash later.
  this.vetoes[command.val] = true;
  if(command.key === 'id'){
    delete this.recommendations[command.val];
  }else{
    var keys = Object.keys(this.recommendations);
    for(var i=0;i<keys.length;i++){
      if(this.recommendations[keys[i]].categories[0].indexOf(command.val) > -1){
        delete this.recommendations[keys[i]];
      }
    }
  };
  for(var i=0;i<this.numRecs && this.recQueue.length > 0 && Object.keys(this.recommendations).length < this.numRecs;i++){
    var current = this.recQueue.pop();
    var vetoed = false;
    if(this.vetoes[current.id]){
      vetoed = true;
    };
    // First in array is food-related;
    for(var j=0;j++;j<current.categories[0].length){
      if(this.vetoes[current.categories[0][i]]){
        vetoed = true;
      }
    };
    if(!vetoed){
      this.recommendations[current.id] = current;
    }else{
      i --;
    }
  };

};

module.exports.RecSession = RecSession;

// // (location, uid, numRecommendations)
// var testsesh = new RecSession('94103', 0, 3);
// testsesh.veto({key:'id', val:'crepes-a-go-go-san-francisco-2'})
// testsesh.buildRecommendation(function(recs){
//   console.log(Object.keys(recs))
//   testsesh.veto({key:'id', val:'so-san-francisco-4'})
//   console.log(Object.keys(testsesh.getRecs()));
//   testsesh.veto({key:'category', val:'asianfusion'})
//   console.log(Object.keys(testsesh.getRecs()));
// })
<commit_msg>Removed old veto<commit_after>var authConfig = require('../config/authConfig.js');
var yelp = require('./yelpApi.js');
var Heap = require('heap');
var utils = require('./utils.js');
var fs = require('fs');
/*
Creates an instance of Recommendation Session

@constructor
@this {RecSession}
@param {string} loc The user-input location (zip code, address, etc).
@param {string} uid The randomly generated UID for this session.
@param {number} numRecs number of recommendations to return.
*/

var RecSession = function(loc, uid, numRecs) {
  this.location = loc;
  this.uniqueID = uid;
  this.recommendations = {};
  this.vetoes = {};
  this.numRecs = numRecs;
  this.recQueue = [];
};

/*
Gets the restaurant data from Yelp and saves it in the instance of RecSession.

@this {RecSession}
@param {callback} cb The callback to invoke with (err, data, res)
*/

RecSession.prototype.getYelpData = function(cb) {
  var yelpClient = new yelp.YelpClient(authConfig);
  var searchParams = {
    sort: 1,
    radius_filter: 1000,
    location: this.location
  };

  yelpClient.searchRestaurants(searchParams, function(err, data, res) {
    if (err) {
      console.error('Error - Yelp API returned: ', err);
    }

    this.yelpData = data;
    cb(err, data, res);
  }.bind(this));
};
/*
Builds the recommendation queue.

@param {callback} cb The callback to invoke with (recommendations)
*/
RecSession.prototype.buildRecommendation = function(cb){
  var subCallback = function(err, data, res){
    if(!err){
      this.lat = data.region.center.latitude;
      this.longi = data.region.center.longitude;
      this.recQueue = data.businesses;
      this.recQueue.sort(function(a, b){
        return utils.calculateScore(this.lat, this.longi, a) - utils.calculateScore(this.lat, this.longi, b);
      }.bind(this));

      for(var i=0;i<this.numRecs && this.recQueue.length > 0;i++){
        var current = this.recQueue.pop();
        var vetoed = false;
        if(this.vetoes[current.id]){
          vetoed = true;
        };
        for(var j=0;j++;j<current.categories.length){
          if(this.vetoes[current.categories[i]]){
            vetoed = true;
          }
        };
        if(!vetoed){
          this.recommendations[current.id] = current;
        }else{
          i --;
        }
      }

      cb(this.recommendations);
    }
  };

  this.getYelpData(subCallback.bind(this));
};
/*
Returns recs

@returns {object} Object of recommendations keyed by id.
*/
RecSession.prototype.getRecs = function(){
  return this.recommendations;
};
/*
Adds veto to session

@param {object} Takes command object of form {key: 'id'|'object', val: valueToVeto}
*/
RecSession.prototype.veto = function(command) {
  // Refactor using lodash later.
  this.vetoes[command.val] = true;
  if(command.key === 'id'){
    delete this.recommendations[command.val];
  }else{
    var keys = Object.keys(this.recommendations);
    for(var i=0;i<keys.length;i++){
      if(this.recommendations[keys[i]].categories[0].indexOf(command.val) > -1){
        delete this.recommendations[keys[i]];
      }
    }
  };
  for(var i=0;i<this.numRecs && this.recQueue.length > 0 && Object.keys(this.recommendations).length < this.numRecs;i++){
    var current = this.recQueue.pop();
    var vetoed = false;
    if(this.vetoes[current.id]){
      vetoed = true;
    };
    // First in array is food-related;
    for(var j=0;j++;j<current.categories[0].length){
      if(this.vetoes[current.categories[0][i]]){
        vetoed = true;
      }
    };
    if(!vetoed){
      this.recommendations[current.id] = current;
    }else{
      i --;
    }
  };

};

module.exports.RecSession = RecSession;

// // (location, uid, numRecommendations)
// var testsesh = new RecSession('94103', 0, 3);
// testsesh.veto({key:'id', val:'crepes-a-go-go-san-francisco-2'})
// testsesh.buildRecommendation(function(recs){
//   console.log(Object.keys(recs))
//   testsesh.veto({key:'id', val:'so-san-francisco-4'})
//   console.log(Object.keys(testsesh.getRecs()));
//   testsesh.veto({key:'category', val:'asianfusion'})
//   console.log(Object.keys(testsesh.getRecs()));
// })

{'fraction_non_alphanumeric': 0.1582791281524592, 'fraction_numerical': 0.005705808513066302, 'mean_word_length': 3.233816425120773, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, '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': '183951', 'n_tokens_mistral': 3081, 'n_tokens_neox': 2958, 'n_words': 635}