<commit_before># Description:
#   This bot measures sentiment in all channels its in and tracks analytics
#
# Commands:
#   who's happy?
#   who's stressed?
#   where's the happiness?
#   where's the stress?
#
# Author:
#   <NAME>
#
# Notes
#   Tracks scores by current month. Collects sentiment on users [sentiment:week_digit:user] and
#   channels [sentiment:week_digit:channel] in format:
#
#    [{
#      name: user_or_channe_name
#      year: 'YYYY',
#      score_average: 0,
#      score_count: 0
#    }]
#
# Example:
#   [sentiment:12:user:michi] = {n:a:yyyy} where n is the number of records, and a is the average score of the n records
#   To add a new score: (n + 1), ((a * n) + 2.5 / (n+1)) => new averages
#
sentiment = require('sentiment')
_ = require('underscore')

module.exports = (robot) ->
  # helper method to get the week of the year in numeric form
  getWeekOfYear = (dateObj = new Date()) ->
    onejan = new Date(dateObj.getFullYear(), 0, 1)
    Math.ceil((((dateObj - onejan) / 86400000) + onejan.getDay() + 1) / 7)

  # helper to return 4 digit year
  getYear = (dateObj = new Date()) ->
    dateObj.getFullYear()

  #  Description:
  #    Get The top n records from weekNum. records are of type topType
  #  Params:
  #    ordering: (ascending|descending)
  #    topNumber: Number of records you want back
  #    topType: (channel|user)
  #    weekNum: 0-51
  #  Returns:
  #    [{
  #      username: name
  #      score: 0
  #    }]
  getTopForWeek = (ordering, topNumber, topType, weekNum) ->
    _.sortBy(getAllEntriesForWeek(topType, weekNum), (entry) ->
      (if ordering == 'ascending' then -1 else 1) * entry.score
    ).slice(0, topNumber)

  # Description:
  #  Gets all of the records of type recordType from weekNum
  # Params:
  #  recordType: (channel|user)
  #  weekNum: 0-51
  #  Returns:
  #    [{
  #      username: name
  #      score: 0
  #    }]
  getAllEntriesForWeek = (recordType, weekNum) ->
    (robot.brain[calculateKey(recordType, weekNum)] || {}).data || []


  # Description: returns keys
  #  Params:
  #   recordType: (channel|user)
  #   recordName: channel_name or user_name
  #   weekNum: 0-52
  #
  calculateKey  = (recordType, weekNum) ->
    "sentiment:#{weekNum}:#{recordType}"

  # Description:
  #  Updates the record with the value of sentimentScore
  # Params:
  #  recordType: (channel|user)
  #  recordName: channel_name or user_name
  #  weekNum 0-52
  #  sentimentScore: AFINN Sentiment score
  updateEntry = (recordType, recordName, weekNum, sentimentScore) ->
    masterRecord = (robot.brain[calculateKey(recordType, weekNum)] || {}).data || []
    entry = (_.filter(masterRecord, (x) -> x.name == recordName) || [])[0]

    if !entry || !entry.name
      entry =
        name: recordName
      masterRecord.push entry
    if !entry.year
      entry.year = getYear()
      entry.score_average = 0
      entry.score_count = 0

    entry.score_average = 0 if !entry.score_average
    entry.score_count = 0 if !entry.score_count
    entry.score_average = ((entry.score_average * entry.score_count) + sentimentScore) / (entry.score_count + 1)
    entry.score_count = entry.score_count + 1

    robot.brain[calculateKey(recordType, weekNum)] = {data: masterRecord}

  # helper method to get sender of the message
  get_username = (response) ->
    "@#{response.message.user.name}"

  # helper method to get channel of originating message
  get_channel = (response) ->
    if response.message.room == response.message.user.name
      "@#{response.message.room}"
    else
      "##{response.message.room}"

  # update sentiment of a record
  # Params:
  #   Response
  logSentiment = (response) ->
    analysis = sentiment(response.message.text)
    if(response.message.text.length > 2 && analysis && analysis.score)
      updateEntry('user', get_username(response), getWeekOfYear(), analysis.score)
      updateEntry('channel', get_channel(response), getWeekOfYear(), analysis.score)

  prettyPrintList = (entries) ->
    output = ''
    entries = entries || []
    if entries.length
      for entry, i in entries
        output += "#{i+1}: #{entry.name} (#{entry.average_score})\n"
    output

  onlyNegative = (list) ->
    _.filter(list || [], (x) -> (x.score_average <= 0))
