Last active
August 29, 2015 14:00
-
-
Save Idorobots/11405377 to your computer and use it in GitHub Desktop.
Half-assed apriori attempt.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module apriori; | |
import std.stdio; | |
import std.string; | |
import std.conv; | |
bool contains(A, E)(A array, E element) { | |
foreach(E e; array) { | |
if(e == element) return true; | |
} | |
return false; | |
} | |
void main(string[] args) { | |
if(args.length < 2) { | |
writeln("USAGE: ", args[0], " supportThreshold"); | |
return; | |
} | |
auto supportProc = to!double(args[1]); | |
string[][] input; | |
ulong[string] itemSets; | |
ulong allRules; | |
string[][string] rules; | |
// Build 1-item sets | |
foreach(line; stdin.byLine()) { | |
auto words = line.idup.strip().split(","); | |
input ~= words; | |
foreach(word; words) { | |
itemSets[word]++; | |
} | |
} | |
double supportThreshold = supportProc * input.length; | |
// Build frequent 1-item sets | |
ulong[string] tmp; | |
foreach(key, support; itemSets) { | |
if(support > supportThreshold) tmp[key] = support; | |
} | |
itemSets = tmp; | |
// Build frequent 2-item sets | |
foreach(key1; itemSets.keys) { | |
foreach(key2; itemSets.keys) { | |
if(key1 != key2) { | |
ulong support; | |
foreach(line; input) { | |
if(contains(line, key1) && contains(line, key2)) support++; | |
} | |
if(support > supportThreshold) { | |
allRules += 2; | |
rules[key1] ~= key2; | |
rules[key2] ~= key1; | |
} | |
} | |
} | |
} | |
// Etc... | |
// Build index mapping. | |
ulong[string] indexMapping; | |
foreach(i, key; itemSets.keys) { | |
indexMapping[key] = i; | |
} | |
// JSON output as a graph. | |
writeln("{\"nodes\": ["); | |
foreach(name, support; itemSets) { | |
writeln("\t{"); | |
writeln("\t\t\"id\": ", indexMapping[name], ","); | |
writeln("\t\t\"label\": \"", name, "\","); | |
writeln("\t\t\"support\": ", (cast(double) support) / input.length); | |
write("\t}"); | |
if(indexMapping[name] < indexMapping.length-1) write(","); | |
writeln(); | |
} | |
writeln("], \"edges\": ["); | |
ulong i; | |
foreach(name; itemSets.keys) { | |
if(name !in rules) continue; | |
foreach(target; rules[name]) { | |
writeln("\t{"); | |
writeln("\t\t\"id\": ", i++, ","); | |
writeln("\t\t\"source\": ", indexMapping[name], ","); | |
writeln("\t\t\"target\": ", indexMapping[target]); | |
write("\t}"); | |
if(i < allRules) write(","); | |
writeln(); | |
} | |
} | |
writeln("]}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment