Judge every article locally: serious cases by keywords, tone grade and importance (refs #57)

This is the first judgement of SPEC 10.2 point 4, without a language model.

- Serious cases: cyberattack, suspension, fraud, bankruptcy, investigation and
  delisting are found by whole-word phrases in English and Italian, so an
  Italian headline still counts.
- Tone: the five tone grades follow the bounds of the specification.
- Importance: 4 for a serious case, 3 for a negative tone, 2 when the article is
  linked to an instrument, otherwise 1.
- Classifier: it sits behind IToneClassifier and is asked only for English
  titles. The ONNX model plugs in there; without it the tone stays unknown and
  everything else works.

Every article stores its score, case and importance. Each story keeps the mean
score, the case and the highest importance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-14 18:57:49 +01:00
1 parent f1ba452aba
commit 9ee2367693
5 files changed
+259 -12

No files matched your search

+99
View File
@@ -269,4 +269,103 @@ public sealed class NewsTests
Assert.IsFalse(NewsCollector.Due(never, Now.AddMinutes(59)));
Assert.IsTrue(NewsCollector.Due(never, Now.AddHours(1)), "Never more than an hour between two tries.");
}
[TestMethod]
public void TheToneHasFiveGradesWithTheBoundsOfTheSpecification()
{
Assert.AreEqual(Tone.Negative, NewsJudge.Grade(-1));
Assert.AreEqual(Tone.Negative, NewsJudge.Grade(-0.6));
Assert.AreEqual(Tone.SlightlyNegative, NewsJudge.Grade(-0.59));
Assert.AreEqual(Tone.SlightlyNegative, NewsJudge.Grade(-0.2));
Assert.AreEqual(Tone.Neutral, NewsJudge.Grade(-0.19));
Assert.AreEqual(Tone.Neutral, NewsJudge.Grade(0.2));
Assert.AreEqual(Tone.SlightlyPositive, NewsJudge.Grade(0.21));
Assert.AreEqual(Tone.SlightlyPositive, NewsJudge.Grade(0.6));
Assert.AreEqual(Tone.Positive, NewsJudge.Grade(0.61));
Assert.AreEqual(0.5, new ToneScore(0.7, 0.2, 0.1).Score, 1e-9);
}
[TestMethod]
public void SeriousCasesAreFoundInBothLanguagesAndOnlyAsWholeWords()
{
Assert.AreEqual(SevereKinds.Cyberattack, NewsJudge.Severe("Exchange halts withdrawals after hack, Solana tokens drained", null));
Assert.AreEqual(SevereKinds.Suspension, NewsJudge.Severe("Exchange halts withdrawals for maintenance", null));
Assert.AreEqual(SevereKinds.Fraud, NewsJudge.Severe("SEC charges founder with securities fraud", null));
Assert.AreEqual(SevereKinds.Bankruptcy, NewsJudge.Severe("Retailer files for Chapter 11 protection", null));
Assert.AreEqual(SevereKinds.Investigation, NewsJudge.Severe("Regulators open a probe into the bank", null));
Assert.AreEqual(SevereKinds.Delisting, NewsJudge.Severe("Exchange to delist three tokens", null));
Assert.AreEqual(SevereKinds.Suspension, NewsJudge.Severe("Il titolo sospeso dalle negoziazioni", null));
Assert.AreEqual(SevereKinds.Fraud, NewsJudge.Severe("Inchiesta per frode sulla società", null), "Two cases: the first in the order of 10.2.");
Assert.AreEqual(SevereKinds.Cyberattack, NewsJudge.Severe("Banca colpita da un attacco informatico", null));
Assert.AreEqual(SevereKinds.Bankruptcy, NewsJudge.Severe("Shares fall", "The company warned of a possible bankruptcy."), "The summary counts too.");
Assert.IsNull(NewsJudge.Severe("Students win the bank's hackathon", null));
Assert.IsNull(NewsJudge.Severe("Bitcoin drifts below $90,000 as traders await U.S. data", null));
Assert.IsNull(NewsJudge.Severe("Borse europee in rialzo, Piazza Affari guida", null));
}
[TestMethod]
public void ImportanceComesFromTheSeriousCasesTheToneAndTheLinks()
{
var classifier = new WordClassifier();
Assert.AreEqual(new NewsJudgement(null, SevereKinds.Fraud, 4), NewsJudge.Judge("Inchiesta per frode", null, "it", true, classifier));
Assert.AreEqual(0, classifier.Calls, "Italian titles are never given to the classifier.");
Assert.AreEqual(new NewsJudgement(-0.8125, null, 3), NewsJudge.Judge("Nvidia slides", null, "en", false, classifier));
Assert.AreEqual(new NewsJudgement(0.75, null, 2), NewsJudge.Judge("Apple shares jump", null, "en", true, classifier));
Assert.AreEqual(new NewsJudgement(0, null, 1), NewsJudge.Judge("Markets wait", null, "en", false, classifier));
Assert.AreEqual(new NewsJudgement(-0.8125, SevereKinds.Cyberattack, 4), NewsJudge.Judge("Tokens drained in a hack", null, "en", true, classifier));
Assert.AreEqual(new NewsJudgement(null, null, 2), NewsJudge.Judge("Nvidia slides", null, "en", true, null), "Without the classifier the tone is unknown.");
Assert.AreEqual(Tone.Negative, NewsJudge.Judge("Nvidia slides", null, "en", false, classifier).Tone);
Assert.IsNull(NewsJudge.Judge("Nvidia slides", null, "en", false, null).Tone);
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task EveryArticleKeepsItsJudgementAndTheStorySumsThemUp()
{
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
var clock = new ManualClock(Now);
foreach (var venue in new[] { Venues.Alpaca, Venues.Okx })
{
await new InstrumentCatalog(test.Db, clock).SyncAsync(venue, [.. Instruments.Where(i => i.Venue == venue)], complete: false, CancellationToken.None)
.ConfigureAwait(false);
}
var store = new NewsStore(test.Db, clock);
await store.EnsureDefaultsAsync(CancellationToken.None).ConfigureAwait(false);
var sources = (await store.ListSourcesAsync(CancellationToken.None).ConfigureAwait(false)).ToDictionary(s => s.Name);
var linker = await new NewsCollector(new HttpClient(), test.Db, clock).DictionaryAsync(CancellationToken.None).ConfigureAwait(false);
var classifier = new WordClassifier();
foreach (var (file, source) in new[] { ("cnbc", "CNBC"), ("marketwatch", "MarketWatch"), ("coindesk", "CoinDesk"), ("sole24ore", "Il Sole 24 Ore") })
{
await store.IngestAsync(sources[source], FeedParser.Parse(Fixture.Bytes($"news/{file}.xml")), linker, CancellationToken.None, classifier).ConfigureAwait(false);
}
var stories = await test.Db.ReadAsync(s => s.QueryAsync("SELECT title, score, severe, importance FROM news_stories",
r => (Title: r.Text("title"), Score: r.IsNull("score") ? (double?)null : r.Measure("score"), Severe: r.TextOrNull("severe"), Importance: (int)r.Number("importance"))))
.ConfigureAwait(false);
(string Title, double? Score, string? Severe, int Importance) Story(string start) => stories.Single(s => s.Title.StartsWith(start, StringComparison.Ordinal));
Assert.AreEqual((0.75, (string?)null, 2), (Story("Apple shares jump").Score, Story("Apple shares jump").Severe, Story("Apple shares jump").Importance));
Assert.AreEqual(3, Story("Target cuts").Importance, "Negative, though linked to nothing.");
Assert.AreEqual(3, Story("Fire at a chip plant").Importance);
Assert.AreEqual((SevereKinds.Cyberattack, 4), (Story("Exchange halts withdrawals").Severe, Story("Exchange halts withdrawals").Importance));
Assert.AreEqual(((double?)null, 2), (Story("Il Bitcoin scende").Score, Story("Il Bitcoin scende").Importance), "Italian: no tone, linked to Bitcoin.");
Assert.AreEqual((0d, 1), (Story("Weekly wrap").Score ?? double.NaN, Story("Weekly wrap").Importance));
Assert.AreEqual(1, Story("Borse europee").Importance);
}
/// <summary>A stand-in for the classifier of 4.5: a few words decide the tone.</summary>
private sealed class WordClassifier : IToneClassifier
{
public int Calls { get; private set; }
public ToneScore Classify(string text)
{
Calls++;
var lower = text.ToLowerInvariant();
return new[] { "slides", "cuts", "drained", "hits" }.Any(lower.Contains) ? new ToneScore(0.0625, 0.875, 0.0625)
: new[] { "jump", "gains" }.Any(lower.Contains) ? new ToneScore(0.875, 0.125, 0)
: new ToneScore(0.125, 0.125, 0.75);
}
}
}