<commit_before>package blog.debug

import java.util.Properties
import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer

import blog.Main
import blog.common.Util
import blog.model.Evidence
import blog.model.Model
import blog.model.Queries
import blog.sample.LWSampler

class LWDebugger(
  val model: Model,
  val evidence: Evidence,
  val queries: Queries) {

  // All samples so far.
  val samples: ListBuffer[LWSample] = new ListBuffer()

  // Latest sampled world.
  var lastSample: LWSample = null

  // The underlying sampler.
  val sampler = new LWSampler(model, new Properties())
  sampler.initialize(evidence, queries)

  // Compute next sample, print it, and add it to samples.
  def sampleOne = {
    sampler.nextSample()
    lastSample = new LWSample(
      model, sampler.getLatestWorld(), sampler.getLatestLogWeight())
    println(lastSample)
    samples.append(lastSample)
    queries.foreach(query => query.updateStats(lastSample.world, lastSample.logWeight))
  }

  // Compute next n samples.
  def sampleMany(n: Int) {
    for (i <- 1 to n) {
      sampleOne
    }
  }

  // Turn verbosity on or off.
  def verbose = Util.setVerbose(true)
  def noverbose = Util.setVerbose(false)

  // Shortcuts.
  def m = model
  def e = evidence
  def q = queries
  def n = sampleOne
  def s = lastSample
}

object LWDebugger {
  /**
   * Create a LWDebugger for the given model.
   *
   * Example usage from iblog:
   * <code>
   * scala> val d = LWDebugger.make("tmp/burglary.all")
   * scala> import d._
   * scala> n
   * scala> s.eval("Earthquake | JohnCalls")
   * </code>
   */<commit_msg>hist method to print query results so far<commit_after>package blog.debug

import java.util.Properties
import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer

import blog.Main
import blog.common.Util
import blog.io.TableWriter
import blog.model.Evidence
import blog.model.Model
import blog.model.Queries
import blog.sample.LWSampler

class LWDebugger(
  val model: Model,
  val evidence: Evidence,
  val queries: Queries) {

  // All samples so far.
  val samples: ListBuffer[LWSample] = new ListBuffer()

  // Latest sampled world.
  var lastSample: LWSample = null

  // The underlying sampler.
  val sampler = new LWSampler(model, new Properties())
  sampler.initialize(evidence, queries)

  // Compute next sample, print it, and add it to samples.
  def sampleOne = {
    sampler.nextSample()
    lastSample = new LWSample(
      model, sampler.getLatestWorld(), sampler.getLatestLogWeight())
    println(lastSample)
    samples.append(lastSample)
    queries.foreach(query => query.updateStats(lastSample.world, lastSample.logWeight))
    // TODO: print number of samples so far
    // TODO: stats method to print out sampler stats
  }

  // Compute next n samples.
  def sampleMany(n: Int) {
    for (i <- 1 to n) {
      sampleOne
    }
  }

  // Print query results so far.
  def printResults {
    val writer = new blog.io.TableWriter(queries)
    writer.writeResults(System.out)
  }

  // Turn verbosity on or off.
  def verbose = Util.setVerbose(true)
  def noverbose = Util.setVerbose(false)

  // Shortcuts.
  def m = model
  def e = evidence
  def q = queries
  def n = sampleOne
  def s = lastSample
  def hist = printResults
}

object LWDebugger {
  /**
   * Create a LWDebugger for the given model.
   *
   * Example usage from iblog:
   * <code>
   * scala> val d = LWDebugger.make("tmp/burglary.all")
   * scala> import d._
   * scala> n
   * scala> s.eval("Earthquake | JohnCalls")
   * </code>
   */
{'fraction_non_alphanumeric': 0.10204081632653061, 'fraction_numerical': 0.0005591277606933184, 'mean_word_length': 3.175029171528588, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '7463901', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1151, 'n_words': 337}