Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Classification based on subject and preview text #8257

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
707f69b
Classify emails based on subjects
st3iny Jan 4, 2023
4694e9e
fixup! Classify emails based on subjects
st3iny Jan 9, 2023
45e3fd5
fixup! Classify emails based on subjects
st3iny Jan 18, 2023
c814be4
Cache features per sender
st3iny Jan 24, 2023
a8e6cb1
Implement preprocess command
st3iny Jan 24, 2023
9ab8e77
feat(importance-classifier): Reduce text feature vector
ChristophWurst Jan 26, 2023
07c5013
fixup! feat(importance-classifier): Reduce text feature vector
ChristophWurst Jan 26, 2023
30fdfed
fixup! feat(importance-classifier): Reduce text feature vector
ChristophWurst Jan 27, 2023
fcc4090
fixup! feat(importance-classifier): Reduce text feature vector
ChristophWurst Jan 30, 2023
400ad37
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Jan 31, 2023
23fccdb
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Jan 31, 2023
6aa7596
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Jan 31, 2023
2f35709
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Jan 31, 2023
ed840d6
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 2, 2023
cf7cec1
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 3, 2023
8e6f55f
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 21, 2023
892754c
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 24, 2023
880fe2d
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 24, 2023
315b821
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 24, 2023
4640793
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 24, 2023
77a8c26
fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 28, 2023
e7847ba
fixup! fixup! feat(importance-classifier): Reduce text feature vector
st3iny Mar 30, 2023
f55e9a1
Try wcv -> tfidf pipeline
st3iny Mar 30, 2023
6d50a01
Fix transformer persistence
st3iny May 15, 2023
d91fe43
Refactor classifcation of new messages
st3iny May 15, 2023
909d31f
Refactor peristence
st3iny May 17, 2023
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
2 changes: 2 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
<command>OCA\Mail\Command\TrainAccount</command>
<command>OCA\Mail\Command\Thread</command>
<command>OCA\Mail\Command\UpdateAccount</command>
<command>OCA\Mail\Command\PreprocessAccount</command>
<command>OCA\Mail\Command\RunMetaEstimator</command>
</commands>
<settings>
<admin>OCA\Mail\Settings\AdminSettings</admin>
Expand Down
3 changes: 0 additions & 3 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use OCA\Mail\Events\MessageDeletedEvent;
use OCA\Mail\Events\MessageFlaggedEvent;
use OCA\Mail\Events\MessageSentEvent;
use OCA\Mail\Events\NewMessagesSynchronized;
use OCA\Mail\Events\OutboxMessageCreatedEvent;
use OCA\Mail\Events\SynchronizationEvent;
use OCA\Mail\HordeTranslationHandler;
Expand All @@ -57,7 +56,6 @@
use OCA\Mail\Listener\InteractionListener;
use OCA\Mail\Listener\MailboxesSynchronizedSpecialMailboxesUpdater;
use OCA\Mail\Listener\MessageCacheUpdaterListener;
use OCA\Mail\Listener\NewMessageClassificationListener;
use OCA\Mail\Listener\OauthTokenRefreshListener;
use OCA\Mail\Listener\SaveSentMessageListener;
use OCA\Mail\Listener\SpamReportListener;
Expand Down Expand Up @@ -120,7 +118,6 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(MessageSentEvent::class, FlagRepliedMessageListener::class);
$context->registerEventListener(MessageSentEvent::class, InteractionListener::class);
$context->registerEventListener(MessageSentEvent::class, SaveSentMessageListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, NewMessageClassificationListener::class);
$context->registerEventListener(SynchronizationEvent::class, AccountSynchronizedThreadUpdaterListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);

Expand Down
5 changes: 1 addition & 4 deletions lib/BackgroundJob/TrainImportanceClassifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ protected function run($argument) {
}

