-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.java
More file actions
267 lines (251 loc) · 23.9 KB
/
Copy pathCipher.java
File metadata and controls
267 lines (251 loc) · 23.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.util.*;
import java.io.*;
public class Cipher
{
private static double[] monograms = {-2.4590411830323085, -4.132173592118468, -3.4531955409664876, -3.2516098508490314, -2.112252173438691, -3.8251527064714383, -3.8697610441135724, -3.0046302876114774, -2.6138608391025646, -6.120303443941524, -4.817500508167032, -3.168547717913935, -3.6784058004087767, -2.63495985306993, -2.5946413300623408, -3.8794754318338596, -6.8682987268792015, -2.7594419004092243, -2.69886207389627, -2.4148441322145864, -3.6187636638103458, -4.5475181901318065, -4.003391919959257, -6.258818713559825, -4.062055152505765, -6.7788668353173};
private static double[][] bigrams = {
{-7.828977153799092, -6.199992666863778, -5.486903460060774, -5.672133633773617, -8.575352026985957, -6.631049334462464, -6.196161636740829, -7.572087502231942, -5.734701535842676, -8.510926722047248, -6.735435967978163, -4.728827043086611, -5.60087935197376, -4.126687413994796, -8.660226665413262, -6.225577153541179, -9.526932549240499, -4.625920315877447, -4.740347953268941, -4.495060962118698, -6.785966805111384, -6.257050298098156, -7.0061394463226145, -8.78623047010508, -5.927584619712791, -8.635463965940183},
{-6.1895772492295595, -8.74425047520848, -9.51021324651144, -10.32564198592901, -5.403168549004295, -10.957550570264262, -11.578024079960228, -10.239598141392637, -6.900305498749895, -9.63566025937169, -11.9841434646532, -6.4345135633210715, -10.023231379651051, -10.615103393153268, -6.271203747234687, -10.533389888507822, -13.572612098177231, -6.841337314794296, -8.028608705587224, -9.219953190222643, -6.2784646427675055, -10.491554522712768, -10.336729525606179, -14.174133135380243, -6.7171580010168785, -13.183914142018677},
{-5.379701792880119, -9.582449714744234, -7.264561565773893, -9.397973705202064, -5.3861010600261965, -9.690115608190977, -10.077969639213073, -5.369617916459656, -6.238262856803997, -11.553401300696997, -6.397177899312942, -6.6344506005984005, -9.623932044733856, -10.043034424033243, -5.0859136073666145, -9.331887113202686, -10.220003558669266, -6.664612517459292, -8.048717720581118, -5.896373015761933, -6.845030493881302, -10.734046111582563, -9.729661001331037, -13.612014217226703, -8.23628517733986, -11.220761886233474},
{-5.504973233302773, -6.562576118392492, -7.041585615267731, -6.985352698472739, -5.0750245028614405, -6.977237562663, -7.479091464177121, -6.849008818593106, -5.295852465578302, -8.595902124310987, -9.014906635535278, -7.256514380107029, -7.106454397485106, -7.327977920702294, -5.797803432904938, -7.22613819624768, -9.633165214590777, -6.631169989161664, -6.02729032398096, -5.61451142331913, -6.603592460098819, -8.158012307390392, -6.781340907446914, -11.968703684353034, -7.575341835771978, -10.694365879169748},
{-4.603124908013125, -6.095848057387898, -5.122529769146092, -4.529336562369005, -5.454306276149594, -5.787800145021811, -6.257342539324975, -6.349208199405558, -5.597696262355526, -8.143243398024675, -7.491659159464165, -5.232468782011528, -5.47355451545818, -4.480323517359489, -5.7674861207241594, -5.73116872905787, -7.992546244818384, -4.026416637297975, -4.327679236665061, -4.879327734955612, -7.0706493888578, -6.013566560702426, -5.678934039293475, -6.640422963363134, -6.353290927158293, -9.1366819834774},
{-6.248755089668228, -8.49057476047506, -7.920386444661891, -8.662281964205071, -6.228459753645313, -6.566053744299377, -8.821762136741182, -7.961444123171651, -5.8875731577817385, -9.681799203828085, -9.846413967465, -7.310418987391821, -8.147773157933381, -8.998647525875052, -5.431546849299091, -8.189773377018085, -11.418907119763375, -6.250977365375038, -7.655387248540154, -5.754856244326014, -7.228093338729667, -9.779749368765517, -8.366080113245115, -12.318942593494317, -8.607737460630913, -11.839751761622084},
{-5.952505775724935, -8.202748860861059, -8.10995463360456, -8.500035521887176, -5.703001351955677, -7.989912583705135, -7.987870024697548, -6.0814128632429165, -6.41142881899951, -10.103870785078817, -10.180891732952606, -7.425422484797591, -8.207713981553129, -7.485545129618988, -6.269209626739786, -8.177053794869396, -11.189551846973158, -6.4274904958470405, -7.005701925946383, -6.377538952365223, -7.045307289703624, -10.049805645909036, -7.922170576686816, -12.586581449418322, -8.392368486122828, -11.92932584305301},
{-4.789229320864666, -8.358058908868255, -8.006588886703586, -8.559796463120401, -3.7599265883973865, -8.553147165814451, -9.216850200495685, -8.086788320329507, -5.057949150295018, -10.033191824889048, -9.93078185223834, -8.215416814255459, -8.069640670515962, -8.047018248888447, -5.389874806433715, -8.393547988652534, -10.66221722463787, -7.025712187006593, -7.470981170576239, -6.249518488686, -7.352458006916923, -9.993785004583481, -8.033194025195149, -13.261357345613533, -8.002852788812731, -11.66702093040966},
{-6.069179486046601, -7.417052954463252, -5.305383113317636, -5.814987548225482, -5.845793532891253, -6.62443442563654, -6.117460812100388, -8.86514303485161, -8.870987984227494, -9.890065003821354, -7.422192282940503, -5.488417226254602, -6.016368759081619, -3.8983403457895744, -5.317485446642999, -7.163417139504451, -9.604218017418148, -5.913971949070288, -4.751633355925417, -4.735998435000144, -8.92242831566275, -6.160484186025253, -8.453111824952332, -8.500526667630902, -10.691076653754022, -7.748274771332241},
{-7.833857902797748, -12.315479431322158, -12.070087794283511, -12.193097424042719, -7.974971090460173, -12.742854649066066, -12.792899555792006, -12.237105178331937, -9.400370314227267, -12.50183387709379, -12.643023633461144, -12.782474175912172, -12.173431773666143, -12.608888224499385, -7.370839515418182, -11.738182171569552, -15.60545117787409, -10.891824166712526, -11.607835160047413, -12.263794056945269, -7.298734527982299, -13.090864710119076, -12.501958224271268, -15.571411131634099, -13.535227894357813, -14.229249124444603},
{-7.330616124701473, -9.15315757725518, -9.239425851136026, -9.652164674391836, -6.006342957827886, -8.993086274484128, -9.936114867778048, -8.802602531728375, -6.611635555042303, -10.938308085996194, -10.502187042550071, -8.5388364966385, -9.094300790415032, -7.728094960597034, -7.807788390711604, -9.351055192844171, -12.64747255009885, -9.05117058723988, -7.200309659237699, -8.004559106197595, -9.051963769857494, -10.986744219944407, -8.700979677949434, -13.653819399320012, -8.963827917258012, -12.864521800421597},
{-5.228748529221521, -7.470304315049709, -7.526929167443945, -6.045119463335359, -4.958073894127222, -7.377780347694004, -8.448337213778853, -8.12976652575331, -5.223891482950056, -9.893566801726502, -8.219943628444634, -5.1677214544791585, -7.576030067355236, -8.65656458933364, -5.624931410988323, -7.4382450159833375, -10.933995382837228, -7.963111732902032, -6.211468330580674, -6.45250622028238, -6.889693259194324, -8.15823678588258, -7.763934843001505, -12.541012315728775, -5.751506666338348, -11.231176115809602},
{-5.28875489242134, -6.955684531733744, -8.27508681015987, -9.10359184772109, -5.067362504185747, -8.707316824171299, -9.626765299704486, -8.71323990616535, -5.873078626315745, -10.379920474819507, -10.569821535303147, -9.109006312407327, -7.055421341390855, -8.954650858843884, -5.810810667396263, -6.31334293045961, -12.218294661035424, -8.786543763509941, -7.005146053767368, -7.254876557811555, -7.0486553943864365, -10.364759989756928, -8.436375221601931, -12.622964131070118, -7.704583152678923, -12.37440593435525},
{-5.212945081871848, -7.090284415009301, -5.649708753940119, -4.5391092150637, -5.063919472778748, -6.772510911757478, -4.719559306261328, -7.0070457094281275, -5.51250554435665, -8.077257180165685, -7.259056165433707, -7.065530684678888, -7.037773440071452, -6.726987165466125, -5.433115472592041, -7.284034981568414, -9.897880872265473, -7.499175605654502, -5.31295727611675, -4.446018467763455, -7.054860181216356, -7.585996035497858, -6.933086774918692, -10.964315090452793, -6.903333633359976, -9.69449314635125},
{-6.491856489228789, -6.545400435792223, -6.337658621731615, -6.34247446643087, -7.410201595135088, -4.952898858926338, -6.94569952441319, -7.191878250534782, -6.6973740145734055, -8.785843151164276, -7.148905288586884, -5.7526381434274985, -5.324297987118074, -4.330402404379988, -6.052636042778337, -6.024459404773764, -10.470166152701438, -4.549316387388455, -5.7621706361319465, -5.371840747463952, -4.934363033680238, -6.377263540817805, -5.690230170021438, -8.80262868209452, -7.712948431839346, -9.847939780890446},
{-5.881349688817246, -9.368014238955118, -9.287486786974707, -9.669656012002482, -5.626406693540305, -9.24383777213017, -9.927232771130775, -7.3333356317060865, -6.656509746966305, -11.428658960717774, -10.860675631683677, -6.088336599650863, -8.443220114088943, -10.399612132054774, -5.893954913852547, -6.788175349722017, -12.356183183779274, -5.7924171827450115, -7.506111425398337, -7.03368717426766, -7.021778483209385, -11.40633491597155, -9.006068860063321, -13.360741718546004, -9.297935683291259, -13.007904478462368},
{-10.98206835208258, -11.972577958204946, -12.912566174141432, -13.118929950408619, -13.48463377846405, -13.107472446518736, -14.336983135895775, -12.772319310169408, -10.983974229567062, -14.985559999235289, -14.57513947959917, -13.017645488128942, -12.768903006212277, -13.94261692085566, -13.039649850179975, -13.477681259149167, -14.363830385931964, -13.492136939967203, -12.242510984784479, -12.451579356424748, -6.944187862029091, -13.84178344351305, -11.73387512188427, -15.54760048294038, -13.763056525503032, -16.552686713597666},
{-5.016966708013534, -7.164136799010213, -6.429767777036786, -6.171896675357018, -4.262345138628024, -7.137590236587504, -6.835972469356209, -7.283839591164291, -5.052895592001438, -9.02944275264434, -6.869801301640337, -6.802673819442793, -6.373464650548217, -6.416864407128832, -4.996743837227958, -7.094318419100276, -10.223902075113202, -6.597655647981296, -5.316208619720656, -5.305958682453527, -6.698510023270668, -7.38151605607522, -7.163601112747615, -11.625070775221502, -6.198517272524331, -10.548517499349522},
{-4.968100904677294, -6.657504267467528, -5.992360737517461, -7.025907626297357, -4.9209542294846305, -6.567949215360193, -7.657169617254375, -5.552187736426087, -5.123187850814694, -8.722315037727292, -7.529585110971044, -6.769550044389997, -6.652641686902773, -6.946933974372117, -5.19793538671837, -6.01388673645469, -8.594676903618018, -7.115265409118981, -5.4319737052011146, -4.38264104804878, -6.066284962446627, -8.49743488189713, -6.211724027180645, -11.34838572132426, -7.577042980732129, -10.89969640578157},
{-5.108208618417294, -7.032904786755598, -6.723919436018982, -7.519034083907455, -4.627277661101131, -7.157512467151615, -7.946668612066109, -3.609810251525098, -4.6133581635787255, -8.952725768569877, -8.86571632792817, -6.684986047978631, -7.047583770169281, -7.794162653083523, -4.540823405358659, -7.250149118927484, -10.210118965924673, -5.6106133026264144, -5.43161290596506, -5.40837080100069, -6.234550753581678, -8.731287058396575, -6.184763010431074, -11.941960562038894, -6.291410087995217, -9.644906434928023},
{-6.848086388325172, -7.276402112525885, -6.6240911295430775, -7.119335656276422, -6.777065609417157, -8.725941491748348, -6.7966380404982, -9.45271553627741, -7.124507693600807, -10.800476952395538, -9.035800769474049, -6.0521826039206115, -6.892700315070861, -5.648193204195379, -8.802893300737523, -6.702948853384386, -12.127583485066651, -5.51885077274452, -5.618346257488625, -5.654812516388759, -11.13590400453498, -9.922894226006399, -9.41401247599378, -10.30427087742399, -9.003152739239816, -10.244484192154653},
{-6.9582082363654845, -11.905826338650632, -11.201776896930458, -10.829907258220203, -4.993662696795937, -11.944307396668625, -12.037714795527723, -11.871779780589277, -6.133382051229388, -12.843304597157212, -12.840073294402393, -11.387247891601348, -11.72368749700427, -11.78073170996475, -7.559573500779383, -11.143323239331679, -14.882288101373879, -10.711048874836887, -9.96114526547041, -11.137984113260076, -10.862930723174605, -12.173834755738895, -11.459637898055497, -14.119073358197216, -9.828330714801911, -14.311597157270606},
{-5.548280367754852, -9.301291072947256, -9.1740487275862, -9.209801196123204, -5.7928771411597175, -9.461976150097986, -10.338864638296439, -5.899392436116003, -5.649814250123769, -10.680216873423314, -10.276016371770151, -8.790847468344628, -9.053803136970144, -7.077344076223644, -6.1629611723605375, -9.505958623398252, -12.491935868663715, -8.167593287013004, -7.684471023253083, -8.108607373252683, -10.081865095950775, -11.121932303006142, -8.765586291667926, -13.736850369622791, -8.96319373824109, -11.31252826122316},
{-8.472727973978577, -10.735990180396586, -8.731509098371845, -11.183694557446934, -8.79669472936683, -10.55205892041257, -11.60877645630721, -10.16413131047101, -8.347530740362403, -12.916134923233333, -12.665908258763938, -11.192317173242008, -10.431667420252893, -11.732002003459856, -9.927043335034108, -7.761821997834789, -13.590363502174805, -10.779400386916196, -10.240517723371465, -7.959876637987872, -10.285669158214903, -11.841966744149742, -10.497895316629359, -11.722888364591174, -10.732932366260519, -14.546392067592},
{-6.3924069852931344, -7.379905065216284, -7.231554992529104, -7.619447918779117, -6.500270510694652, -7.5367792311234965, -8.324050262758156, -7.542858200492485, -6.876544832500218, -9.34303215727966, -9.308579103141835, -7.671873252949072, -7.449186920286362, -7.976110006352961, -6.1649562108191525, -7.42345452742959, -10.802918456893016, -7.667908806809775, -6.351793843298106, -6.467748370178964, -8.735072772795862, -9.259943609904228, -7.154367010734912, -12.449748232725764, -9.471659632172663, -10.919416120683413},
{-8.445484212448857, -11.354742321196417, -11.565246939009361, -11.78606602582032, -7.835547829730198, -11.924288401490367, -12.007531960019561, -10.600938002195909, -8.811967965331128, -13.310233880772996, -12.09080969743605, -10.897207021900417, -11.450340783883275, -12.09167562330918, -9.229949847415742, -11.865640336958517, -13.526529161705982, -11.792804780720086, -10.725917833109579, -11.237459555432903, -10.54758345513481, -12.616737940051877, -11.047572971415484, -14.378340918646378, -10.617499665927674, -9.880314765929837}};
public static double[][][] trigrams = null;
public static double[][][][] quadgrams = null;
private static int defaultScore = 2;
private static String[] plaintexts = {
"One man's life or death were but a small price to pay for the acquirement of the knowledge which I sought, for the dominion I should acquire and transmit over the elemental foes of our race.",
"The modern masters promise very little; they know that metals cannot be transmuted and that the elixir of life is a chimera but these philosophers, whose hands seem only made to dabble in dirt, and their eyes to pore over the microscope or crucible, have indeed performed miracles. They penetrate into the recesses of nature and show how she works in her hiding-places. They ascend into the heavens; they have discovered how the blood circulates, and the nature of the air we breathe. They have acquired new and almost unlimited powers; they can command the thunders of heaven, mimic the earthquake, and even mock the invisible world with its own shadows.",
"But success SHALL crown my endeavours. Wherefore not? Thus far I have gone, tracing a secure way over the pathless seas, the very stars themselves being witnesses and testimonies of my triumph. Why not still proceed over the untamed yet obedient element? What can stop the determined heart and resolved will of man?",
"Had I right, for my own benefit, to inflict this curse upon everlasting generations? I had before been moved by the sophisms of the being I had created; I had been struck senseless by his fiendish threats; but now, for the first time, the wickedness of my promise burst upon me; I shuddered to think that future ages might curse me as their pest, whose selfishness had not hesitated to buy its own peace at the price, perhaps, of the existence of the whole human race.",
"Gatsby believed in the green light, the orgastic future that year by year recedes before us. It eluded us then, but that's no matterto-morrow we will run faster, stretch out our arms farther....And one fine morning So we beat on, boats against the current, borne back ceaselessly into the past.",
"Power is not a means; it is an end. One does not establish a dictatorship in order to safeguard a revolution; one makes the revolution in order to establish the dictatorship. The object of persecution is persecution. The object of torture is torture. The object of power is power. Now you begin to understand me.",
"Until they become conscious they will never rebel, and until after they have rebelled they cannot become conscious... Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.",
"I leave Sisyphus at the foot of the mountain! One always finds one's burden again. But Sisyphus teaches the higher fidelity that negates the gods and raises rocks. He too concludes that all is well. This universe henceforth without a master seems to him neither sterile nor futile. Each atom of that stone, each mineral flake of that night filled mountain, in itself forms a world. The struggle itself toward the heights is enough to fill a man's heart. One must imagine Sisyphus happy.",
"For, after all, how do we know that two and two make four? Or that the force of gravity works? Or that the past is unchangeable? If both the past and the external world exist only in the mind, and if the mind itself is controllable what then?",
"I perceived in this moment that when the white man turns tyrant it is his own freedom that he destroys. He becomes a sort of hollow, posing dummy, the conventionalized figure of a sahib. For it is the condition of his rule that he shall spend his life in trying to impress the \"natives,\" and so in every crisis he has got to do what the \"natives\" expect of him. He wears a mask, and his face grows to fit it. I had got to shoot the elephant",
"The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brothers keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee.",
"It's incredibly obvious, isn't it? A foreign substance is introduced into our precious bodily fluids, without the knowledge of the individual, certainly without any choice. That's the way a hard-core Commie works.",
};
/**
* Uses the current default scoring system to obtain a score
* @param s the string
* @return the string's score
*/
public static double getScore (String s)
{
if (defaultScore == 2)
return getBiScore(s);
if (defaultScore == 3)
return getTriScore(s);
if (defaultScore == 4)
return getQuadScore(s);
return getBiScore(s);
}
/**
* Uses bigram frequencies
* @param s the string
* @return the string's score
*/
public static double getBiScore (String s)
{
s = s.replaceAll("[^A-Za-z]", "").toLowerCase();
double score = 0;
for (int i = 0; i < s.length() - 1; i++)
score += bigrams[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a'];
return score / (s.length() - 1);
}
/**
* Uses bigram frequencies, and uses spaces as delimiters.
* If the score of 'hi there' is being calculated, the bigrams 'hi' 'th' 'he' 'er' and 're' will be used in the calculation
* @param s
* @return teh string's score
*/
public static double getBiScoreSpaces (String s)
{
s = s.toLowerCase().replaceAll("[^a-z ]", "");
double score = 0, total = 0;
String[] words = s.split(" ");
for (String word : words)
for (int i = 0; i + 1 < word.length(); i++, total++)
score += bigrams[word.charAt(i) - 'a'][word.charAt(i + 1) - 'a'];
return score / total;
}
/**
* Uses trigram frequencies
* @param s the string
* @return the string's score
*/
public static double getTriScore (String s)
{
if (trigrams == null)
loadTrigrams();
s = s.replaceAll("[^A-Za-z]", "").toLowerCase();
double score = 0;
for (int i = 0; i < s.length() - 2; i++)
score += trigrams[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a'][s.charAt(i + 2) - 'a'];
if (Double.isInfinite(score))
return -Double.MAX_VALUE;
return score / (s.length() - 2);
}
public static double getTriScoreSpaces (String s)
{
if (trigrams == null)
loadTrigrams();
s = s.toLowerCase().replaceAll("[^a-z ]", "");
double score = 0, total = 0;
String[] words = s.split(" ");
for (String word : words)
for (int i = 0; i + 2 < word.length(); i++, total++)
score += trigrams[word.charAt(i) - 'a'][word.charAt(i + 1) - 'a'][word.charAt(i + 2) - 'a'];
return score / total;
}
/**
* Uses quadgram frequencies
* @param s the string
* @return the string's score
*/
public static double getQuadScore (String s)
{
if (quadgrams == null)
loadQuadgrams();
s = s.replaceAll("[^A-Za-z]", "").toLowerCase();
double score = 0;
for (int i = 0; i < s.length() - 3; i++)
score += quadgrams[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a'][s.charAt(i + 2) - 'a'][s.charAt(i + 3) - 'a'];
if (Double.isInfinite(score))
return -Double.MAX_VALUE;
return score / (s.length() - 3);
}
public static double getQuadScoreSpaces (String s)
{
if (quadgrams == null)
loadQuadgrams();
s = s.toLowerCase().replaceAll("[^a-z ]", "");
double score = 0, total = 0;
String[] words = s.split(" ");
for (String word : words)
for (int i = 0; i + 3 < word.length(); i++, total++)
score += quadgrams[word.charAt(i) - 'a'][word.charAt(i + 1) - 'a'][word.charAt(i + 2) - 'a'][word.charAt(i + 3) - 'a'];
return score / total;
}
public static double getMonoScore (String s)
{
double score = 0;
s = s.replaceAll("[^A-Za-z]", "").toLowerCase();
for (int i = 0; i < s.length(); i++)
score += monograms[s.charAt(i) - 'a'];
return score / s.length();
}
/**
* Set the default scoring system to use n-grams. Bigrams, trigrams, and quadgrams are supported. Invalid input defaults to bigrams.
* @param n n-gram frequencies to use
*/
public static void setDefaultScoring (int n)
{
defaultScore = n;
}
/**
* Returns the current n for the n-gram frequencies being used
* @return the size of the n-grams being used
*/
public static int getDefaultScoring ()
{
return defaultScore;
}
private static void loadTrigrams ()
{
File f = new File("resources/trigrams.txt");
if (!f.exists())
f = new File("../resources/trigrams.txt");
Scanner scan;
try {
scan = new Scanner(f);
} catch (FileNotFoundException e) {return;}
trigrams = new double[26][26][26];
int i0 = 0, i1 = 0;
while (scan.hasNextLine())
{
String[] in = scan.nextLine().split(", ");
for (int i = 0; i < in.length; i++)
trigrams[i0][i1][i] = Double.parseDouble(in[i]);
if (i1 == 25)
{
i1 = 0;
i0++;
}
else
i1++;
}
}
private static void loadQuadgrams ()
{
File f = new File("resources/quadgrams.txt");
if (!f.exists())
f = new File("../resources/quadgrams.txt");
Scanner scan;
try {
scan = new Scanner(f);
} catch (FileNotFoundException e) {return;}
quadgrams = new double[26][26][26][26];
int i0 = 0, i1 = 0, i2 = 0;
while (scan.hasNextLine())
{
String[] in = scan.nextLine().split(", ");
for (int i = 0; i < in.length; i++)
quadgrams[i0][i1][i2][i] = Double.parseDouble(in[i]);
if (i2 == 25)
{
i2 = 0;
if (i1 == 25)
{
i1 = 0;
i0++;
}
else
i1++;
}
else
i2++;
}
}
public static void loadNGrams ()
{
if (trigrams == null)
loadTrigrams();
if (quadgrams == null)
loadQuadgrams();
}
public static String getPlaintext ()
{
return plaintexts[(int) (Math.random() * plaintexts.length)];
}
public static String getPlaintext (int id)
{
if (id < 0)
return plaintexts[0];
if (id >= plaintexts.length)
return plaintexts[plaintexts.length];
return plaintexts[id];
}
}