<commit_before># RiveScript.js
#
# This code is released under the MIT License.
# See the "LICENSE" file for more information.
#
# http://www.rivescript.com/
"use strict"

# Brain logic for RiveScript
utils = require("./utils")
inherit_utils = require("./inheritance")
RSVP = require("rsvp")

##
# Brain (RiveScript master)
#
# Create a Brain object which handles the actual process of fetching a reply.
##
class Brain
  constructor: (master) ->
    @master = master
    @strict = master._strict
    @utf8   = master._utf8

    # Private variables only relevant to the reply-answering part of RiveScript.
    @_currentUser = null # The current user asking for a message

  # Proxy functions
  say: (message) ->
    @master.say message
  warn: (message, filename, lineno) ->
    @master.warn message, filename, lineno


  ##
  # string reply (string user, string msg[, scope])
  #
  # Fetch a reply for the user.
  ##
  reply: (user, msg, scope, async) ->
    @say "Asked to reply to [#{user}] #{msg}"

    # Store the current user's ID.
    @_currentUser = user

    # Format their message.
    msg   = @formatMessage(msg)
    reply = ""

    # Set initial match to be undefined
    if @master.getUservars(user)
      @master._users[user].__initialmatch__ = undefined

    # If the BEGIN block exists, consult it first.
    if @master._topics.__begin__
      begin = @_getReply(user, "request", "begin", 0, scope)

      # OK to continue?
      if begin.indexOf("{ok}") > -1
        reply = @_getReply(user, msg, "normal", 0, scope)
        begin = begin.replace(/\{ok\}/g, reply)

      reply = begin
      reply = @processTags(user, msg, reply, [], [], 0, scope)
    else
      reply = @_getReply(user, msg, "normal", 0, scope)

    reply = @processCallTags(reply, scope, async)

    if not utils.isAPromise(reply)
      @onAfterReply(msg, user, reply)
    else
      reply.then (result) =>
        @onAfterReply(msg, user, result)

    return reply

  onAfterReply: (msg, user, reply) ->
    # Save their reply history
    @master._users[user].__history__.input.pop()
    @master._users[user].__history__.input.unshift(msg)
    @master._users[user].__history__.reply.pop()
    @master._users[user].__history__.reply.unshift(reply)

    # Unset the current user ID.
    @_currentUser = undefined

  ##
  # string|Promise processCallTags (string reply, object scope, bool async)
  #
  # Process <call> tags in the preprocessed reply string.
  # If `async` is true, processCallTags can handle asynchronous subroutines
  # and it returns a promise, otherwise a string is returned
  ##
  processCallTags: (reply, scope, async) ->
    reply = reply.replace(/«__call__»/ig, "<call>")
    reply = reply.replace(/«\/__call__»/ig, "</call>")
    callRe = /<call>([\s\S]+?)<\/call>/ig
    argsRe = /«__call_arg__»([\s\S]*?)«\/__call_arg__»/mig

    giveup = 0
    matches = {}
    promises = []

    while true
      giveup++<commit_msg>Don't need /m on regexp<commit_after># RiveScript.js
#
# This code is released under the MIT License.
# See the "LICENSE" file for more information.
#
# http://www.rivescript.com/
"use strict"

# Brain logic for RiveScript
utils = require("./utils")
inherit_utils = require("./inheritance")
RSVP = require("rsvp")

##
# Brain (RiveScript master)
#
# Create a Brain object which handles the actual process of fetching a reply.
##
class Brain
  constructor: (master) ->
    @master = master
    @strict = master._strict
    @utf8   = master._utf8

    # Private variables only relevant to the reply-answering part of RiveScript.
    @_currentUser = null # The current user asking for a message

  # Proxy functions
  say: (message) ->
    @master.say message
  warn: (message, filename, lineno) ->
    @master.warn message, filename, lineno


  ##
  # string reply (string user, string msg[, scope])
  #
  # Fetch a reply for the user.
  ##
  reply: (user, msg, scope, async) ->
    @say "Asked to reply to [#{user}] #{msg}"

    # Store the current user's ID.
    @_currentUser = user

    # Format their message.
    msg   = @formatMessage(msg)
    reply = ""

    # Set initial match to be undefined
    if @master.getUservars(user)
      @master._users[user].__initialmatch__ = undefined

    # If the BEGIN block exists, consult it first.
    if @master._topics.__begin__
      begin = @_getReply(user, "request", "begin", 0, scope)

      # OK to continue?
      if begin.indexOf("{ok}") > -1
        reply = @_getReply(user, msg, "normal", 0, scope)
        begin = begin.replace(/\{ok\}/g, reply)

      reply = begin
      reply = @processTags(user, msg, reply, [], [], 0, scope)
    else
      reply = @_getReply(user, msg, "normal", 0, scope)

    reply = @processCallTags(reply, scope, async)

    if not utils.isAPromise(reply)
      @onAfterReply(msg, user, reply)
    else
      reply.then (result) =>
        @onAfterReply(msg, user, result)

    return reply

  onAfterReply: (msg, user, reply) ->
    # Save their reply history
    @master._users[user].__history__.input.pop()
    @master._users[user].__history__.input.unshift(msg)
    @master._users[user].__history__.reply.pop()
    @master._users[user].__history__.reply.unshift(reply)

    # Unset the current user ID.
    @_currentUser = undefined

  ##
  # string|Promise processCallTags (string reply, object scope, bool async)
  #
  # Process <call> tags in the preprocessed reply string.
  # If `async` is true, processCallTags can handle asynchronous subroutines
  # and it returns a promise, otherwise a string is returned
  ##
  processCallTags: (reply, scope, async) ->
    reply = reply.replace(/«__call__»/ig, "<call>")
    reply = reply.replace(/«\/__call__»/ig, "</call>")
    callRe = /<call>([\s\S]+?)<\/call>/ig
    argsRe = /«__call_arg__»([\s\S]*?)«\/__call_arg__»/ig

    giveup = 0
    matches = {}
    promises = []

    while true
      giveup++
{'fraction_non_alphanumeric': 0.13499232998125107, 'fraction_numerical': 0.0027271177773990113, 'mean_word_length': 3.041322314049587, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4641335', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1860, 'n_words': 585}