-
Notifications
You must be signed in to change notification settings - Fork 95
/
NGramMarkovChainsV9.m
184 lines (144 loc) · 7.16 KB
/
NGramMarkovChainsV9.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(* ::Package:: *)
(*
N-gram Markov chains Mathematica package
Copyright (C) 2014 Anton Antonov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Anton Antonov,
antononcube@gmail.com,
7320 Colbury Ave,
Windermere, Florida, USA.
*)
(*
Mathematica is (C) Copyright 1988-2014 Wolfram Research, Inc.
Protected by copyright law and international treaties.
Unauthorized reproduction or distribution subject to severe civil
and criminal penalties.
Mathematica is a registered trademark of Wolfram Research, Inc.
*)
(* Version 1.0 *)
(*
This package provides an implementation of Markov chains training and generation using the N-gram model.
The implementation utilizes Mathematica's high-dimensional sparse array data structure (SparseArray) and
weighted random sampling (RandomSample).
For the building of an N-gram model of a list or text an N-1 dimensional sparse array is used.
Note that dimensions up to 5 are allowed, but that can be easily changed.
*)
(* TODO
1. It is a good idea to add working precision option to NGramMarkovChainModel.
2. I need to profile and experiment with other ways of making the model arrays column stochastic. Both methods I have programmed so far are slow for larger number of words because of the unpacking of the sparse array of the n-gram model. That is for by default "ColumnStochastic"->False.
*)
BeginPackage["NGramMarkovChain`"]
NGramMarkovChainModel::usage = "NGramMarkovChainModel[ws_List, ngram_Integer] finds Markov chain probabilities from ws for each ngram number of words."
NGramMarkovChainGenerate::usage = "NGramMarkovChainGenerate[m_NGramModel, s_List, n_Integer] generates a list of n words using the n-gram model m starting with the sequence s."
NGramMarkovChainText::usage = "NGramMarkovChainText[textArg_String, ngram_Integer, startWords:{_String..}, nwords_Integer] splits the string textArg according to the value given to the option WordSeparators, finds the ngram Markov chain probabilities, and generates text of nwords."
NGramModel::usage = "Symbol for holding the n-gram model data."
MakeColumnStochastic::usage = "MakeColumnStochastic[m_?ArrayQ] makes the array m column stochastic."
Begin["`Private`"]
(* direct, easy to understand, in place *)
Clear[MakeColumnStochastic]
MakeColumnStochastic[mat_?ArrayQ] :=
Block[{t, inds, IVar, iseq, csmat},
inds = Array[IVar, Length[Dimensions[mat]] - 1];
csmat = mat;
Do[
iseq = Sequence @@ Append[inds, All];
t = Total[csmat[[iseq]]];
If[t > 0,
csmat[[iseq]] = csmat[[iseq]]/t
],
Evaluate[Sequence @@ Transpose[{inds, Most@Dimensions[mat]}]]
];
If[Head[mat] === SparseArray,
SparseArray[csmat],
csmat
]
];
NGramMarkovChainModel::farg = "A list is expected as a first argument.";
NGramMarkovChainModel::sarg = "A positive integer is expected as a second argument.";
Clear[NGramMarkovChainModel]
Options[NGramMarkovChainModel] = {"ColumnStochastic" -> False};
NGramMarkovChainModel[textWords_, numberOfPreviousWords_, opts : OptionsPattern[]] :=
Module[{words, PickWord, markovMat, wordToIndexRules, indexToWordRules, ntuples, inds, randomTextWords, columnStochasticOpt},
If[ ! ListQ[ textWords ],
Message[NGramMarkovChainModel::farg];
Return[{}]
];
If[ !(IntegerQ[numberOfPreviousWords] && numberOfPreviousWords > 0 ),
Message[NGramMarkovChainModel::sarg];
Return[{}]
];
columnStochasticOpt = OptionValue[NGramMarkovChainModel, "ColumnStochastic"];
words = Union[textWords];
wordToIndexRules = Dispatch[Thread[words -> Range[Length[words]]]];
indexToWordRules = Dispatch[Thread[Range[Length[words]] -> words]];
ntuples = Partition[textWords, numberOfPreviousWords + 1, 1];
markovMat = SparseArray[{}, Table[Length[words], {numberOfPreviousWords + 1}]];
Do[
inds = Apply[Sequence, t /. wordToIndexRules];
markovMat[[inds]] = markovMat[[inds]] + 1,
{t, ntuples}];
If[TrueQ[columnStochasticOpt],
markovMat = MakeColumnStochastic[N[markovMat]];
];
NGramModel[markovMat, wordToIndexRules, indexToWordRules]
];
Clear[NGramMarkovChainGenerate]
NGramMarkovChainGenerate[
NGramModel[markovMat_SparseArray,
wordToIndexRules : (_Dispatch | {_Rule ..}),
indexToWordRules : (_Dispatch | {_Rule ..})], startWords_List,
numberOfWords_Integer] :=
Module[{words, numberOfPreviousWords, randomTextWords, PickWord, startNGram, b = True, w},
words = If[TrueQ[Head[wordToIndexRules] === Dispatch], wordToIndexRules[[1, All, 1]], wordToIndexRules[[All, 1]]];
PickWord[inds_] :=
Block[{ps = Total[markovMat[[Sequence @@ inds]]]},
If[ps > 0,
{True,
RandomSample[Normal[markovMat[[Sequence @@ inds]]] -> words,
1][[1]]},
{False, None}
]
];
(*PickWord[inds_]:=RandomSample[Normal[markovMat[[Sequence@@inds]]]->words,1][[1]];*)
PickWord[ss : {_String ..}] := PickWord[ss /. wordToIndexRules];
numberOfPreviousWords = Length[Dimensions[markovMat]] - 1;
startNGram = startWords;
If[Length[startNGram] < numberOfPreviousWords,
startNGram =
Join @@ Table[
startNGram, {Ceiling[
numberOfPreviousWords/Length[startNGram]]}]
];
startNGram = Take[startNGram, -numberOfPreviousWords];
(*randomTextWords=Nest[Append[#,PickWord[Take[#,-numberOfPreviousWords]]]&,startNGram,numberOfWords];*)
randomTextWords =
NestWhile[({b, w} = PickWord[Take[#, -numberOfPreviousWords]]; If[b, Append[#, w ], #]) &, startNGram, b &, 1, numberOfWords];
randomTextWords
] /; Equal @@ Join[
Dimensions[markovMat],
{If[TrueQ[Head[wordToIndexRules] === Dispatch],
Length[wordToIndexRules[[1]]], Length[wordToIndexRules]],
If[TrueQ[Head[indexToWordRules] === Dispatch],
Length[indexToWordRules[[1]]], Length[indexToWordRules]]}] &&
numberOfWords > 0;
Clear[NGramMarkovChainText]
Options[NGramMarkovChainText] = {WordSeparators -> {Whitespace, "\n", " ", ".", ",", "!", "?", ";", ":", "-", "\"", "\'"}};
NGramMarkovChainText[text_String, numberOfPreviousWords_Integer, startWords : {_String ..}, numberOfWords_Integer, opts : OptionsPattern[]] :=
Module[{textWords, randomTextWords, wordSeparators, ngMod},
wordSeparators = OptionValue[NGramMarkovChainText, WordSeparators];
textWords = StringSplit[text, wordSeparators];
ngMod = NGramMarkovChainModel[textWords, numberOfPreviousWords, "ColumnStochastic" -> False];
randomTextWords = NGramMarkovChainGenerate[ngMod, startWords, numberOfWords];
StringJoin @@ Riffle[randomTextWords, " "]
] /; 1 <= numberOfPreviousWords <= 5;
End[]
EndPackage[]