Files
Anubis/src/Anubis/News/Judgement.cs
T
Alby96andClaude Opus 5.5 9ee2367693 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>
2026-11-14 18:57:49 +01:00

130 lines
5.8 KiB
C#
Raw 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.
using System.Text.RegularExpressions;
namespace Anubis.News;
/// <summary>The serious cases of SPEC 10.2 point 4, found by keywords in English and in Italian.</summary>
public static class SevereKinds
{
public const string Cyberattack = "cyberattack";
public const string Suspension = "suspension";
public const string Fraud = "fraud";
public const string Bankruptcy = "bankruptcy";
public const string Investigation = "investigation";
public const string Delisting = "delisting";
}
/// <summary>The five grades of tone of SPEC 10.2 point 4.</summary>
public enum Tone
{
Negative = -2,
SlightlyNegative = -1,
Neutral = 0,
SlightlyPositive = 1,
Positive = 2,
}
/// <summary>What the local classifier says of an English text: the probabilities of its three classes.</summary>
public sealed record ToneScore(double Positive, double Negative, double Neutral)
{
/// <summary>The score of 10.2: probability of positive minus probability of negative, from −1 to +1.</summary>
public double Score => Positive - Negative;
}
/// <summary>The local classifier of SPEC 4.5. It reads only English; for other texts the caller does not ask.</summary>
public interface IToneClassifier
{
ToneScore Classify(string text);
}
/// <summary>The first judgement of an article: tone, serious case and importance, all without a language model.</summary>
public sealed record NewsJudgement(double? Score, string? Severe, int Importance)
{
public Tone? Tone => Score is { } s ? NewsJudge.Grade(s) : null;
}
/// <summary>
/// The first judgement of SPEC 10.2 point 4, local and free. The tone comes from the classifier for English texts;
/// the serious cases from keywords in two languages, so an Italian title of a fraud is still a serious case.
/// Importance: 4 to the serious cases, 3 to a negative article (score up to −0.6), 2 to the others linked to an
/// instrument, 1 to the rest. The 5 is given only by the language model (#58); a calendar event (3) arrives with #59.
/// </summary>
public static partial class NewsJudge
{
// Lower case, words separated by single spaces. A phrase matches whole words only: "hacked" is not "hack".
private static readonly (string Kind, string[] Phrases)[] Keywords =
[
(SevereKinds.Cyberattack,
[
"hack", "hacked", "hacker", "hackers", "hacking", "cyberattack", "cyber attack", "cyberattacks", "ransomware", "data breach", "security breach",
"exploit", "exploited", "drained", "attacco informatico", "attacchi informatici", "attacco hacker", "violazione dei dati",
]),
(SevereKinds.Suspension,
[
"trading halt", "trading halted", "halts trading", "halted trading", "suspends trading", "trading suspended", "suspended from trading",
"halts withdrawals", "halted withdrawals", "suspends withdrawals", "withdrawals halted", "withdrawals suspended", "pauses withdrawals",
"sospeso dalle negoziazioni", "sospensione delle negoziazioni", "sospende le negoziazioni", "titolo sospeso", "sospende i prelievi",
"sospensione dei prelievi", "blocca i prelievi",
]),
(SevereKinds.Fraud,
[
"fraud", "frauds", "fraudulent", "ponzi", "embezzlement", "accounting irregularities", "frode", "frodi", "truffa", "falso in bilancio",
"irregolarità contabili",
]),
(SevereKinds.Bankruptcy,
[
"bankruptcy", "bankrupt", "chapter 11", "insolvency", "insolvent", "files for bankruptcy", "fallimento", "bancarotta", "insolvenza",
"insolvente", "concordato preventivo", "amministrazione straordinaria",
]),
(SevereKinds.Investigation,
[
"investigation", "investigations", "probe", "probes", "subpoena", "subpoenaed", "indicted", "indictment", "charged with", "sec charges",
"raided", "indagine", "indagini", "indagato", "indagati", "inchiesta", "perquisizione", "perquisizioni",
]),
(SevereKinds.Delisting,
[
"delist", "delisted", "delisting", "delistings", "revoca dalla quotazione", "esclusione dalle negoziazioni",
]),
];
/// <summary>The grade of a score, with the bounds of 10.2: up to −0.6, −0.2, +0.2, +0.6, then positive.</summary>
public static Tone Grade(double score) => score switch
{
<= -0.6 => Tone.Negative,
<= -0.2 => Tone.SlightlyNegative,
<= 0.2 => Tone.Neutral,
<= 0.6 => Tone.SlightlyPositive,
_ => Tone.Positive,
};
/// <summary>The first serious case named by the text, in the order of 10.2, or null.</summary>
public static string? Severe(string title, string? summary)
{
var text = " " + Spaces().Replace(NonWord().Replace((title + " " + summary).ToLowerInvariant().Replace('’', '\''), " "), " ") + " ";
foreach (var (kind, phrases) in Keywords)
{
if (phrases.Any(p => text.Contains(" " + p + " ", StringComparison.Ordinal)))
{
return kind;
}
}
return null;
}
/// <summary>The judgement of one article. The classifier is asked only for English and only when it exists.</summary>
public static NewsJudgement Judge(string title, string? summary, string language, bool linkedToInstrument, IToneClassifier? classifier)
{
ArgumentNullException.ThrowIfNull(title);
double? score = classifier is not null && language == "en" ? Math.Clamp(classifier.Classify(title).Score, -1, 1) : null;
var severe = Severe(title, summary);
var importance = severe is not null ? 4 : score <= -0.6 ? 3 : linkedToInstrument ? 2 : 1;
return new NewsJudgement(score, severe, importance);
}
[GeneratedRegex(@"[^\p{L}\p{N}']+")]
private static partial Regex NonWord();
[GeneratedRegex(@"\s+")]
private static partial Regex Spaces();
}