Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions settings/qt6-app/qml/pages/PagePersonalisation.qml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ Item {
width: parent.width
spacing: 12

// Accept rate and time saved come from the engine's in-memory
// counters (metrics.json), which reset when fcitx5 restarts —
// hence "this session" and no per-day bars for those two.
Repeater {
model: [
{ label: "Words learned", value: CorpusStats.words.toLocaleString(), bars: [20, 30, 25, 40, 38, 50, CorpusStats.words > 0 ? 100 : 0] },
{ label: "Accept rate · 7d", value: "0%", bars: [0, 0, 0, 0, 0, 0, 0] },
{ label: "Time saved · 7d", value: "0 s", bars: [0, 0, 0, 0, 0, 0, 0] },
{ label: "Words learned", value: CorpusStats.words.toLocaleString(), bars: CorpusStats.wordsPerDay },
{ label: "Accept rate · this session", value: Math.round(Engine.acceptRate * 100) + "%", bars: [] },
{ label: "Time saved · this session", value: root.formatDuration(Engine.secondsSaved), bars: [] },
]
delegate: Rectangle {
required property var modelData
Expand Down Expand Up @@ -63,6 +66,7 @@ Item {
}
Item { width: 6 }
XBars {
opacity: modelData.bars.length > 0 ? 1 : 0 // keep the slot so labels stay aligned
values: modelData.bars
width: 56; height: 24
anchors.verticalCenter: parent.verticalCenter
Expand Down Expand Up @@ -307,6 +311,15 @@ Item {
}
}

// Time saved is an estimate: accepted characters at an assumed typing
// speed (EngineProbe::kCharsPerSecond).
function formatDuration(secs) {
const s = Math.round(secs)
if (s < 60) return s + " s"
if (s < 3600) return Math.round(s / 60) + " min"
return (s / 3600).toFixed(1) + " h"
}

function exportProfile() {
const src = Qt.resolvedUrl("file://" + Qt.platform.os) // placeholder
// In a real app: QFileDialog exposed via invokable; for now show toast
Expand All @@ -325,13 +338,14 @@ Item {

Rectangle {
anchors.centerIn: parent
width: 320; height: 160
width: 320; height: confirmCol.implicitHeight + 40
radius: Theme.radius.card
color: Qt.rgba(0.08, 0.06, 0.13, 0.97)
border.color: Theme.ink12; border.width: 1

Column {
anchors { fill: parent; margins: 20 }
id: confirmCol
anchors { left: parent.left; right: parent.right; top: parent.top; margins: 20 }
spacing: 16

Text {
Expand All @@ -342,7 +356,7 @@ Item {
}
Text {
width: parent.width
text: "This removes all learned text. A backup is saved automatically."
text: "This permanently deletes everything XType learned from your typing. The engine stops using it within 5 minutes."
font.family: Theme.sansFamily; font.pixelSize: 12
color: Theme.ink65; wrapMode: Text.WordWrap
renderType: Text.NativeRendering
Expand All @@ -355,9 +369,9 @@ Item {
variant: "primary"
onClicked: {
resetConfirm.visible = false
// xtype-corpus wipe --yes is destructive — run and refresh stats
Qt.openUrlExternally("xtype-corpus://wipe") // no-op placeholder
CorpusStats.refresh()
const removed = CorpusStats.wipe()
StyleProfile.refresh()
wipeText.text = removed < 0 ? "Could not delete some corpus files" : "Corpus wiped"
wipeToast.show()
}
}
Expand Down
172 changes: 138 additions & 34 deletions settings/qt6-app/src/corpus_stats.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,158 @@
#include "corpus_stats.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>

// Counts whitespace-separated tokens; byte-level so multi-MB corpora stay cheap.
static int countWords(const QByteArray &line) {
int n = 0;
bool inWord = false;
for (char c : line) {
const bool space = c == ' ' || c == '\t' || c == '\r' || c == '\n'
|| c == '\v' || c == '\f';
if (!space && !inWord) ++n;
inWord = !space;
}
return n;
}

static QString timestampsPath(const QString &corpusPath) {
const QFileInfo fi(corpusPath);
return fi.dir().filePath(fi.completeBaseName() + "_timestamps.txt");
}

CorpusStats::CorpusStats(QObject *parent) : QObject(parent) {
_timeout.setSingleShot(true);
_timeout.setInterval(5000);
connect(&_timeout, &QTimer::timeout, this, &CorpusStats::onTimeout);
connect(&_proc, QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished),
this, &CorpusStats::onFinished);
_proc.setProcessChannelMode(QProcess::MergedChannels);
_counts.wordsPerDay = QList<int>(kDays, 0);
_timer.setInterval(3000);
connect(&_timer, &QTimer::timeout, this, &CorpusStats::refresh);
_timer.start();
setCorpusPath("~/.local/share/xtype/corpus.txt");
}

QVariantList CorpusStats::wordsPerDay() const {
QVariantList out;
for (int n : _counts.wordsPerDay) out.append(n);
return out;
}

void CorpusStats::setCorpusPath(const QString &path) {
QString p = path;
if (p.startsWith("~/")) p = QDir::homePath() + p.mid(1);
if (p == _path) return;
_path = p;
_signature.clear();
refresh();
}

QStringList CorpusStats::corpusFiles(const QString &corpusPath) {
const QFileInfo fi(corpusPath);
QStringList out;
if (fi.exists()) out << fi.absoluteFilePath();
// Rotated siblings written by CorpusCollector::rotateIfNeeded(); skip the
// transient .tmp the engine uses for atomic pruning.
const QStringList rotated = fi.dir().entryList({fi.fileName() + ".*"}, QDir::Files, QDir::Name);
for (const QString &name : rotated)
if (!name.endsWith(".tmp")) out << fi.dir().filePath(name);
return out;
}

QString CorpusStats::fileSignature() const {
QString sig;
QStringList inputs = corpusFiles(_path);
inputs << timestampsPath(_path);
for (const QString &f : inputs) {
const QFileInfo fi(f);
if (!fi.exists()) continue;
sig += f + ':' + QString::number(fi.size()) + ':'
+ QString::number(fi.lastModified().toMSecsSinceEpoch()) + ';';
}
return sig;
}

void CorpusStats::refresh() {
if (_running) return;
_running = true;
_proc.start("xtype-corpus", {"stats", "--json"});
if (!_proc.waitForStarted(500)) {
_running = false;
return; // binary not found — stats stay at 0
if (_path.isEmpty()) return;
const QDate today = QDate::currentDate();
const QString sig = fileSignature();
if (sig == _signature && today == _countedOn) return;
_signature = sig;
_countedOn = today;

const CorpusCounts c = count(_path, QDateTime::currentDateTime());
if (c.sentences != _counts.sentences || c.words != _counts.words
|| c.bytes != _counts.bytes || c.wordsPerDay != _counts.wordsPerDay) {
_counts = c;
emit statsChanged();
}
_timeout.start();
}

void CorpusStats::onFinished(int code, QProcess::ExitStatus) {
_timeout.stop();
_running = false;
CorpusCounts CorpusStats::count(const QString &corpusPath, const QDateTime &now) {
CorpusCounts c;
c.wordsPerDay = QList<int>(kDays, 0);

if (code != 0) return;
// The engine appends one timestamp line per corpus line (same order), so
// line i of the live corpus file was harvested at timestamps[i]. Rotated
// files have no timestamps and only contribute to the totals.
QList<qint64> stamps;
{
QFile tf(timestampsPath(corpusPath));
if (tf.open(QIODevice::ReadOnly)) {
for (const QByteArray &l : tf.readAll().split('\n'))
stamps.append(l.trimmed().toLongLong()); // malformed -> 0 (undated)
}
}

const QByteArray raw = _proc.readAll();
const QJsonObject obj = QJsonDocument::fromJson(raw).object();
if (obj.isEmpty()) return;
const QString live = QFileInfo(corpusPath).absoluteFilePath();
const QDate today = now.date();

const int s = obj.value("sentences").toInt();
const int w = obj.value("words").toInt();
const double m = obj.value("mb").toDouble();
for (const QString &path : corpusFiles(corpusPath)) {
QFile f(path);
if (!f.open(QIODevice::ReadOnly)) continue;
const QByteArray data = f.readAll();
c.bytes += data.size();

if (s != _sentences || w != _words || m != _mb) {
_sentences = s;
_words = w;
_mb = m;
emit statsChanged();
const bool dated = (path == live);
const QList<QByteArray> lines = data.split('\n');
for (qsizetype i = 0; i < lines.size(); ++i) {
const int w = countWords(lines[i]);
if (w == 0) continue;
++c.sentences;
c.words += w;

if (!dated || i >= stamps.size() || stamps[i] <= 0) continue;
const QDate day = QDateTime::fromSecsSinceEpoch(stamps[i]).date();
const qint64 ago = day.daysTo(today);
if (ago >= 0 && ago < kDays)
c.wordsPerDay[kDays - 1 - static_cast<int>(ago)] += w;
}
}
return c;
}

void CorpusStats::onTimeout() {
_proc.kill();
_running = false;
int CorpusStats::wipe() {
const int removed = wipeFiles(_path);
_signature.clear();
refresh();
return removed;
}

int CorpusStats::wipeFiles(const QString &corpusPath) {
if (corpusPath.isEmpty()) return -1;
const QFileInfo fi(corpusPath);
// style_profile.json sits next to the corpus (XTypeEngine derives it from
// corpus_path's parent directory).
QStringList targets = corpusFiles(corpusPath);
targets << timestampsPath(corpusPath)
<< fi.dir().filePath(fi.fileName() + ".tmp") // pruneOldEntries() scratch files
<< timestampsPath(corpusPath) + ".tmp"
<< fi.dir().filePath("style_profile.json");

int removed = 0;
bool failed = false;
for (const QString &t : targets) {
if (!QFileInfo::exists(t)) continue;
if (QFile::remove(t)) ++removed;
else failed = true;
}
return failed ? -1 : removed;
}
59 changes: 42 additions & 17 deletions settings/qt6-app/src/corpus_stats.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
#pragma once

#include <QDateTime>
#include <QList>
#include <QObject>
#include <QProcess>
#include <QString>
#include <QTimer>
#include <QVariantList>

struct CorpusCounts {
int sentences = 0;
int words = 0;
qint64 bytes = 0;
QList<int> wordsPerDay; // kDays entries, oldest first; last entry is today
};

class CorpusStats : public QObject {
Q_OBJECT
Q_PROPERTY(int sentences READ sentences NOTIFY statsChanged)
Q_PROPERTY(int words READ words NOTIFY statsChanged)
Q_PROPERTY(double mb READ mb NOTIFY statsChanged)
Q_PROPERTY(int sentences READ sentences NOTIFY statsChanged)
Q_PROPERTY(int words READ words NOTIFY statsChanged)
Q_PROPERTY(double mb READ mb NOTIFY statsChanged)
Q_PROPERTY(QVariantList wordsPerDay READ wordsPerDay NOTIFY statsChanged)

public:
static constexpr int kDays = 7;

explicit CorpusStats(QObject *parent = nullptr);

int sentences() const { return _sentences; }
int words() const { return _words; }
double mb() const { return _mb; }
int sentences() const { return _counts.sentences; }
int words() const { return _counts.words; }
double mb() const { return _counts.bytes / (1024.0 * 1024.0); }
QVariantList wordsPerDay() const;

// Accepts a leading "~/" (as stored in config.toml).
void setCorpusPath(const QString &path);

Q_INVOKABLE void refresh();

// Deletes everything learned from typing: the corpus, its rotated copies,
// the timestamp file and style_profile.json. Returns the number of files
// removed, or -1 if any existing file could not be removed.
Q_INVOKABLE int wipe();
static int wipeFiles(const QString &corpusPath);

// Reads the corpus file plus its rotated siblings (corpus.txt.1, ...) and
// the engine's per-line timestamp file. `now` anchors the 7-day window.
static CorpusCounts count(const QString &corpusPath, const QDateTime &now);

signals:
void statsChanged();

private slots:
void onFinished(int code, QProcess::ExitStatus status);
void onTimeout();

private:
int _sentences = 0;
int _words = 0;
double _mb = 0.0;
QProcess _proc;
QTimer _timeout;
bool _running = false;
static QStringList corpusFiles(const QString &corpusPath);
QString fileSignature() const;

QString _path;
QString _signature; // size+mtime of every input; recount only on change
QDate _countedOn; // day buckets shift at midnight even without writes
CorpusCounts _counts;
QTimer _timer;
};
Loading