<commit_msg>added score<commit_after># Description:
#   This bot measures sentiment in all channels its in and tracks analytics
#
# Commands:
#   who's happy?
#   who's stressed?
#   where's the happiness?
#   where's the stress?
#
# Author:
#   <NAME>
#
# Notes
#   Tracks scores by current month. Collects sentiment on users [sentiment:week_digit:user] and
#   channels [sentiment:week_digit:channel] in format:
#
#    [{
#      name: user_or_channe_name
#      year: 'YYYY',
#      score_average: 0,
#      score_count: 0
#    }]
#
# Example:
#   [sentiment:12:user:michi] = {n:a:yyyy} where n is the number of records, and a is the average score of the n records
#   To add a new score: (n + 1), ((a * n) + 2.5 / (n+1)) => new averages
#
sentiment = require('sentiment')
_ = require('underscore')

module.exports = (robot) ->
  # helper method to get the week of the year in numeric form
  getWeekOfYear = (dateObj = new Date()) ->
    onejan = new Date(dateObj.getFullYear(), 0, 1)
    Math.ceil((((dateObj - onejan) / 86400000) + onejan.getDay() + 1) / 7)

  # helper to return 4 digit year
  getYear = (dateObj = new Date()) ->
    dateObj.getFullYear()

  #  Description:
  #    Get The top n records from weekNum. records are of type topType
  #  Params:
  #    ordering: (ascending|descending)
  #    topNumber: Number of records you want back
  #    topType: (channel|user)
  #    weekNum: 0-51
  #  Returns:
  #    [{
  #      username: name
  #      score: 0
  #    }]
  getTopForWeek = (ordering, topNumber, topType, weekNum) ->
    _.sortBy(getAllEntriesForWeek(topType, weekNum), (entry) ->
      (if ordering == 'ascending' then -1 else 1) * entry.score
    ).slice(0, topNumber)

  # Description:
  #  Gets all of the records of type recordType from weekNum
  # Params:
  #  recordType: (channel|user)
  #  weekNum: 0-51
  #  Returns:
  #    [{
  #      username: name
  #      score: 0
  #    }]
  getAllEntriesForWeek = (recordType, weekNum) ->
    (robot.brain[calculateKey(recordType, weekNum)] || {}).data || []


  # Description: returns keys
  #  Params:
  #   recordType: (channel|user)
  #   recordName: channel_name or user_name
  #   weekNum: 0-52
  #
  calculateKey  = (recordType, weekNum) ->
    "sentiment:#{weekNum}:#{recordType}"

  # Description:
  #  Updates the record with the value of sentimentScore
  # Params:
  #  recordType: (channel|user)
  #  recordName: channel_name or user_name
  #  weekNum 0-52
  #  sentimentScore: AFINN Sentiment score
  updateEntry = (recordType, recordName, weekNum, sentimentScore) ->
    masterRecord = (robot.brain[calculateKey(recordType, weekNum)] || {}).data || []
    entry = (_.filter(masterRecord, (x) -> x.name == recordName) || [])[0]

    if !entry || !entry.name
      entry =
        name: recordName
      masterRecord.push entry
    if !entry.year
      entry.year = getYear()
      entry.score_average = 0
      entry.score_count = 0

    entry.score_average = 0 if !entry.score_average
    entry.score_count = 0 if !entry.score_count
    entry.score_average = ((entry.score_average * entry.score_count) + sentimentScore) / (entry.score_count + 1)
    entry.score_count = entry.score_count + 1

    robot.brain[calculateKey(recordType, weekNum)] = {data: masterRecord}

  # helper method to get sender of the message
  get_username = (response) ->
    "@#{response.message.user.name}"

  # helper method to get channel of originating message
  get_channel = (response) ->
    if response.message.room == response.message.user.name
      "@#{response.message.room}"
    else
      "##{response.message.room}"

  # update sentiment of a record
  # Params:
  #   Response
  logSentiment = (response) ->
    analysis = sentiment(response.message.text)
    if(response.message.text.length > 2 && analysis && analysis.score)
      updateEntry('user', get_username(response), getWeekOfYear(), analysis.score)
      updateEntry('channel', get_channel(response), getWeekOfYear(), analysis.score)

  prettyPrintList = (entries) ->
    output = ''
    entries = entries || []
    if entries.length
      for entry, i in entries
        output += "#{i+1}: #{entry.name} (#{entry.score_average})\n"
    output

  onlyNegative = (list) ->
    _.filter(list || [], (x) -> (x.score_average <= 0))

{'fraction_non_alphanumeric': 0.13339596054485675, 'fraction_numerical': 0.011272898074213245, 'mean_word_length': 3.1184719535783367, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '1040676', 'n_tokens_mistral': 2916, 'n_tokens_neox': 2775, 'n_words': 735}