summaryrefslogtreecommitdiff
path: root/external/hunspell/0001-Recent-Hunspell-fixes-and-improvements.patch
blob: eb48c283b38ca19ac07a0bd6c10e1bbd34db9fdf (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
From 9ad1696fb13d65e5d569b7106749dd4014877c15 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?L=C3=A1szl=C3=B3=20N=C3=A9meth?= <nemeth@numbertext.org>
Date: Wed, 13 Dec 2017 19:27:30 +0100
Subject: [PATCH] Recent Hunspell fixes and improvements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Containing the following up-stream patches:

commit 7ba5beb517310a942bafd7d6d08fc92beae0e439
Author: László Németh <nemeth@numbertext.org>
Date:   Wed Dec 13 19:01:35 2017 +0100

    Support dictionary based REP replacements

    using the following syntax in the dic file:

    word ph:pattern->replacement

commit 711466a276d5d9f3a5f6e9089bb3262894196fbc
Author: László Németh <nemeth@numbertext.org>
Date:   Tue Dec 12 15:09:36 2017 +0100

    fix compiler warnings

commit db142a3addc87bbbdd9a76bc519c69e8ad95af73
Author: László Németh <nemeth@numbertext.org>
Date:   Fri Dec 1 17:24:17 2017 +0100

    Fix regression in Hungarian "moving rule"

    from commit eb97eb789cec47a172f6e9a01db3e6cf3b8dc81d.

    Dictionary words with COMPOUNDFORBIDFLAG are removed
    from the beginning and middle of compound words,
    overriding the effect of COMPOUNDPERMITFLAG,
    except in Hungarian "moving rule".

    Add test example.

commit 05082b4e8a917cfbddefbc5fd2d543895b27f4c1
Author: László Németh <nemeth@numbertext.org>
Date:   Fri Dec 1 16:11:20 2017 +0100

    BREAK: keep also break-at-first-break-point breaking

    to handle the case of suffixes with dashes in compounds.

    Add also test example.

commit caa24d60f1a4514d4e0ef48fa14105e85eb6514c
Author: László Németh <nemeth@numbertext.org>
Date:   Fri Dec 1 11:16:35 2017 +0100

    Improve ph: usage for capitalization and Unicode

    - at capitalized dictionary words, add lowercase ph: patterns
      to the REP rules in a capitalized form, too, to get correct
      suggestions for lowercase and capitalized mispellings:

      Wednesday ph:wendsay (in dic file) results

      both wendsay and Wendsay -> Wednesday suggestions.

      For German and Hungarian:

      add also lowercase pattern -> lowercase dictionary word
      replacement to the REP rules, supporting lowercasing
      by compound word generation or derivational suffixes.

    - fix UTF-8 support of starred ph: fields

    - test examples

commit 8912f2ade54cdc186fe0580471063d92d99eb572
Author: László Németh <nemeth@numbertext.org>
Date:   Fri Dec 1 10:26:07 2017 +0100

    Allow suggestion search for prefix + *two suffixes*

    Remove artificial performance limit to get correct
    suggestions for relatively simple misspellings in
    Hungarian, etc., when the word form contains prefix
    and both derivative and inflectional suffixes, too:

    lefikszálása -> lefixálása

commit eb97eb789cec47a172f6e9a01db3e6cf3b8dc81d
Author: László Németh <nemeth@numbertext.org>
Date:   Fri Dec 1 08:03:38 2017 +0100

    Dictionary words with COMPOUNDFORBIDFLAG are removed

    from the beginning and middle of compound words,
    overriding the effect of COMPOUNDPERMITFLAG.

commit 526f600e194aacbc3817df26f01d8c95c38bf582
Author: László Németh <nemeth@numbertext.org>
Date:   Wed Nov 29 14:58:46 2017 +0100

    skip empty ph: field and support character stripping

    at replacement rule creation.

    When the ph: field ends with the character *,
    strip last character of the replacement (the correct word)
    and last two character of the field (the * and last
    character of the matching pattern) in the replacement rule.

    For example,

    pretty ph:prity*

    dictionary item results "prit -> prett" REP replacement
    rule instead of "prity -> pretty", to get
    "prity -> pretty" and "pritiest -> prettiest" suggestions.

commit ebdd308463a0e8432f56f12804976ba7029a95c4
Author: László Németh <nemeth@numbertext.org>
Date:   Wed Nov 29 13:13:21 2017 +0100

    clean-up suggestion

    - no ngram and compound word suggestions, if "good" suggestion
      exists, ie. uppercase, REP, ph: or dictionary word pair suggestions

    - word pairs are always suggested, if they exist in the dic file

    - word pairs have top priority in suggestions, and
      these are the only suggestions if there is no other good suggestion.

    - also dictionary word pairs separated by dash instead of space
      are handled specially in two-word suggestion (depending from the
      language)

commit 066704985ae474999345f309c71b4929eff1ca95
Author: László Németh <nemeth@numbertext.org>
Date:   Tue Nov 28 12:55:35 2017 +0100

    check dictionary word pairs to filter compound word overgeneration

    Now it's possible to filter bad compound words by listing
    the correct word pairs with space in the dictionary.

commit bbf2eb4ad0c589c38d03321c8b126826d2284a3f
Author: László Németh <nemeth@numbertext.org>
Date:   Tue Nov 28 11:25:01 2017 +0100

    word pairs of the dic file get highest suggestion priority

    when the words are written without space.

    Instead using REP replacements, now it's enough to add

    a lot

    to the English dic file (like in a traditional spelling
    dictionary) to get suggestions for "alot" in the requested
    order:

    alot
    & alot 7 0: a lot, alto, slot, alt, lot...

    (without using word pairs or the REP replacements, the order was

    alot
    & alot 7 0: alto, slot, alt, lot, a lot...)

commit 90cb55f8f1a21c7f62539baf8f3cf6f062080afd
Author: László Németh <nemeth@numbertext.org>
Date:   Tue Nov 28 09:57:23 2017 +0100

    Clean-up ngram suggestions for lowercase words

    don't suggest capitalized dictionary words for lower
    case misspellings in ngram suggestions, except
     - PHONE usage, or
     - in the case of German, where not only proper
       nouns are capitalized, or
     - the capitalized word has special pronunciation

    - fix typos and comments

commit e80685c83d591b834c61866295577a9e214969cb
Author: László Németh <nemeth@numbertext.org>
Date:   Mon Nov 27 18:26:42 2017 +0100

    Remove SUBSTANDARD dictionary roots from suggestions.

commit 89a8ec6ce47ac4442992f4f6ed606012b1a2b799
Author: László Németh <nemeth@numbertext.org>
Date:   Mon Nov 27 08:52:24 2017 +0100

    Optimize condition order in walk_hashtable loop

commit 4e4106fc64bc26df10f8dc24e0e578abb70025c7
Author: László Németh <nemeth@numbertext.org>
Date:   Sat Nov 25 01:37:52 2017 +0100

    Reduce strange ngram suggestions

    - don't suggest proper names for lowercase
      misspellings, except in German

    - length difference of misspellings and
      suggestions must be less than 5 characters

    Other: search capitalized suggestions for lowercase misspellings
    without ngram suggestions, too.

commit 0b8a4d8851c94485dcc13cf8b8688c8d3fb9a783
Author: László Németh <nemeth@numbertext.org>
Date:   Fri Nov 24 20:01:09 2017 +0100

    Use only middle replentries for compound word checking

    allowing compound filtering for compound stems and affixed
    forms in every languages.

    This replaces the partial fix for the CHECKCOMPOUNDREP regression
    in commit 1fada01663b29b57c010a9c274e45a5cf9ecf222.

commit 957950b792fb0fda8fa95983434be265729bb75b
Author: László Németh <nemeth@numbertext.org>
Date:   Fri Nov 24 10:56:13 2017 +0100

    Spelling dictionary should be a real spelling dictionary

    Listing common misspelling of words and *word sequences*
    is the new recommended method to fix missing, incomplete or
    verbose suggestions. Combined with CHECKCOMPOUNDREP,
    this method can limit overgeneration of compound words
    in important cases, too.

    For example, the following line in the dic file

    a lot ph:alot

    will result the best suggestion ("a lot") for the bad "alot"
    at the first place in the suggestion list.

    Use for:

    - give correct suggestions (wendsay or wensday -> Wednesday)

    Wednesday ph:wendsay ph:wensday

    - set priority of good suggestions (eg. wich -> which, witch, winch)

    which ph:wich
    witch ph:witch

    - suggest with one or *more* spaces (eg. inspite->in spite)

    in spite ph:inspite
    Oh, my gosh! ph:omg

    - switch off ngram suggestions for a common misspelling

    - better suggestion during affixation and compounding

    With CHECKCOMPOUNDREP

    - forbid bad compound words

    Implementation details:

    REP reptable created from REP definitions of the aff file and from
    "ph:" fields of the dic file (reptable contains phonetic and other
    common misspellings of letters, letter groups, morphemes and words
    for better suggestions). REP suggestions have greater priority in
    the suggestion list, and they switch off ngram suggestion
    search, avoiding overgeneration of suggestions.

commit 4a8921bd65b39e24344ef38c396e797384b74677
Author: László Németh <nemeth@numbertext.org>
Date:   Wed Nov 22 23:27:00 2017 +0100

    BREAK tries to break at the second word break

    to recognize dictionary words with word break characters
    (at the beginning of the compound word).

    This fixes the problems with the new Hungarian orthography
    about compounding of words with n-dash.

    Example:

    The Hungarian compound word "e-mail-cím" (e-mail address)
    will break into "e-mail" (dictionary word) and "cím", instead
    of "e" and "mail-cím" ("mail" is not a dictionary word) at
    first level of recursive word breaking.
---
 src/hunspell/affixmgr.cxx   | 183 +++++++++++-----------------------
 src/hunspell/affixmgr.hxx   |   5 +-
 src/hunspell/csutil.hxx     |   6 +-
 src/hunspell/hashmgr.cxx    | 236 +++++++++++++++++++++++++++++++++++++++++---
 src/hunspell/hashmgr.hxx    |  15 ++-
 src/hunspell/htypes.hxx     |   9 +-
 src/hunspell/hunspell.cxx   |  75 ++++++++++----
 src/hunspell/suggestmgr.cxx | 200 ++++++++++++++++++++++++-------------
 src/hunspell/suggestmgr.hxx |   7 +-
 9 files changed, 503 insertions(+), 233 deletions(-)

diff --git a/src/hunspell/affixmgr.cxx b/src/hunspell/affixmgr.cxx
index ffce7bb..a98071a 100644
--- a/src/hunspell/affixmgr.cxx
+++ b/src/hunspell/affixmgr.cxx
@@ -96,7 +96,6 @@ AffixMgr::AffixMgr(const char* affpath,
   complexprefixes = 0;
   parsedmaptable = false;
   parsedbreaktable = false;
-  parsedrep = false;
   iconvtable = NULL;
   oconvtable = NULL;
   // allow simplified compound forms (see 3rd field of CHECKCOMPOUNDPATTERN)
@@ -529,14 +528,6 @@ int AffixMgr::parse_file(const char* affpath, const char* key) {
       }
     }
 
-    /* parse in the typical fault correcting table */
-    if (line.compare(0, 3, "REP", 3) == 0) {
-      if (!parse_reptable(line, afflst)) {
-        finishFileMgr(afflst);
-        return 1;
-      }
-    }
-
     /* parse in the input conversion table */
     if (line.compare(0, 5, "ICONV", 5) == 0) {
       if (!parse_convtable(line, afflst, &iconvtable, "ICONV")) {
@@ -1278,22 +1269,41 @@ std::string AffixMgr::prefix_check_twosfx_morph(const char* word,
 // Is word a non compound with a REP substitution (see checkcompoundrep)?
 int AffixMgr::cpdrep_check(const char* word, int wl) {
 
-  if ((wl < 2) || reptable.empty())
+  if ((wl < 2) || get_reptable().empty())
     return 0;
 
-  for (size_t i = 0; i < reptable.size(); ++i) {
-    const char* r = word;
-    const size_t lenp = reptable[i].pattern.size();
-    // search every occurence of the pattern in the word
-    while ((r = strstr(r, reptable[i].pattern.c_str())) != NULL) {
-      std::string candidate(word);
-      size_t type = r == word && langnum != LANG_hu ? 1 : 0;
-      if (r - word + reptable[i].pattern.size() == lenp && langnum != LANG_hu)
-        type += 2;
-      candidate.replace(r - word, lenp, reptable[i].outstrings[type]);
+  for (size_t i = 0; i < get_reptable().size(); ++i) {
+    // use only available mid patterns
+    if (!get_reptable()[i].outstrings[0].empty()) {
+      const char* r = word;
+      const size_t lenp = get_reptable()[i].pattern.size();
+      // search every occurence of the pattern in the word
+      while ((r = strstr(r, get_reptable()[i].pattern.c_str())) != NULL) {
+        std::string candidate(word);
+        candidate.replace(r - word, lenp, get_reptable()[i].outstrings[0]);
+        if (candidate_check(candidate.c_str(), candidate.size()))
+          return 1;
+        ++r;  // search for the next letter
+      }
+    }
+  }
+
+ return 0;
+}
+
+// forbid compound words, if they are in the dictionary as a
+// word pair separated by space
+int AffixMgr::cpdwordpair_check(const char * word, int wl) {
+  if (wl > 2) {
+    std::string candidate(word);
+    for (size_t i = 1; i < candidate.size(); i++) {
+      // go to end of the UTF-8 character
+      if (utf8 && ((word[i] & 0xc0) == 0x80))
+          continue;
+      candidate.insert(i, 1, ' ');
       if (candidate_check(candidate.c_str(), candidate.size()))
         return 1;
-      ++r;  // search for the next letter
+      candidate.erase(i, 1);
     }
   }
 
@@ -1647,6 +1657,12 @@ struct hentry* AffixMgr::compound_check(const std::string& word,
         affixed = 1;
         rv = lookup(st.c_str());  // perhaps without prefix
 
+        // forbid dictionary stems with COMPOUNDFORBIDFLAG in
+        // compound words, overriding the effect of COMPOUNDPERMITFLAG
+        if ((rv) && compoundforbidflag &&
+                TESTAFF(rv->astr, compoundforbidflag, rv->alen) && !hu_mov_rule)
+            continue;
+
         // search homonym with compound flag
         while ((rv) && !hu_mov_rule &&
                ((needaffix && TESTAFF(rv->astr, needaffix, rv->alen)) ||
@@ -1911,7 +1927,8 @@ struct hentry* AffixMgr::compound_check(const std::string& word,
                  TESTAFF(rv->astr, checkcpdtable[scpd - 1].cond2, rv->alen))) {
               // forbid compound word, if it is a non compound word with typical
               // fault
-              if (checkcompoundrep && cpdrep_check(word.c_str(), len))
+              if ((checkcompoundrep && cpdrep_check(word.c_str(), len)) ||
+                      cpdwordpair_check(word.c_str(), len))
                 return NULL;
               return rv_first;
             }
@@ -2035,7 +2052,8 @@ struct hentry* AffixMgr::compound_check(const std::string& word,
                 ((!checkcompounddup || (rv != rv_first)))) {
               // forbid compound word, if it is a non compound word with typical
               // fault
-              if (checkcompoundrep && cpdrep_check(word.c_str(), len))
+              if ((checkcompoundrep && cpdrep_check(word.c_str(), len)) ||
+                      cpdwordpair_check(word.c_str(), len))
                 return NULL;
               return rv_first;
             }
@@ -2060,7 +2078,11 @@ struct hentry* AffixMgr::compound_check(const std::string& word,
             }
             if (rv) {
               // forbid compound word, if it is a non compound word with typical
-              // fault
+              // fault, or a dictionary word pair
+
+              if (cpdwordpair_check(word.c_str(), len))
+                  return NULL;
+
               if (checkcompoundrep || forbiddenword) {
 
                 if (checkcompoundrep && cpdrep_check(word.c_str(), len))
@@ -2071,7 +2093,8 @@ struct hentry* AffixMgr::compound_check(const std::string& word,
                   char r = st[i + rv->blen];
                   st[i + rv->blen] = '\0';
 
-                  if (checkcompoundrep && cpdrep_check(st.c_str(), i + rv->blen)) {
+                  if ((checkcompoundrep && cpdrep_check(st.c_str(), i + rv->blen)) ||
+                      cpdwordpair_check(st.c_str(), i + rv->blen)) {
                     st[ + i + rv->blen] = r;
                     continue;
                   }
@@ -2198,6 +2221,12 @@ int AffixMgr::compound_check_morph(const char* word,
 
       rv = lookup(st.c_str());  // perhaps without prefix
 
+      // forbid dictionary stems with COMPOUNDFORBIDFLAG in
+      // compound words, overriding the effect of COMPOUNDPERMITFLAG
+      if ((rv) && compoundforbidflag &&
+              TESTAFF(rv->astr, compoundforbidflag, rv->alen) && !hu_mov_rule)
+          continue;
+
       // search homonym with compound flag
       while ((rv) && !hu_mov_rule &&
              ((needaffix && TESTAFF(rv->astr, needaffix, rv->alen)) ||
@@ -3414,7 +3443,7 @@ int AffixMgr::expand_rootword(struct guessword* wlst,
 
 // return replacing table
 const std::vector<replentry>& AffixMgr::get_reptable() const {
-  return reptable;
+  return pHMgr->get_reptable();
 }
 
 // return iconv table
@@ -3554,6 +3583,11 @@ FLAG AffixMgr::get_nongramsuggest() const {
   return nongramsuggest;
 }
 
+// return the substandard root/affix control flag
+FLAG AffixMgr::get_substandard() const {
+  return substandard;
+}
+
 // return the forbidden words flag modify flag
 FLAG AffixMgr::get_needaffix() const {
   return needaffix;
@@ -3692,103 +3726,6 @@ bool AffixMgr::parse_cpdsyllable(const std::string& line, FileMgr* af) {
   return true;
 }
 
-/* parse in the typical fault correcting table */
-bool AffixMgr::parse_reptable(const std::string& line, FileMgr* af) {
-  if (parsedrep) {
-    HUNSPELL_WARNING(stderr, "error: line %d: multiple table definitions\n",
-                     af->getlinenum());
-    return false;
-  }
-  parsedrep = true;
-  int numrep = -1;
-  int i = 0;
-  int np = 0;
-  std::string::const_iterator iter = line.begin();
-  std::string::const_iterator start_piece = mystrsep(line, iter);
-  while (start_piece != line.end()) {
-    switch (i) {
-      case 0: {
-        np++;
-        break;
-      }
-      case 1: {
-        numrep = atoi(std::string(start_piece, iter).c_str());
-        if (numrep < 1) {
-          HUNSPELL_WARNING(stderr, "error: line %d: incorrect entry number\n",
-                           af->getlinenum());
-          return false;
-        }
-        reptable.reserve(numrep);
-        np++;
-        break;
-      }
-      default:
-        break;
-    }
-    ++i;
-    start_piece = mystrsep(line, iter);
-  }
-  if (np != 2) {
-    HUNSPELL_WARNING(stderr, "error: line %d: missing data\n",
-                     af->getlinenum());
-    return false;
-  }
-
-  /* now parse the numrep lines to read in the remainder of the table */
-  for (int j = 0; j < numrep; ++j) {
-    std::string nl;
-    if (!af->getline(nl))
-      return false;
-    mychomp(nl);
-    reptable.push_back(replentry());
-    iter = nl.begin();
-    i = 0;
-    int type = 0;
-    start_piece = mystrsep(nl, iter);
-    while (start_piece != nl.end()) {
-      switch (i) {
-        case 0: {
-          if (nl.compare(start_piece - nl.begin(), 3, "REP", 3) != 0) {
-            HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n",
-                             af->getlinenum());
-            reptable.clear();
-            return false;
-          }
-          break;
-        }
-        case 1: {
-          if (*start_piece == '^')
-            type = 1;
-          reptable.back().pattern.assign(start_piece + type, iter);
-          mystrrep(reptable.back().pattern, "_", " ");
-          if (!reptable.back().pattern.empty() && reptable.back().pattern[reptable.back().pattern.size() - 1] == '$') {
-            type += 2;
-            reptable.back().pattern.resize(reptable.back().pattern.size() - 1);
-          }
-          break;
-        }
-        case 2: {
-          reptable.back().outstrings[type].assign(start_piece, iter);
-          mystrrep(reptable.back().outstrings[type], "_", " ");
-          break;
-        }
-        default:
-          break;
-      }
-      ++i;
-      start_piece = mystrsep(nl, iter);
-    }
-    if (reptable.back().pattern.empty() || reptable.back().outstrings[type].empty()) {
-      HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n",
-                       af->getlinenum());
-      reptable.clear();
-      return false;
-    }
-  }
-  return true;
-}
-
-/* parse in the typical fault correcting table */
 bool AffixMgr::parse_convtable(const std::string& line,
                               FileMgr* af,
                               RepList** rl,
diff --git a/src/hunspell/affixmgr.hxx b/src/hunspell/affixmgr.hxx
index d41e69c..38842a3 100644
--- a/src/hunspell/affixmgr.hxx
+++ b/src/hunspell/affixmgr.hxx
@@ -120,8 +120,6 @@ class AffixMgr {
   FLAG nongramsuggest;
   FLAG needaffix;
   int cpdmin;
-  bool parsedrep;
-  std::vector<replentry> reptable;
   RepList* iconvtable;
   RepList* oconvtable;
   bool parsedmaptable;
@@ -251,6 +249,7 @@ class AffixMgr {
 
   short get_syllable(const std::string& word);
   int cpdrep_check(const char* word, int len);
+  int cpdwordpair_check(const char * word, int len);
   int cpdpat_check(const char* word,
                    int len,
                    hentry* r1,
@@ -311,6 +310,7 @@ class AffixMgr {
   FLAG get_forbiddenword() const;
   FLAG get_nosuggest() const;
   FLAG get_nongramsuggest() const;
+  FLAG get_substandard() const;
   FLAG get_needaffix() const;
   FLAG get_onlyincompound() const;
   const char* get_derived() const;
@@ -338,7 +338,6 @@ class AffixMgr {
   bool parse_flag(const std::string& line, unsigned short* out, FileMgr* af);
   bool parse_num(const std::string& line, int* out, FileMgr* af);
   bool parse_cpdsyllable(const std::string& line, FileMgr* af);
-  bool parse_reptable(const std::string& line, FileMgr* af);
   bool parse_convtable(const std::string& line,
                       FileMgr* af,
                       RepList** rl,
diff --git a/src/hunspell/csutil.hxx b/src/hunspell/csutil.hxx
index 5d83f80..01c0a24 100644
--- a/src/hunspell/csutil.hxx
+++ b/src/hunspell/csutil.hxx
@@ -272,7 +272,7 @@ LIBHUNSPELL_DLL_EXPORTED char* get_stored_pointer(const char* s);
 // hash entry macros
 LIBHUNSPELL_DLL_EXPORTED inline char* HENTRY_DATA(struct hentry* h) {
   char* ret;
-  if (!h->var)
+  if (!(h->var & H_OPT))
     ret = NULL;
   else if (h->var & H_OPT_ALIASM)
     ret = get_stored_pointer(HENTRY_WORD(h) + h->blen + 1);
@@ -284,7 +284,7 @@ LIBHUNSPELL_DLL_EXPORTED inline char* HENTRY_DATA(struct hentry* h) {
 LIBHUNSPELL_DLL_EXPORTED inline const char* HENTRY_DATA(
     const struct hentry* h) {
   const char* ret;
-  if (!h->var)
+  if (!(h->var & H_OPT))
     ret = NULL;
   else if (h->var & H_OPT_ALIASM)
     ret = get_stored_pointer(HENTRY_WORD(h) + h->blen + 1);
@@ -297,7 +297,7 @@ LIBHUNSPELL_DLL_EXPORTED inline const char* HENTRY_DATA(
 LIBHUNSPELL_DLL_EXPORTED inline const char* HENTRY_DATA2(
     const struct hentry* h) {
   const char* ret;
-  if (!h->var)
+  if (!(h->var & H_OPT))
     ret = "";
   else if (h->var & H_OPT_ALIASM)
     ret = get_stored_pointer(HENTRY_WORD(h) + h->blen + 1);
diff --git a/src/hunspell/hashmgr.cxx b/src/hunspell/hashmgr.cxx
index 23421b5..ec3803b 100644
--- a/src/hunspell/hashmgr.cxx
+++ b/src/hunspell/hashmgr.cxx
@@ -78,6 +78,7 @@
 #include "hashmgr.hxx"
 #include "csutil.hxx"
 #include "atypes.hxx"
+#include "langnum.hxx"
 
 // build a hash table from a munched word list
 
@@ -182,7 +183,8 @@ int HashMgr::add_word(const std::string& in_word,
                       unsigned short* aff,
                       int al,
                       const std::string* in_desc,
-                      bool onlyupcase) {
+                      bool onlyupcase,
+                      int captype) {
   const std::string* word = &in_word;
   const std::string* desc = in_desc;
 
@@ -243,20 +245,119 @@ int HashMgr::add_word(const std::string& in_word,
   hp->astr = aff;
   hp->next = NULL;
   hp->next_homonym = NULL;
+  hp->var = (captype == INITCAP) ? H_OPT_INITCAP : 0;
 
   // store the description string or its pointer
   if (desc) {
-    hp->var = H_OPT;
+    hp->var += H_OPT;
     if (aliasm) {
       hp->var += H_OPT_ALIASM;
       store_pointer(hpw + word->size() + 1, get_aliasm(atoi(desc->c_str())));
     } else {
       strcpy(hpw + word->size() + 1, desc->c_str());
     }
-    if (strstr(HENTRY_DATA(hp), MORPH_PHON))
+    if (strstr(HENTRY_DATA(hp), MORPH_PHON)) {
       hp->var += H_OPT_PHON;
-  } else
-    hp->var = 0;
+      // store ph: fields (pronounciation, misspellings, old orthography etc.)
+      // of a morphological description in reptable to use in REP replacements.
+      if (reptable.capacity() < (unsigned int)(tablesize/MORPH_PHON_RATIO))
+          reptable.reserve(tablesize/MORPH_PHON_RATIO);
+      std::string fields = HENTRY_DATA(hp);
+      std::string::const_iterator iter = fields.begin();
+      std::string::const_iterator start_piece = mystrsep(fields, iter);
+      while (start_piece != fields.end()) {
+        if (std::string(start_piece, iter).find(MORPH_PHON) == 0) {
+          std::string ph = std::string(start_piece, iter).substr(sizeof MORPH_PHON - 1);
+          if (ph.size() > 0) {
+            std::vector<w_char> w;
+            size_t strippatt;
+            std::string wordpart;
+            // dictionary based REP replacement, separated by "->"
+            // for example "pretty ph:prity ph:priti->pretti" to handle
+            // both prity -> pretty and pritier -> prettiest suggestions.
+            if (((strippatt = ph.find("->")) != std::string::npos) &&
+                    (strippatt > 0) && (strippatt < ph.size() - 2)) {
+                wordpart = ph.substr(strippatt + 2);
+                ph.erase(ph.begin() + strippatt, ph.end());
+            } else
+                wordpart = in_word;
+            // when the ph: field ends with the character *,
+            // strip last character of the pattern and the replacement
+            // to match in REP suggestions also at character changes,
+            // for example, "pretty ph:prity*" results "prit->prett"
+            // REP replacement instead of "prity->pretty", to get
+            // prity->pretty and pritiest->prettiest suggestions.
+            if (ph.at(ph.size()-1) == '*') {
+              strippatt = 1;
+              size_t stripword = 0;
+              if (utf8) {
+                while ((strippatt < ph.size()) &&
+                  ((ph.at(ph.size()-strippatt-1) & 0xc0) == 0x80))
+                     ++strippatt;
+                while ((stripword < wordpart.size()) &&
+                  ((wordpart.at(wordpart.size()-stripword-1) & 0xc0) == 0x80))
+                     ++stripword;
+              }
+              ++strippatt;
+              ++stripword;
+              if ((ph.size() > strippatt) && (wordpart.size() > stripword)) {
+                ph.erase(ph.size()-strippatt, strippatt);
+                wordpart.erase(in_word.size()-stripword, stripword);
+              }
+            }
+            // capitalize lowercase pattern for capitalized words to support
+            // good suggestions also for capitalized misspellings, eg.
+            // Wednesday ph:wendsay
+            // results wendsay -> Wednesday and Wendsay -> Wednesday, too.
+            if (captype==INITCAP) {
+              std::string ph_capitalized;
+              if (utf8) {
+                u8_u16(w, ph);
+                if (get_captype_utf8(w, langnum) == NOCAP) {
+                  mkinitcap_utf(w, langnum);
+                  u16_u8(ph_capitalized, w);
+                }
+              } else if (get_captype(ph, csconv) == NOCAP)
+                  mkinitcap(ph_capitalized, csconv);
+
+              if (ph_capitalized.size() > 0) {
+                // add also lowercase word in the case of German or
+                // Hungarian to support lowercase suggestions lowercased by
+                // compound word generation or derivational suffixes
+                // (for example by adjectival suffix "-i" of geographical
+                // names in Hungarian:
+                // Massachusetts ph:messzecsuzec
+                // messzecsuzeci -> massachusettsi (adjective)
+                // For lowercasing by conditional PFX rules, see
+                // tests/germancompounding test example or the
+                // Hungarian dictionary.)
+                if (langnum == LANG_de || langnum == LANG_hu) {
+                  std::string wordpart_lower(wordpart);
+                  if (utf8) {
+                    u8_u16(w, wordpart_lower);
+                    mkallsmall_utf(w, langnum);
+                    u16_u8(wordpart_lower, w);
+                  } else {
+                    mkallsmall(wordpart_lower, csconv);
+                  }
+                  reptable.push_back(replentry());
+                  reptable.back().pattern.assign(ph);
+                  reptable.back().outstrings[0].assign(wordpart_lower);
+                }
+                reptable.push_back(replentry());
+                reptable.back().pattern.assign(ph_capitalized);
+                reptable.back().outstrings[0].assign(wordpart);
+              }
+            }
+            reptable.push_back(replentry());
+            reptable.back().pattern.assign(ph);
+            reptable.back().outstrings[0].assign(wordpart);
+          }
+        }
+        start_piece = mystrsep(fields, iter);
+      }
+    }
+  }
 
   struct hentry* dp = tableptr[i];
   if (!dp) {
@@ -347,12 +448,12 @@ int HashMgr::add_hidden_capitalized_word(const std::string& word,
       mkallsmall_utf(w, langnum);
       mkinitcap_utf(w, langnum);
       u16_u8(st, w);
-      return add_word(st, wcl, flags2, flagslen + 1, dp, true);
+      return add_word(st, wcl, flags2, flagslen + 1, dp, true, INITCAP);
     } else {
       std::string new_word(word);
       mkallsmall(new_word, csconv);
       mkinitcap(new_word, csconv);
-      int ret = add_word(new_word, wcl, flags2, flagslen + 1, dp, true);
+      int ret = add_word(new_word, wcl, flags2, flagslen + 1, dp, true, INITCAP);
       return ret;
     }
   }
@@ -435,7 +536,7 @@ int HashMgr::add(const std::string& word) {
     int al = 0;
     unsigned short* flags = NULL;
     int wcl = get_clen_and_captype(word, &captype);
-    add_word(word, wcl, flags, al, NULL, false);
+    add_word(word, wcl, flags, al, NULL, false, captype);
     return add_hidden_capitalized_word(word, wcl, flags, al, NULL,
                                        captype);
   }
@@ -450,14 +551,14 @@ int HashMgr::add_with_affix(const std::string& word, const std::string& example)
     int captype;
     int wcl = get_clen_and_captype(word, &captype);
     if (aliasf) {
-      add_word(word, wcl, dp->astr, dp->alen, NULL, false);
+      add_word(word, wcl, dp->astr, dp->alen, NULL, false, captype);
     } else {
       unsigned short* flags =
           (unsigned short*)malloc(dp->alen * sizeof(unsigned short));
       if (flags) {
         memcpy((void*)flags, (void*)dp->astr,
                dp->alen * sizeof(unsigned short));
-        add_word(word, wcl, flags, dp->alen, NULL, false);
+        add_word(word, wcl, flags, dp->alen, NULL, false, captype);
       } else
         return 1;
     }
@@ -605,7 +706,7 @@ int HashMgr::load_tables(const char* tpath, const char* key) {
     int wcl = get_clen_and_captype(ts, &captype, workbuf);
     const std::string *dp_str = dp.empty() ? NULL : &dp;
     // add the word and its index plus its capitalized form optionally
-    if (add_word(ts, wcl, flags, al, dp_str, false) ||
+    if (add_word(ts, wcl, flags, al, dp_str, false, captype) ||
         add_hidden_capitalized_word(ts, wcl, flags, al, dp_str, captype)) {
       delete dict;
       return 5;
@@ -940,8 +1041,19 @@ int HashMgr::load_config(const char* affpath, const char* key) {
     if (line.compare(0, 15, "COMPLEXPREFIXES", 15) == 0)
       complexprefixes = 1;
 
+    /* parse in the typical fault correcting table */
+    if (line.compare(0, 3, "REP", 3) == 0) {
+      if (!parse_reptable(line, afflst)) {
+        delete afflst;
+        return 1;
+      }
+    }
+
+    // don't check the full affix file, yet
     if (((line.compare(0, 3, "SFX", 3) == 0) ||
-         (line.compare(0, 3, "PFX", 3) == 0)) && line.size() > 3 && isspace(line[3]))
+         (line.compare(0, 3, "PFX", 3) == 0)) &&
+            line.size() > 3 && isspace(line[3]) &&
+            !reptable.empty()) // (REP table is in the end of Afrikaans aff file)
       break;
   }
 
@@ -1191,3 +1303,103 @@ char* HashMgr::get_aliasm(int index) const {
   HUNSPELL_WARNING(stderr, "error: bad morph. alias index: %d\n", index);
   return NULL;
 }
+
+/* parse in the typical fault correcting table */
+bool HashMgr::parse_reptable(const std::string& line, FileMgr* af) {
+  if (!reptable.empty()) {
+    HUNSPELL_WARNING(stderr, "error: line %d: multiple table definitions\n",
+                     af->getlinenum());
+    return false;
+  }
+  int numrep = -1;
+  int i = 0;
+  int np = 0;
+  std::string::const_iterator iter = line.begin();
+  std::string::const_iterator start_piece = mystrsep(line, iter);
+  while (start_piece != line.end()) {
+    switch (i) {
+      case 0: {
+        np++;
+        break;
+      }
+      case 1: {
+        numrep = atoi(std::string(start_piece, iter).c_str());
+        if (numrep < 1) {
+          HUNSPELL_WARNING(stderr, "error: line %d: incorrect entry number\n",
+                           af->getlinenum());
+          return false;
+        }
+        reptable.reserve(numrep);
+        np++;
+        break;
+      }
+      default:
+        break;
+    }
+    ++i;
+    start_piece = mystrsep(line, iter);
+  }
+  if (np != 2) {
+    HUNSPELL_WARNING(stderr, "error: line %d: missing data\n",
+                     af->getlinenum());
+    return false;
+  }
+
+  /* now parse the numrep lines to read in the remainder of the table */
+  for (int j = 0; j < numrep; ++j) {
+    std::string nl;
+    if (!af->getline(nl))
+      return false;
+    mychomp(nl);
+    reptable.push_back(replentry());
+    iter = nl.begin();
+    i = 0;
+    int type = 0;
+    start_piece = mystrsep(nl, iter);
+    while (start_piece != nl.end()) {
+      switch (i) {
+        case 0: {
+          if (nl.compare(start_piece - nl.begin(), 3, "REP", 3) != 0) {
+            HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n",
+                             af->getlinenum());
+            reptable.clear();
+            return false;
+          }
+          break;
+        }
+        case 1: {
+          if (*start_piece == '^')
+            type = 1;
+          reptable.back().pattern.assign(start_piece + type, iter);
+          mystrrep(reptable.back().pattern, "_", " ");
+          if (!reptable.back().pattern.empty() && reptable.back().pattern[reptable.back().pattern.size() - 1] == '$') {
+            type += 2;
+            reptable.back().pattern.resize(reptable.back().pattern.size() - 1);
+          }
+          break;
+        }
+        case 2: {
+          reptable.back().outstrings[type].assign(start_piece, iter);
+          mystrrep(reptable.back().outstrings[type], "_", " ");
+          break;
+        }
+        default:
+          break;
+      }
+      ++i;
+      start_piece = mystrsep(nl, iter);
+    }
+    if (reptable.back().pattern.empty() || reptable.back().outstrings[type].empty()) {
+      HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n",
+                       af->getlinenum());
+      reptable.clear();
+      return false;
+    }
+  }
+  return true;
+}
+
+// return replacing table
+const std::vector<replentry>& HashMgr::get_reptable() const {
+  return reptable;
+}
diff --git a/src/hunspell/hashmgr.hxx b/src/hunspell/hashmgr.hxx
index da485d7..b6eaddd 100644
--- a/src/hunspell/hashmgr.hxx
+++ b/src/hunspell/hashmgr.hxx
@@ -81,6 +81,12 @@
 
 enum flag { FLAG_CHAR, FLAG_LONG, FLAG_NUM, FLAG_UNI };
 
+// morphological description of a dictionary item can contain
+// arbitrary number "ph:" (MORPH_PHON) fields to store typical
+// phonetic or other misspellings of that word.
+// ratio of lines/lines with "ph:" in the dic file: 1/MORPH_PHON_RATIO
+#define MORPH_PHON_RATIO 500
+
 class HashMgr {
   int tablesize;
   struct hentry** tableptr;
@@ -99,6 +105,10 @@ class HashMgr {
   unsigned short* aliasflen;
   int numaliasm;  // morphological desciption `compression' with aliases
   char** aliasm;
+  // reptable created from REP table of aff file and from "ph:" fields
+  // of the dic file. It contains phonetic and other common misspellings
+  // (letters, letter groups and words) for better suggestions
+  std::vector<replentry> reptable;
 
  public:
   HashMgr(const char* tpath, const char* apath, const char* key = NULL);
@@ -119,6 +129,7 @@ class HashMgr {
   int get_aliasf(int index, unsigned short** fvec, FileMgr* af) const;
   int is_aliasm() const;
   char* get_aliasm(int index) const;
+  const std::vector<replentry>& get_reptable() const;
 
  private:
   int get_clen_and_captype(const std::string& word, int* captype);
@@ -129,7 +140,8 @@ class HashMgr {
                unsigned short* ap,
                int al,
                const std::string* desc,
-               bool onlyupcase);
+               bool onlyupcase,
+               int captype);
   int load_config(const char* affpath, const char* key);
   bool parse_aliasf(const std::string& line, FileMgr* af);
   int add_hidden_capitalized_word(const std::string& word,
@@ -139,6 +151,7 @@ class HashMgr {
                                   const std::string* dp,
                                   int captype);
   bool parse_aliasm(const std::string& line, FileMgr* af);
+  bool parse_reptable(const std::string& line, FileMgr* af);
   int remove_forbidden_flag(const std::string& word);
 };
 
diff --git a/src/hunspell/htypes.hxx b/src/hunspell/htypes.hxx
index 8f66a00..76228c4 100644
--- a/src/hunspell/htypes.hxx
+++ b/src/hunspell/htypes.hxx
@@ -44,9 +44,10 @@
   (v) = ((v) << (q)) | (((v) >> (32 - q)) & ((1 << (q)) - 1));
 
 // hentry options
-#define H_OPT (1 << 0)
-#define H_OPT_ALIASM (1 << 1)
-#define H_OPT_PHON (1 << 2)
+#define H_OPT (1 << 0)          // is there optional morphological data?
+#define H_OPT_ALIASM (1 << 1)   // using alias compression?
+#define H_OPT_PHON (1 << 2)     // is there ph: field in the morphological data?
+#define H_OPT_INITCAP (1 << 3)  // is dictionary word capitalized?
 
 // see also csutil.hxx
 #define HENTRY_WORD(h) &(h->word[0])
@@ -61,7 +62,7 @@ struct hentry {
   unsigned short* astr;  // affix flag vector
   struct hentry* next;   // next word with same hash code
   struct hentry* next_homonym;  // next homonym word (with same hash code)
-  char var;      // variable fields (only for special pronounciation yet)
+  char var;      // bit vector of H_OPT hentry options
   char word[1];  // variable-length word (8-bit or UTF-8 encoding)
 };
 
diff --git a/src/hunspell/hunspell.cxx b/src/hunspell/hunspell.cxx
index 1ef11df..6c5aeb6 100644
--- a/src/hunspell/hunspell.cxx
+++ b/src/hunspell/hunspell.cxx
@@ -666,6 +666,37 @@ bool HunspellImpl::spell(const std::string& word, int* info, std::string* root)
       size_t plen = wordbreak[j].size();
       size_t found = scw.find(wordbreak[j]);
       if ((found > 0) && (found < wl - plen)) {
+        size_t found2 = scw.find(wordbreak[j], found + 1);
+        // try to break at the second occurance
+        // to recognize dictionary words with wordbreak
+        if (found2 > 0 && (found2 < wl - plen))
+            found = found2;
+        if (!spell(scw.substr(found + plen)))
+          continue;
+        std::string suffix(scw.substr(found));
+        scw.resize(found);
+        // examine 2 sides of the break point
+        if (spell(scw))
+          return true;
+        scw.append(suffix);
+
+        // LANG_hu: spec. dash rule
+        if (langnum == LANG_hu && wordbreak[j] == "-") {
+          suffix = scw.substr(found + 1);
+          scw.resize(found + 1);
+          if (spell(scw))
+            return true;  // check the first part with dash
+          scw.append(suffix);
+        }
+        // end of LANG specific region
+      }
+    }
+
+    // other patterns (break at first break point)
+    for (size_t j = 0; j < wordbreak.size(); ++j) {
+      size_t plen = wordbreak[j].size();
+      size_t found = scw.find(wordbreak[j]);
+      if ((found > 0) && (found < wl - plen)) {
         if (!spell(scw.substr(found + plen)))
           continue;
         std::string suffix(scw.substr(found));
@@ -870,6 +901,7 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
   }
 
   int capwords = 0;
+  bool good = false;
 
   // check capitalized form for FORCEUCASE
   if (pAMgr && captype == NOCAP && pAMgr->get_forceucase()) {
@@ -884,22 +916,27 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
 
   switch (captype) {
     case NOCAP: {
-      pSMgr->suggest(slst, scw.c_str(), &onlycmpdsug);
+      good |= pSMgr->suggest(slst, scw.c_str(), &onlycmpdsug);
+      if (abbv) {
+        std::string wspace(scw);
+        wspace.push_back('.');
+        good |= pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
+      }
       break;
     }
 
     case INITCAP: {
       capwords = 1;
-      pSMgr->suggest(slst, scw.c_str(), &onlycmpdsug);
+      good |= pSMgr->suggest(slst, scw.c_str(), &onlycmpdsug);
       std::string wspace(scw);
       mkallsmall2(wspace, sunicw);
-      pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
+      good |= pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
       break;
     }
     case HUHINITCAP:
       capwords = 1;
     case HUHCAP: {
-      pSMgr->suggest(slst, scw.c_str(), &onlycmpdsug);
+      good |= pSMgr->suggest(slst, scw.c_str(), &onlycmpdsug);
       // something.The -> something. The
       size_t dot_pos = scw.find('.');
       if (dot_pos != std::string::npos) {
@@ -925,19 +962,19 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
         // TheOpenOffice.org -> The OpenOffice.org
         wspace = scw;
         mkinitsmall2(wspace, sunicw);
-        pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
+        good |= pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
       }
       wspace = scw;
       mkallsmall2(wspace, sunicw);
       if (spell(wspace.c_str()))
         insert_sug(slst, wspace);
       size_t prevns = slst.size();
-      pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
+      good |= pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
       if (captype == HUHINITCAP) {
         mkinitcap2(wspace, sunicw);
         if (spell(wspace.c_str()))
           insert_sug(slst, wspace);
-        pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
+        good |= pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
       }
       // aNew -> "a New" (instead of "a new")
       for (size_t j = prevns; j < slst.size(); ++j) {
@@ -964,11 +1001,11 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
     case ALLCAP: {
       std::string wspace(scw);
       mkallsmall2(wspace, sunicw);
-      pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
+      good |= pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
       if (pAMgr && pAMgr->get_keepcase() && spell(wspace.c_str()))
         insert_sug(slst, wspace);
       mkinitcap2(wspace, sunicw);
-      pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
+      good |= pSMgr->suggest(slst, wspace.c_str(), &onlycmpdsug);
       for (size_t j = 0; j < slst.size(); ++j) {
         mkallcap(slst[j]);
         if (pAMgr && pAMgr->get_checksharps()) {
@@ -1000,12 +1037,11 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
     }
   }
   // END OF LANG_hu section
-
-  // try ngram approach since found nothing or only compound words
-  if (pAMgr && (slst.empty() || onlycmpdsug) && (pAMgr->get_maxngramsugs() != 0)) {
+  // try ngram approach since found nothing good suggestion
+  if (!good && pAMgr && (slst.empty() || onlycmpdsug) && (pAMgr->get_maxngramsugs() != 0)) {
     switch (captype) {
       case NOCAP: {
-        pSMgr->ngsuggest(slst, scw.c_str(), m_HMgrs);
+        pSMgr->ngsuggest(slst, scw.c_str(), m_HMgrs, NOCAP);
         break;
       }
       case HUHINITCAP:
@@ -1013,21 +1049,21 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
       case HUHCAP: {
         std::string wspace(scw);
         mkallsmall2(wspace, sunicw);
-        pSMgr->ngsuggest(slst, wspace.c_str(), m_HMgrs);
+        pSMgr->ngsuggest(slst, wspace.c_str(), m_HMgrs, HUHCAP);
         break;
       }
       case INITCAP: {
         capwords = 1;
         std::string wspace(scw);
         mkallsmall2(wspace, sunicw);
-        pSMgr->ngsuggest(slst, wspace.c_str(), m_HMgrs);
+        pSMgr->ngsuggest(slst, wspace.c_str(), m_HMgrs, INITCAP);
         break;
       }
       case ALLCAP: {
         std::string wspace(scw);
         mkallsmall2(wspace, sunicw);
         size_t oldns = slst.size();
-        pSMgr->ngsuggest(slst, wspace.c_str(), m_HMgrs);
+        pSMgr->ngsuggest(slst, wspace.c_str(), m_HMgrs, ALLCAP);
         for (size_t j = oldns; j < slst.size(); ++j) {
           mkallcap(slst[j]);
         }
@@ -1037,6 +1073,11 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
   }
 
   // try dash suggestion (Afo-American -> Afro-American)
+  // Note: LibreOffice was modified to treat dashes as word
+  // characters to check "scot-free" etc. word forms, but
+  // we need to handle suggestions for "Afo-American", etc.,
+  // while "Afro-American" is missing from the dictionary.
+  // TODO avoid possible overgeneration
   size_t dash_pos = scw.find('-');
   if (dash_pos != std::string::npos) {
     int nodashsug = 1;
@@ -1048,7 +1089,7 @@ std::vector<std::string> HunspellImpl::suggest(const std::string& word) {
     size_t prev_pos = 0;
     bool last = false;
 
-    while (nodashsug && !last) {
+    while (!good && nodashsug && !last) {
       if (dash_pos == scw.size())
         last = 1;
       std::string chunk = scw.substr(prev_pos, dash_pos - prev_pos);
diff --git a/src/hunspell/suggestmgr.cxx b/src/hunspell/suggestmgr.cxx
index 73ea91e..ade85af 100644
--- a/src/hunspell/suggestmgr.cxx
+++ b/src/hunspell/suggestmgr.cxx
@@ -132,6 +132,11 @@ SuggestMgr::SuggestMgr(const char* tryme, unsigned int maxn, AffixMgr* aptr) {
       ctryl = u8_u16(ctry_utf, tryme);
     }
   }
+
+  // language with possible dash usage
+  // (latin letters or dash in TRY characters)
+  lang_with_dash_usage = (ctry &&
+      ((strchr(ctry, '-') != NULL) || (strchr(ctry, 'a') != NULL)));
 }
 
 SuggestMgr::~SuggestMgr() {
@@ -169,10 +174,13 @@ void SuggestMgr::testsug(std::vector<std::string>& wlst,
   }
 }
 
-// generate suggestions for a misspelled word
-//    pass in address of array of char * pointers
-// onlycompoundsug: probably bad suggestions (need for ngram sugs, too)
-void SuggestMgr::suggest(std::vector<std::string>& slst,
+/* generate suggestions for a misspelled word
+ *    pass in address of array of char * pointers
+ * onlycompoundsug: probably bad suggestions (need for ngram sugs, too)
+ * return value: true, if there is a good suggestion
+ * (REP, ph: or a dictionary word pair)
+ */
+bool SuggestMgr::suggest(std::vector<std::string>& slst,
                         const char* w,
                         int* onlycompoundsug) {
   int nocompoundtwowords = 0;
@@ -182,6 +190,7 @@ void SuggestMgr::suggest(std::vector<std::string>& slst,
   std::string w2;
   const char* word = w;
   size_t oldSug = 0;
+  bool good_suggestion = false;
 
   // word reversing wrapper for complex prefixes
   if (complexprefixes) {
@@ -196,11 +205,11 @@ void SuggestMgr::suggest(std::vector<std::string>& slst,
   if (utf8) {
     wl = u8_u16(word_utf, word);
     if (wl == -1) {
-      return;
+      return false;
     }
   }
 
-  for (int cpdsuggest = 0; (cpdsuggest < 2) && (nocompoundtwowords == 0);
+  for (int cpdsuggest = 0; (cpdsuggest < 2) && (nocompoundtwowords == 0) && !good_suggestion;
        cpdsuggest++) {
     // limit compound suggestion
     if (cpdsuggest > 0)
@@ -208,15 +217,21 @@ void SuggestMgr::suggest(std::vector<std::string>& slst,
 
     // suggestions for an uppercase word (html -> HTML)
     if (slst.size() < maxSug) {
+      size_t i = slst.size();
       if (utf8)
         capchars_utf(slst, &word_utf[0], wl, cpdsuggest);
       else
         capchars(slst, word, cpdsuggest);
+      if (slst.size() > i)
+        good_suggestion = true;
     }
 
     // perhaps we made a typical fault of spelling
     if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
+      size_t i = slst.size();
       replchars(slst, word, cpdsuggest);
+      if (slst.size() > i)
+        good_suggestion = true;
     }
 
     // perhaps we made chose the wrong char from a related set
@@ -294,15 +309,19 @@ void SuggestMgr::suggest(std::vector<std::string>& slst,
     }
 
     // perhaps we forgot to hit space and two words ran together
-    if (!nosplitsugs && (slst.size() < maxSug) &&
-        (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
-      twowords(slst, word, cpdsuggest);
+    // (dictionary word pairs have top priority here, so
+    // we always suggest them, in despite of nosplitsugs, and
+    // drop compound word and other suggestions)
+    if (!cpdsuggest || (!nosplitsugs && slst.size() < oldSug + maxcpdsugs)) {
+      good_suggestion = twowords(slst, word, cpdsuggest, good_suggestion);
     }
 
   }  // repeating ``for'' statement compounding support
 
   if (!nocompoundtwowords && (!slst.empty()) && onlycompoundsug)
     *onlycompoundsug = 1;
+
+  return good_suggestion;
 }
 
 // suggestions for an uppercase word (html -> HTML)
@@ -721,17 +740,22 @@ int SuggestMgr::forgotchar_utf(std::vector<std::string>& wlst,
   return wlst.size();
 }
 
-/* error is should have been two words */
-int SuggestMgr::twowords(std::vector<std::string>& wlst,
+/* error is should have been two words
+ * return value is true, if there is a dictionary word pair,
+ * or there was already a good suggestion before calling
+ * this function.
+ */
+bool SuggestMgr::twowords(std::vector<std::string>& wlst,
                          const char* word,
-                         int cpdsuggest) {
+                         int cpdsuggest,
+                         bool good) {
   int c2;
   int forbidden = 0;
   int cwrd;
 
   int wl = strlen(word);
   if (wl < 3)
-    return wlst.size();
+    return false;
 
   if (langnum == LANG_hu)
     forbidden = check_forbidden(word, wl);
@@ -750,63 +774,87 @@ int SuggestMgr::twowords(std::vector<std::string>& wlst,
     }
     if (utf8 && p[1] == '\0')
       break;  // last UTF-8 character
-    *p = '\0';
-    int c1 = checkword(candidate, cpdsuggest, NULL, NULL);
-    if (c1) {
-      c2 = checkword((p + 1), cpdsuggest, NULL, NULL);
-      if (c2) {
-        *p = ' ';
-
-        // spec. Hungarian code (need a better compound word support)
-        if ((langnum == LANG_hu) && !forbidden &&
-            // if 3 repeating letter, use - instead of space
-            (((p[-1] == p[1]) &&
-              (((p > candidate + 1) && (p[-1] == p[-2])) || (p[-1] == p[2]))) ||
-             // or multiple compounding, with more, than 6 syllables
-             ((c1 == 3) && (c2 >= 2))))
-          *p = '-';
-
-        cwrd = 1;
-        for (size_t k = 0; k < wlst.size(); ++k) {
-          if (wlst[k] == candidate) {
-            cwrd = 0;
-            break;
-          }
-        }
-        if (wlst.size() < maxSug) {
-          if (cwrd) {
-            wlst.push_back(candidate);
-          }
-        } else {
-          free(candidate);
-          return wlst.size();
+
+    // Suggest only word pairs, if they are listed in the dictionary.
+    // For example, adding "a lot" to the English dic file will
+    // result only "alot" -> "a lot" suggestion instead of
+    // "alto, slot, alt, lot, allot, aloft, aloe, clot, plot, blot, a lot".
+    // Note: using "ph:alot" keeps the other suggestions:
+    // a lot ph:alot
+    // alot -> a lot, alto, slot...
+    *p = ' ';
+    if (!cpdsuggest && checkword(candidate, cpdsuggest, NULL, NULL)) {
+      // remove not word pair suggestions
+      if (!good) {
+        good = true;
+        wlst.clear();
+      }
+      wlst.insert(wlst.begin(), candidate);
+    }
+
+    // word pairs with dash?
+    if (lang_with_dash_usage) {
+      *p = '-';
+
+      if (!cpdsuggest && checkword(candidate, cpdsuggest, NULL, NULL)) {
+        // remove not word pair suggestions
+        if (!good) {
+          good = true;
+          wlst.clear();
         }
-        // add two word suggestion with dash, if TRY string contains
-        // "a" or "-"
-        // NOTE: cwrd doesn't modified for REP twoword sugg.
-        if (ctry && (strchr(ctry, 'a') || strchr(ctry, '-')) &&
-            mystrlen(p + 1) > 1 && mystrlen(candidate) - mystrlen(p) > 1) {
-          *p = '-';
+        wlst.insert(wlst.begin(), candidate);
+      }
+    }
+
+    if (wlst.size() < maxSug && !nosplitsugs && !good) {
+      *p = '\0';
+      int c1 = checkword(candidate, cpdsuggest, NULL, NULL);
+      if (c1) {
+        c2 = checkword((p + 1), cpdsuggest, NULL, NULL);
+        if (c2) {
+          // spec. Hungarian code (TODO need a better compound word support)
+          if ((langnum == LANG_hu) && !forbidden &&
+              // if 3 repeating letter, use - instead of space
+              (((p[-1] == p[1]) &&
+              (((p > candidate + 1) && (p[-1] == p[-2])) || (p[-1] == p[2]))) ||
+              // or multiple compounding, with more, than 6 syllables
+              ((c1 == 3) && (c2 >= 2))))
+            *p = '-';
+          else
+            *p = ' ';
+
+          cwrd = 1;
           for (size_t k = 0; k < wlst.size(); ++k) {
             if (wlst[k] == candidate) {
               cwrd = 0;
               break;
             }
           }
-          if (wlst.size() < maxSug) {
-            if (cwrd) {
+
+          if (cwrd && (wlst.size() < maxSug))
               wlst.push_back(candidate);
+
+          // add two word suggestion with dash, depending on the language
+          // Note that cwrd doesn't modified for REP twoword sugg.
+          if ( !nosplitsugs && lang_with_dash_usage &&
+              mystrlen(p + 1) > 1 && mystrlen(candidate) - mystrlen(p) > 1) {
+            *p = '-';
+            for (size_t k = 0; k < wlst.size(); ++k) {
+              if (wlst[k] == candidate) {
+                cwrd = 0;
+                break;
+              }
             }
-          } else {
-            free(candidate);
-            return wlst.size();
+
+            if ((wlst.size() < maxSug) && cwrd)
+              wlst.push_back(candidate);
           }
         }
       }
     }
   }
   free(candidate);
-  return wlst.size();
+  return good;
 }
 
 // error is adjacent letter were swapped
@@ -994,7 +1042,8 @@ int SuggestMgr::movechar_utf(std::vector<std::string>& wlst,
 // generate a set of suggestions for very poorly spelled words
 void SuggestMgr::ngsuggest(std::vector<std::string>& wlst,
                           const char* w,
-                          const std::vector<HashMgr*>& rHMgr) {
+                          const std::vector<HashMgr*>& rHMgr,
+                          int captype) {
   int lval;
   int sc;
   int lp, lpphon;
@@ -1071,18 +1120,34 @@ void SuggestMgr::ngsuggest(std::vector<std::string>& wlst,
     u8_u16(w_word, word);
     u8_u16(w_target, target);
   }
-  
+
   std::string f;
   std::vector<w_char> w_f;
-  
+
   for (size_t i = 0; i < rHMgr.size(); ++i) {
     while (0 != (hp = rHMgr[i]->walk_hashtable(col, hp))) {
-      if ((hp->astr) && (pAMgr) &&
-          (TESTAFF(hp->astr, forbiddenword, hp->alen) ||
-           TESTAFF(hp->astr, ONLYUPCASEFLAG, hp->alen) ||
-           TESTAFF(hp->astr, nosuggest, hp->alen) ||
-           TESTAFF(hp->astr, nongramsuggest, hp->alen) ||
-           TESTAFF(hp->astr, onlyincompound, hp->alen)))
+      // skip exceptions
+      if (
+           // skip it, if the word length different by 5 or
+           // more characters (to avoid strange suggestions)
+           // (except Unicode characters over BMP)
+           (((abs(n - hp->clen) > 4) && !nonbmp)) ||
+           // don't suggest capitalized dictionary words for
+           // lower case misspellings in ngram suggestions, except
+           // - PHONE usage, or
+           // - in the case of German, where not only proper
+           //   nouns are capitalized, or
+           // - the capitalized word has special pronunciation
+           ((captype == NOCAP) && (hp->var & H_OPT_INITCAP) &&
+              !ph && (langnum != LANG_de) && !(hp->var & H_OPT_PHON)) ||
+           // or it has one of the following special flags
+           ((hp->astr) && (pAMgr) &&
+             (TESTAFF(hp->astr, forbiddenword, hp->alen) ||
+             TESTAFF(hp->astr, ONLYUPCASEFLAG, hp->alen) ||
+             TESTAFF(hp->astr, nosuggest, hp->alen) ||
+             TESTAFF(hp->astr, nongramsuggest, hp->alen) ||
+             TESTAFF(hp->astr, onlyincompound, hp->alen)))
+         )
         continue;
 
       if (utf8) {
@@ -1105,7 +1170,7 @@ void SuggestMgr::ngsuggest(std::vector<std::string>& wlst,
         sc = ngram(3, word, f, NGRAM_LONGER_WORSE) + leftcommon;
       }
 
-      // check special pronounciation
+      // check special pronunciation
       f.clear();
       if ((hp->var & H_OPT_PHON) &&
           copy_field(f, HENTRY_DATA(hp), MORPH_PHON)) {
@@ -1559,7 +1624,8 @@ int SuggestMgr::checkword(const std::string& word,
     if (rv) {
       if ((rv->astr) &&
           (TESTAFF(rv->astr, pAMgr->get_forbiddenword(), rv->alen) ||
-           TESTAFF(rv->astr, pAMgr->get_nosuggest(), rv->alen)))
+           TESTAFF(rv->astr, pAMgr->get_nosuggest(), rv->alen) ||
+           TESTAFF(rv->astr, pAMgr->get_substandard(), rv->alen)))
         return 0;
       while (rv) {
         if (rv->astr &&
@@ -1584,7 +1650,7 @@ int SuggestMgr::checkword(const std::string& word,
     if (!rv && pAMgr->have_contclass()) {
       rv = pAMgr->suffix_check_twosfx(word.c_str(), word.size(), 0, NULL, FLAG_NULL);
       if (!rv)
-        rv = pAMgr->prefix_check_twosfx(word.c_str(), word.size(), 1, FLAG_NULL);
+        rv = pAMgr->prefix_check_twosfx(word.c_str(), word.size(), 0, FLAG_NULL);
     }
 
     // check forbidden words
diff --git a/src/hunspell/suggestmgr.hxx b/src/hunspell/suggestmgr.hxx
index 19ffc03..f0daf23 100644
--- a/src/hunspell/suggestmgr.hxx
+++ b/src/hunspell/suggestmgr.hxx
@@ -109,6 +109,7 @@ class SuggestMgr {
   char* ctry;
   size_t ctryl;
   std::vector<w_char> ctry_utf;
+  bool lang_with_dash_usage;
 
   AffixMgr* pAMgr;
   unsigned int maxSug;
@@ -124,8 +125,8 @@ class SuggestMgr {
   SuggestMgr(const char* tryme, unsigned int maxn, AffixMgr* aptr);
   ~SuggestMgr();
 
-  void suggest(std::vector<std::string>& slst, const char* word, int* onlycmpdsug);
-  void ngsuggest(std::vector<std::string>& slst, const char* word, const std::vector<HashMgr*>& rHMgr);
+  bool suggest(std::vector<std::string>& slst, const char* word, int* onlycmpdsug);
+  void ngsuggest(std::vector<std::string>& slst, const char* word, const std::vector<HashMgr*>& rHMgr, int captype);
 
   std::string suggest_morph(const std::string& word);
   std::string suggest_gen(const std::vector<std::string>& pl, const std::string& pattern);
@@ -149,7 +150,7 @@ class SuggestMgr {
   int extrachar(std::vector<std::string>&, const char*, int);
   int badcharkey(std::vector<std::string>&, const char*, int);
   int badchar(std::vector<std::string>&, const char*, int);
-  int twowords(std::vector<std::string>&, const char*, int);
+  bool twowords(std::vector<std::string>&, const char*, int, bool);
 
   void capchars_utf(std::vector<std::string>&, const w_char*, int wl, int);
   int doubletwochars_utf(std::vector<std::string>&, const w_char*, int wl, int);
-- 
2.7.4