In a thread on value co-occurrence, I posted a demo (see post) showing a proof of concept. There was a question as to how the complex looking action code worked. What follows is a cleaner version of what’s in the example in my earlier file.
Edit:
The original solution used a rule but the combination of using a CPU-intensive rule to set the $Text of the current item (which was likely itself an agent) seemed to cause a few problems. A better solution is to pass the output of the action to a separate note. this has the advantage too that the action runs once only (though the stamp can be manually re-applied as needed).
Assumptions (used in making the code):
- All attributes starting ‘$My’ simply indicate the attribute data type to use. Where more that one of that type is needed, I suffix a number (e.g.
$MyList2
). Feel free to use other attribute names as you see fit in you own use. - The values to analyse are from a multi-value (Set or List type) attribute, here called
$OpenCode
. - This code is a rule to be run on an agent which finds only the desired notes. Regardless of other scoping arguments, the query should include
& $OpenCode.size > 1
as only notes where the attribute has 2 or more values will contribute co-occurring pairs for analysis. - The path to the note used to receive the result of the action is stored in a String-type attribute $TargetNote.
- $TargetNote is set as a key attribute in the note/agent whose children are being analysed.
- Where possible Sandbox group system attribute such as $MyString are used with an exta List $MyList2 and a Set $MySet2 need to be added to the document
- All pairs are assumed to have no ordering, i.e. AB is also considered an instance of BA, so AB==BA.
- The loop variable names X, Y and Z have no specific meaning. You can choose others if you prefer.
- If you want to review or alter the code, I suggest doing so in a code note or a text editor before pasting into the Rule Inspector as the code can’t all fit in the visible box.
- This code has not been tested with very large value sets nor for funky values. The presence of (regex) control characters, e.g.
[ \ ^ $ . | ? * + ( )
, in values may cause issue. I’d recommend constraining individual values to A-Z, a-z, 0-9, underscore and space to avoid any excitement. - The code checks only pairs of values and not triples, quartets, etc., of co-occurrence.
The process uses uses 2 stamps, both of which are applied _to the target agent. The first simply resets the target not $Text (erasing it). This is not required, but doing so changes the (Outline view) note icon to show an empty note. When the main action is run the icon changes to show it containing text thus indicating the action has completed. This makes more sense of a real-world document where there may be a lot of data and other rule/agent processes running at the same time. The first stamp’s code:
$Text($TargetNote)=;
The main action is this:
$Text($TargetNote)=;
$MyString =;
$MyNumber =;
$MySet = collect(children,$OpenCode);
$MyList = $MySet.isort;
$MyList2 = $MyList;
$MySet2 = collect(children,$Path(original));
$MyList.each(X){
$MyList2.each(Y){
$MySet2.each(Z){
if(X != Y & ($OpenCode(Z).contains(X) & $OpenCode(Z).contains(Y))){
$MyNumber = $MyNumber + 1;
};
};
if(X != Y & $MyNumber > 0){
$MyString = $MyString + X + " + " + Y + ": " + $MyNumber + "\n";
};
$MyNumber =;
};
$MyList2 = $MyList2 - X;
};
$Text($TargetNote)=$MyString;
The resulting target note’s $Text looks like this:
blue + green: 10
blue + orange: 6
I’ll add another post annotating the big action to explain what’s going on (as this post is long enough!).
The demo is here: http://www.acrobatfaq.com/tbdemos/OpenCode_Example_with_co-occurrence.zip