@@ -879,6 +879,39 @@ RunState Suggest::run_json(std::istream& is, std::ostream& os)
879879 return sentence.runstate ;
880880}
881881
882+ RunState Suggest::run_autocorrect (std::istream& is, std::ostream& os)
883+ {
884+ json::sanity_test ();
885+ Sentence sentence = run_sentence (is, *generator, msgs, generate_all_readings);
886+ vector<Err> errs = mk_errs (sentence);
887+
888+ size_t offset = 0 ;
889+ u16string text = fromUtf8 (sentence.text .str ());
890+ for (const auto & e : errs) {
891+ if (e.beg > offset) {
892+ os << toUtf8 (text.substr (offset, e.beg - offset));
893+ }
894+ bool printed = false ;
895+ for (const auto & r : e.rep ) {
896+ os << toUtf8 (r);
897+ printed = true ;
898+ break ;
899+ }
900+ if (!printed) {
901+ os << toUtf8 (e.form );
902+ }
903+ offset = e.end ;
904+ }
905+ os << toUtf8 (text.substr (offset));
906+
907+ if (sentence.runstate == flushing) {
908+ os << ' \0 ' ;
909+ os.flush ();
910+ os.clear ();
911+ }
912+ return sentence.runstate ;
913+ }
914+
882915
883916void print_cg_reading (const Casing& inputCasing, const string& readinglines, std::ostream& os, const hfst::HfstTransducer& t, bool generate_all_readings) {
884917 os << readinglines;
@@ -938,7 +971,7 @@ void run_cg(std::istream& is, std::ostream& os, const hfst::HfstTransducer& t, b
938971 }
939972}
940973
941- void Suggest::run (std::istream& is, std::ostream& os, bool json )
974+ void Suggest::run (std::istream& is, std::ostream& os, RunMode mode )
942975{
943976 try {
944977 auto _old = std::locale::global (std::locale (" " ));
@@ -947,11 +980,17 @@ void Suggest::run(std::istream& is, std::ostream& os, bool json)
947980 {
948981 std::cerr << " WARNING: Couldn't set global locale \"\" (locale-specific native environment): " << e.what () << std::endl;
949982 }
950- if (json) {
951- while (run_json (is, os) == flushing);
952- }
953- else {
954- run_cg (is, os, *generator, generate_all_readings); // ignores ignores
983+ switch (mode) {
984+ case RunJson:
985+ while (run_json (is, os) == flushing);
986+ break ;
987+ case RunAutoCorrect:
988+ while (run_autocorrect (is, os) == flushing);
989+ break ;
990+ case RunCG:
991+ default :
992+ run_cg (is, os, *generator, generate_all_readings); // ignores ignores
993+ break ;
955994 }
956995}
957996
0 commit comments