Rule Set Examples

An Ascribe Rule Set lets you programmatically alter the results of linguistic analysis.  See these posts for information on what Rule Sets do and how to author them.

Modify Finding Rule Examples

Rule Sets let you modify the results of linguistic analysis to correct problems or tailor the analysis to your business vertical.

Correct Sentiment Polarity

Suppose you have analyzed survey responses about a proposed new product offering.  You observe that the comment:

I disliked nothing about the product.

produces a finding of negative sentiment, with a topic of “nothing” and an expression of “disliked”:

Disliked nothing

Looking through the survey responses you find that there are a few examples of this problem, always with a topic of “nothing”.  Of course, the topic “nothing” effectively reverses the sentiment finding, but the linguistic analysis engine was not smart enough to realize that.  You can fix this problem with a simple Modify Finding rule:

// If topic is "nothing" reverse the sentiment polarity
if (f.s && /^nothing$/i.test(f.t)) {
  f.s = -f.s;
}

If the finding has sentiment and the topic is “nothing”, we reverse the sentiment polarity.

Uppercase Brand Name

In this example suppose our Inspection contains mentions of our brand.  When our brand name appears as the topic we would like to see it in upper case.  Here is a Modify Finding rule to do that:

// Uppercase our brand
f.t = f.t.replace(/\bascribe\b/ig, "Ascribe");

The regular expression matches the word “ascribe”.  The \b operators match word boundaries, so our regular expression matches the word “ascribe”, but not “ascribes”.  The i flag makes the expression ignore case, and the g flag causes all occurrences of the match to be replaced.

This rule will correct the case of the topics but will not change the comment itself.  If we wanted to do that we would need a Modify Response on Load rule.

Veto Finding Rule Example

If our business is windshield repair, comments from our customers will commonly mention broken windshields.  The linguistic analyzer will merrily produce findings of negative sentiment to these mentions.  But for us that’s not a negative thing, that’s our business!  We may well want to discard findings of negative sentiment about broken windshields.  We can use this Veto Finding rule:

// Discard negative sentiment findings for "broken"
if (f.s < 0) {
  if (f.e == "broken") {
    if (/glass|window|windshield/i.test(f.t)) {
      return true;
    }
  }
}

Recall from Authoring Rule Sets that when a Veto Finding rule returns a Boolean value of true, the finding will be discarded from the analysis.  The rule above will veto findings with an expression of “broken” and a topic that contains “glass”, “window”, or “windshield”.

Add Finding from Finding Rule Example

Let’s suppose that we are in the roof repair business.  Two of the comments we received are:

  • The repair of the roof is inadequate.
  • The repair of the roof behind the chimney is inadequate.

Sentiment analysis correctly gives us a negative sentiment finding with topic of “repair” and expression of “inadequate” for the first comment, but not for the second.  The more complex structure of the second comment caused it to miss the inadequate repair.  But we really want to find these mentions of inadequate repair.  That’s core to our business.  What can we do?  A simple approach would be to use an Add Finding from Response rule that looks for the words “repair” and “inadequate” in the response, and add a negative sentiment finding.  But that approach is going to introduce a lot of incorrect findings, because it does not use any knowledge of the sentence structure.  A better way would be to see whether the topic analysis could be used to improve our sentiment analysis results.  Looking at the topic analysis we find that we do have a finding of “repair” and an expression of “is inadequate”.  The topic analysis did a better job of finding inadequate repair than the sentiment analysis did.  We can use an Add Finding from Finding rule to generate a new finding of negative sentiment from this topic finding:

// Add sentiment finding for inadequate repair
if (f.s == null) {
  if (/repair/i.test(f.t)) {
    if (/inadequate/i.test(f.e)) {
      f.t = "repair";
      f.e = "inadequate";
      f.s = -2;
      return f;
    }
  }
}

We first test whether this is a topic finding (f.s == null), if so we look for the desired topic and expression.  If we find them, we return a negative sentiment finding topic of “repair” and an expression of “inadequate”.

Because this rule adds findings, we end up both the original topic finding and the new sentiment finding in our Inspection.  If we used the same code as a Modify Finding rule we would end up with only the sentiment finding.  The topic finding would have been converted to a sentiment finding.  One approach is not superior to the other; the approach that is best for you depends on just how you prefer to modify the analysis.

Add Finding from Response Rule Examples

Add Finding from Response rules let you add findings to an Inspection independent of the linguistic analysis engine.  As described in [rule set processing], these rules let you augment the findings from linguistic analysis with findings you create.

One use for Add Finding from Response rules is to create “alerts” that find responses with some set of key words you want to flag.

Suppose we want to flag responses that may have reference to some legal action.  We could look for responses that contain any of the words “lawyer”, “sue”, or “attorney”.  If we find a response with one of these words we add a finding like so:

// Add alert finding for legal issues.
// Search for trouble words and add a new finding if one is found.

// Search pattern for problem words ignoring case.
var troublePattern = /\b(lawyer|sue|att?[oe]rne?y)\b/i;
 
// The array of match results
var arr;

// Check for a trouble word
if ((arr = troublePattern.exec(f.r)) != null) {
  var word = arr[0]; // the captured word

  // Setup the properties of the new finding
  f.t = "Alert!"; // topic
  f.e = word; // expression
  f.x = word; // extract

  // Returning a finding causes it to be added
  return f;
}

Note that our regular expression also captures common misspellings of the word “attorney”.  After the analysis it is simple to search for the topic “Alert!” and find all our alerts.

The rule above will add a single finding for the first problem word found.  What if we want an alert for all the problem words?  We can add multiple findings for a single response by returning an array of findings.  If the array is empty, no findings will be added.  Here is our modified rule:

// Add alert findings for legal issues.
// Search for trouble words and add a new finding for each.

// Search pattern for words with global flag.
var wordPattern = /\w+/g;

// Search pattern for problem words ignoring case.
var troublePattern = /\b(lawyer|sue|att?[oe]rne?y)\b/i;

// The set of new findings to return.
var findingsOut = [];

// Loop through each word in the response.
var arr;
while ((arr = wordPattern.exec(f.r)) != null) {
  var word = arr[0]; // the next word

  // Check for a trouble word
  if (troublePattern.test(word)) {

    // Create a new finding and set its properties
    var newFinding = new Finding();
    newFinding.r = f.r; // response
    newFinding.t = "Alert!"; // topic
    newFinding.e = word; // expression
    newFinding.x = word; // extract

    // Add the new finding to the set of findings to return
    findingsOut.push(newFinding);
  }
}

// Return the new findings
return findingsOut;

Leave a Reply

Your email address will not be published. Required fields are marked *