autoText/cpp/Context.cpp
2023-12-10 07:20:40 +01:00

63 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <ctype.h>
#include "Context.hpp"
using namespace std;
// ========================================
const int Context::prevSize (16);
const string Context::end (".?!");
const string Context::sep (" ,;-");
int
Context::getPrevSize () {
return prevSize;
}
bool
Context::validChar (const char &c) {
if (isalpha (c))
return true;
return
end.find (c) != string::npos ||
sep.find (c) != string::npos;
}
bool
Context::isEnd (const string &c) {
return end.find (c) != string::npos;
}
bool
Context::isSep (const string &c) {
return sep.find (c) != string::npos;
}
void
Context::forward (string current) {
if (!prevSize)
return;
if (current == " " && prev.empty ())
return;
if (end.find (current) != string::npos) {
prev.clear ();
return;
}
prev += current;
if (prev.size () > prevSize) {
int count (prevSize);
for (auto rit (prev.crbegin ()); rit != prev.crend (); ++rit)
// (ASCII || start UTF) && countdown
if ((! (*rit & 0b10000000) || (*rit & 0b01000000)) && ! --count) {
prev = prev.substr (prev.crend () - rit -1);
break;
}
}
}
// ========================================