Skip to content

Instantly share code, notes, and snippets.

@sguzman
Created June 16, 2020 08:46
Show Gist options
  • Save sguzman/b814f00d8bf271b144e99f561db2b10e to your computer and use it in GitHub Desktop.
Save sguzman/b814f00d8bf271b144e99f561db2b10e to your computer and use it in GitHub Desktop.
Recursive func for producing minimal grammar based on most frequent sequence
func[{
junk_,
c_,
cardinalList_,
carg_
}] :=
With[{
cardinal = Association[cardinalList]
},
With[{
data = Select[
Table[
With[{
list = Tally@Partition[c, i, 1]
},
Splice@
Table[{
First@j,
Total@
Map[cardinal, First@j] Last@j,
i Last@j,
Last@j},
{j, list}]],
{i, 1, 100, 1}],
And[
Last@# != 1,
Length@First@# != 1]
&]
},
With[{
first = First@
First@
TakeLargestBy[data
, #[[2]] &, 1],
rankedData = TakeLargestBy[data
, #[[2]] &, 10]
},
With[{
grid = Grid@
Prepend[
rankedData,
{"Pattern", "Strength", "Letters", "Freq"}
]
},
{
grid,
SequenceReplace[c,
first :> C[carg]],
Prepend[cardinalList, C[carg] -> Length@first],
carg + 1
}
]]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment