Tuesday, January 5, 2016

SurveyGizmo Custom Scripting

One nice feature of SurveyGizmo is their server-side custom scripting functionality (example below). It’s a PHP-like scripting language that allows you to modify the survey contents and operation at runtime. The script runs server-side before the page loads and offers a lot options for customizing a survey.

There are several things I would love to see with this feature:

  1. Syntax validation in the editor. Having to go through several survey pages to test your script and then find out you are missing a semi-colon or have a typo is no fun. A basic eval of the script would catch most of these errors.
  2. Using full PHP instead of PHP-lite. Having programmed a survey system I can understand why Survey Gizmo went with a more controlled subset of the language but when I need to use a function that isn’t there I’m not happy about it.
  3. Adding an option to run a script on page save. Currently, the scripts only run on page load so custom logic, validation and other items need to be on the next page. Sometimes it would be nice to just have them on the same page.

Having said that all in all its still pretty powerful functionality that we use with a lot of surveys at Researchscape.

Example of mapping the state question to another question that tracks the four main US Census regions.

Monday, January 4, 2016

SurveyGizmo Hacks

At Researchscape we’ve been using SurveyGizmo to run most of our surveys. It has a lot of flexibility built in but the customization options make it easy to adapt it for our needs. Going to start posting some quick hacks that I’ve used to customize various surveys.

First one up is a simple table customization. By default the table layout is dynamic but if you have a scale question with some labels (eg Strongly Disagree – 1, 2, 3, 4, Strongly Agree – 5) the column will not be equally sized. However we don’t want to change the whole table to a fixed layout which would squeeze the row heading.

Solution is to add a CSS class to the table question and in the Style section add custom CSS to the survey like follows:

.fixed-columns td:nth-child(n+2) {
    width: 8%;
}

Tuesday, December 15, 2015

Scratchpad Tools

Whenever I want to test out some small C# code I use the excellent LINQPad. It's great as a quick scratchpad for everything from a couple of lines of code to a small program. You can reference your own class libraries so it's great for performance testing. I also use it a lot to transform files. The free version is nice but if you can swing it, Pro is worth it just for the auto-completion.

For JavaScript testing and debugging it's hard to beat JSFiddle. I use it often and like the ability to save and share snippets of JS. I was interested when I found .NET Fiddle online. Could it offer the same versatility of LINQPad and the online sharing of JSFiddle.

I took the code from my last blog post and pasted it in .NET Fiddle. Interestingly by default code is evaluated and run as you enter it (you can turn this option off by setting Auto Run to 'No'). The first thing that occurred was that I got this error message:

Fatal Error: Memory usage limit was exceeded

Apparently the number of iterations in my test loop was too many. Once I dialed that back to 100,000 the code ran fine. This code is pretty basic and not creating a lot of large objects so this limit seems low. Also looks like no GC is running so this limit my hinder other tests.

It does have a 'Share' link that gives you either a direct link to the fiddle or a widget that you can embed directly in a blog post. Here is the widget for this post:


Definitely a nice tool for sharing code but think I will stick with LINQPad for most of my scratchpad needs.

Monday, December 7, 2015

All Your Base Are Belong To Us

I wanted to encode a number in Base 36. A simple enough task, but as these things sometimes do, this took me down the rabbit hole of wanting to create a generic routine. I needed a set of methods, one that would take an integer and convert it to an arbitrary base and another to convert it back to a Base 10 number. There are a bunch of examples floating around the web but I wanted to create my own (combining some of the best elements from the code I found). It's a relatively easy problem to solve but along the way I found some interesting tricks to speed up the process.

So here is my C# take on this old problem:

I used LINQPad to create a quick program to test it out.

Friday, November 20, 2015

N-Gram Extraction

For reporting purposes we do some simple word analysis. I've been looking to expand on that by analyzing the frequency of 2 and 3 word pairs. So I've been evaluating some n-gram extraction algorithms.

Wikipedia defines an n-gram as follows:

In the fields of computational linguistics and probability, an n-gram is a contiguous sequence of n items from a given sequence of text or speech. An n-gram of size 1 is referred to as a "unigram"; size 2 is a "bigram" (or, less commonly, a "digram"); size 3 is a "trigram"

Initially I had been using string.split() to break the text into words and process each one but found the overhead to be too high (lots of string copying). So I changed the approach to loop through each character in the text and build up words. To get the word pairs (or triplets) my first thought was to use Queues. First I had to have a queue that was a fixed size. It needed to drop items from the front when new ones were added to the end. From StackOverflow I found a simple example to do just that:

With that in place I could do something like the following:

That worked but still had some unnecessary overhead with a StringBuilder for each word and having to join the words back together to get the bigram. So I rewrote the code using pointers into the text and copying out the words (or word pairs) as I found them. This approach proved to be the fastest.

I also took one more pass and made the code more generic. Rather than using specific variables for each pairing I used an array to hold on each word location. This code was slower than the previous due to the loops and array access. For looking for larger number of n-grams (say 4+) I think this would be the better code to use.

Monday, July 27, 2015

Now more dynamic than ever

The dynamic keyword was introduced in C# 4 and I remember reading about it back then. Most of the articles (like this one) highlighted using it for late binding of method calls. Useful for calling external libraries in a different language or interfacing with old COM code but not something I would normally need in managed C# land.

Well the other day as I was reading about the Visitor pattern in C# I came across this article (Farewell Visitor) which used dynamic dispatch to simplify the visitor code. Very neat use of dynamic to essentially dynamically cast a variable to the underlying object type before calling a matching overridden method.

To see a quick example of this in action past the following code in LinqPad and check out the results.
void Main() {

 var shapes = new List();
 shapes.Add(new Shape());
 shapes.Add(new Circle());
 shapes.Add(new Square());
 
 // Only the Shape method gets called each time
 foreach (var element in shapes) {
  PrintShape(element);
 }
 
 Console.WriteLine();
 
 // Using dynamic the matching object method gets called
 foreach (dynamic element in shapes) {
  PrintShape(element);
 }
}

public void PrintShape(Shape shape) {
 Console.WriteLine("I am a Shape");
}

public void PrintShape(Circle circle) {
 Console.WriteLine("I am a Circle");
}

public void PrintShape(Square square) {
 Console.WriteLine("I am a Square");
}

// Simple shape and two subclasses
public class Shape {
}

public class Circle : Shape {
}

public class Square : Shape {
}

Friday, June 5, 2015

Use the Snipping Tool in Delay Mode

Over the years I've used a number of screen capture tools. They are great for demonstrating how to steps or documenting a bug. In the past, I would recommend Snagit as the best capture software but then in Windows Vista Microsoft added the Snipping Tool. Snagit remains my recommendation for as one of the best full featured capture tool (with video capture and image markup) but for quick screen capture the Snipping Tool is great.

One of the items that appears to be missing from the Snipping Tool is a delayed mode so you can capture a menu or dropdown list after it is expanded. Well it turns out you can do a delayed capture with the Snipping Tool even if it's not obvious how to trigger it.

First start the Snipping Tool. If it's in capture mode hit Cancel so the window looks like:


Next open the menu or drop down the list you want to capture then press the "Ctrl" + "Print Screen" buttons on your keyboard. This will trigger a new capture with the screen locked as it is so you can get an image of the menu.

In Windows 10 it looks like the delayed mode will become easier to do with an explicit UI button to trigger it (info from Neowin)