add repetition filter and refactor host

This commit is contained in:
Akash Mozumdar 2018-08-27 20:49:33 -04:00
parent e92d260b31
commit 4660be2efb
3 changed files with 11 additions and 13 deletions

View File

@ -119,6 +119,8 @@ namespace
default: default:
{ {
ThreadParam tp = *(ThreadParam*)buffer; ThreadParam tp = *(ThreadParam*)buffer;
buffer[bytesRead] = 0;
buffer[bytesRead + 1] = 0;
DispatchText(tp, buffer + sizeof(tp), bytesRead - sizeof(tp)); DispatchText(tp, buffer + sizeof(tp), bytesRead - sizeof(tp));
} }
break; break;

View File

@ -5,14 +5,7 @@
#include "textthread.h" #include "textthread.h"
#include "const.h" #include "const.h"
TextThread::TextThread(ThreadParam tp, DWORD status) : TextThread::TextThread(ThreadParam tp, DWORD status) : tp(tp), status(status) {}
deletionEvent(CreateEventW(nullptr, FALSE, FALSE, NULL)),
flushThread([&]() { while (WaitForSingleObject(deletionEvent, 100) == WAIT_TIMEOUT) Flush(); }),
timestamp(GetTickCount()),
Output(nullptr),
tp(tp),
status(status)
{}
TextThread::~TextThread() TextThread::~TextThread()
{ {
@ -30,7 +23,7 @@ std::wstring TextThread::GetStore()
void TextThread::Flush() void TextThread::Flush()
{ {
LOCK ttLock(ttMutex); LOCK ttLock(ttMutex);
if (timestamp - GetTickCount() < 250 || buffer.size() == 0) return; // TODO: let user change delay before sentence is flushed if (buffer.size() < 400 && (timestamp - GetTickCount() < 250 || buffer.size() == 0)) return; // TODO: let user change delay before sentence is flushed
std::wstring sentence; std::wstring sentence;
if (status & USING_UNICODE) if (status & USING_UNICODE)
{ {
@ -43,6 +36,7 @@ void TextThread::Flush()
delete[] converted; delete[] converted;
} }
AddSentence(sentence); AddSentence(sentence);
memset(buffer.data(), 0, buffer.size());
buffer.clear(); buffer.clear();
} }
@ -56,6 +50,8 @@ void TextThread::AddSentence(std::wstring sentence)
void TextThread::AddText(const BYTE *con, int len) void TextThread::AddText(const BYTE *con, int len)
{ {
LOCK ttLock(ttMutex); LOCK ttLock(ttMutex);
// Artikash 8/27/2018: add repetition filter
if (len > 6 && buffer.data() && (strstr(buffer.data(), (const char*)con) || wcsstr((const wchar_t*)buffer.data(), (const wchar_t*)con))) return;
buffer.insert(buffer.end(), con, con + len); buffer.insert(buffer.end(), con, con + len);
timestamp = GetTickCount(); timestamp = GetTickCount();
} }

View File

@ -28,11 +28,11 @@ private:
std::vector<char> buffer; std::vector<char> buffer;
std::wstring storage; std::wstring storage;
std::recursive_mutex ttMutex; std::recursive_mutex ttMutex;
HANDLE deletionEvent;
std::thread flushThread; HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL);
DWORD timestamp; std::thread flushThread = std::thread([&]() { while (WaitForSingleObject(deletionEvent, 100) == WAIT_TIMEOUT) Flush(); });
DWORD timestamp = GetTickCount();
ThreadOutputCallback Output; ThreadOutputCallback Output;
ThreadParam tp; ThreadParam tp;