try {
$this->classifier->train(
$account,
$this->logger
);
$this->classifier->train($account, $this->logger);
} catch (Throwable $e) {
$this->logger->error('Cron importance classifier training failed: ' . $e->getMessage(), [
'exception' => $e,
Expand Down
24 changes: 15 additions & 9 deletions lib/Command/PredictImportance.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
* @author 2023 Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -29,6 +30,7 @@
use OCA\Mail\Db\Message;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\Classification\ImportanceClassifier;
use OCA\Mail\Support\ConsoleLoggerDecorator;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IConfig;
use Psr\Log\LoggerInterface;
Expand All @@ -41,6 +43,7 @@
class PredictImportance extends Command {
public const ARGUMENT_ACCOUNT_ID = 'account-id';
public const ARGUMENT_SENDER = 'sender';
public const ARGUMENT_SUBJECT = 'subject';

private AccountService $accountService;
private ImportanceClassifier $classifier;
Expand All @@ -59,26 +62,27 @@ public function __construct(AccountService $service,
$this->config = $config;
}

/**
* @return void
*/
protected function configure() {
protected function configure(): void {
$this->setName('mail:predict-importance');
$this->setDescription('Predict importance of an incoming message');
$this->addArgument(self::ARGUMENT_ACCOUNT_ID, InputArgument::REQUIRED);
$this->addArgument(self::ARGUMENT_SENDER, InputArgument::REQUIRED);
$this->addArgument(self::ARGUMENT_SUBJECT, InputArgument::OPTIONAL);
}

public function isEnabled() {
public function isEnabled(): bool {
return $this->config->getSystemValueBool('debug');
}

/**
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$accountId = (int)$input->getArgument(self::ARGUMENT_ACCOUNT_ID);
$sender = $input->getArgument(self::ARGUMENT_SENDER);
$subject = $input->getArgument(self::ARGUMENT_SUBJECT) ?? '';

$consoleLogger = new ConsoleLoggerDecorator(
$this->logger,
$output
);

try {
$account = $this->accountService->findById($accountId);
Expand All @@ -89,9 +93,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$fakeMessage = new Message();
$fakeMessage->setUid(0);
$fakeMessage->setFrom(AddressList::parse("Name <$sender>"));
$fakeMessage->setSubject($subject);
[$prediction] = $this->classifier->classifyImportance(
$account,
[$fakeMessage]
[$fakeMessage],
$consoleLogger
);
if ($prediction) {
$output->writeln('Message is important');
Expand Down
81 changes: 81 additions & 0 deletions lib/Command/PreprocessAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
*
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Command;

use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\PreprocessingService;
use OCP\AppFramework\Db\DoesNotExistException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function memory_get_peak_usage;

class PreprocessAccount extends Command {
public const ARGUMENT_ACCOUNT_ID = 'account-id';

private AccountService $accountService;
private PreprocessingService $preprocessingService;
private LoggerInterface $logger;

public function __construct(AccountService $service,
PreprocessingService $preprocessingService,
LoggerInterface $logger) {
parent::__construct();

$this->accountService = $service;
$this->preprocessingService = $preprocessingService;
$this->logger = $logger;
}

/**
* @return void
*/
protected function configure() {
$this->setName('mail:account:preprocess');
$this->setDescription('Preprocess all mailboxes of an IMAP account');
$this->addArgument(self::ARGUMENT_ACCOUNT_ID, InputArgument::REQUIRED);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$accountId = (int)$input->getArgument(self::ARGUMENT_ACCOUNT_ID);

try {
$account = $this->accountService->findById($accountId);
} catch (DoesNotExistException $e) {
$output->writeln("<error>Account $accountId does not exist</error>");
return 1;
}

$this->preprocessingService->process(4294967296, $account);

$mbs = (int)(memory_get_peak_usage() / 1024 / 1024);
$output->writeln('<info>' . $mbs . 'MB of memory used</info>');

return 0;
}
}
169 changes: 169 additions & 0 deletions lib/Command/RunMetaEstimator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
*
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Mail\Command;

use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\Classification\FeatureExtraction\NewCompositeExtractor;
use OCA\Mail\Service\Classification\ImportanceClassifier;
use OCA\Mail\Support\ConsoleLoggerDecorator;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IConfig;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Rubix\ML\Backends\Amp;
use Rubix\ML\Classifiers\KNearestNeighbors;
use Rubix\ML\CrossValidation\KFold;
use Rubix\ML\CrossValidation\Metrics\FBeta;
use Rubix\ML\GridSearch;
use Rubix\ML\Kernels\Distance\Euclidean;
use Rubix\ML\Kernels\Distance\Jaccard;
use Rubix\ML\Kernels\Distance\Manhattan;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RunMetaEstimator extends Command {
public const ARGUMENT_ACCOUNT_ID = 'account-id';
public const ARGUMENT_SHUFFLE = 'shuffle';
public const ARGUMENT_LOAD_DATA = 'load-data';

private AccountService $accountService;
private LoggerInterface $logger;
private ImportanceClassifier $classifier;
private ContainerInterface $container;
private IConfig $config;

public function __construct(
AccountService $accountService,
LoggerInterface $logger,
ImportanceClassifier $classifier,
ContainerInterface $container,
IConfig $config,
) {
parent::__construct();

$this->accountService = $accountService;
$this->logger = $logger;
$this->classifier = $classifier;
$this->container = $container;
$this->config = $config;
}

protected function configure(): void {
$this->setName('mail:account:run-meta-estimator');
$this->setDescription('Run the meta estimator for an account');
$this->addArgument(self::ARGUMENT_ACCOUNT_ID, InputArgument::REQUIRED);
$this->addOption(self::ARGUMENT_SHUFFLE, null, null, 'Shuffle data set before training');
$this->addOption(
self::ARGUMENT_LOAD_DATA,
null,
InputOption::VALUE_REQUIRED,
'Load training data set from a JSON file'
);
}

public function isEnabled(): bool {
return $this->config->getSystemValueBool('debug');
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$accountId = (int)$input->getArgument(self::ARGUMENT_ACCOUNT_ID);
$shuffle = (bool)$input->getOption(self::ARGUMENT_SHUFFLE);

try {
$account = $this->accountService->findById($accountId);
} catch (DoesNotExistException $e) {
$output->writeln("<error>Account $accountId does not exist</error>");
return 1;
}

/** @var NewCompositeExtractor $extractor */
$extractor = $this->container->get(NewCompositeExtractor::class);
$consoleLogger = new ConsoleLoggerDecorator(
$this->logger,
$output
);

if ($loadDataPath = $input->getOption(self::ARGUMENT_LOAD_DATA)) {
$json = file_get_contents($loadDataPath);
$dataSet = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} else {
$dataSet = $this->classifier->buildDataSet(
$account,
$extractor,
$consoleLogger,
null,
$shuffle,
);
}

$estimator = static function () use ($consoleLogger) {
$params = [
[5, 10, 15, 20, 25, 30], // Neighbors
[true, false], // Weighted?
[new Euclidean(), new Manhattan(), new Jaccard()], // Kernel
];

$estimator = new GridSearch(
KNearestNeighbors::class,
$params,
new FBeta(),
new KFold(3)
);
$estimator->setLogger($consoleLogger);
$estimator->setBackend(new Amp());
return $estimator;
};

if ($dataSet) {
$this->classifier->trainWithCustomDataSet(
$account,
$consoleLogger,
$dataSet,
$extractor,
$estimator,
null,
false,
);
} else {
$this->classifier->train(
$account,
$consoleLogger,
$extractor,
$estimator,
$shuffle,
false,
);
}

$mbs = (int)(memory_get_peak_usage() / 1024 / 1024);
$output->writeln('<info>' . $mbs . 'MB of memory used</info>');
return 0;
}
}
Loading