计算字母评估量表的差异

Posted

技术标签:

【中文标题】计算字母评估量表的差异【英文标题】:Calculate difference in alphabetical evaluation scale 【发布时间】:2020-08-02 00:16:43 【问题描述】:

我有一个这样的评估字母表:

enter image description here

我有一个System.Data.DataTable,在Current risk 列中有一个可能的值,而在Forward risk 列中又是一个这个值,我需要计算当前风险和远期风险之间的差异,然后将这导致另一列。 差值是这样计算的: 例子:

如果当前 = AAA 且正向 = AA+ --> 差值 = -1

如果当前 = AAA 且正向 = AAA --> 差值 = 0

如果当前 = AAA 且正向 = AA --> 差值 = -2

如果当前 = AAA 且正向 = AA- --> 差值 = -3

如果当前 = AA+ 且正向 = AAA --> 差值 = +1

如果当前 = AA 且正向 = AAA --> 差值 = +2

如果电流 = A 且正向 = AAA --> 差值 = +5

我正在使用 C# .net 框架,我想到的唯一解决方案是使用大量 if-then,但我想知道是否有更好、更优雅的解决方案来解决这个问题。谢谢你

【问题讨论】:

【参考方案1】:

你可以使用 arrayList<T>,或者任何你真正想要的 collection

// insert what ever you like (this was only an example)
private static readonly string[] _array = new[] "AAA", "AA+", "AA", "A+", "A", "BBB", "BB+";

private static int Difference(string a, string b)
   => Array.IndexOf(_array, a) - Array.IndexOf(_array, b);

...

Console.WriteLine(Difference("AAA", "A+"));

注意:这只是一个示例,也是您可以创建的众多解决方案之一。在此示例中,您可以根据需要填写数组或使用您喜欢的任何集合。你应该有一些容错和验证输入等。

另一个选项是字符串和整数字典。

【讨论】:

【参考方案2】:

如果您创建一个Dictionary 来将风险字符串映射到整数,那么您可以只处理DataTable 中的每一行来更新一列。

首先,计算地图:

var RiskValue = "AAA".AsSingleton()
                     .Concat(new[]  "AA", "A", "BBB", "BB", "B", "CCC" .SelectMany(r => new[]  "+", "", "-" .Select(suffix => r + suffix)))
                     .Concat(new[]  "CC", "C", "D" )
                     .Select((r, v) => new  r, v )
                     .ToDictionary(rv => rv.r, rv => rv.v);

然后用值更新差异列:

foreach (var r in dt.AsEnumerable())
    r["Risk Difference"] = RiskValue[r.Field<string>("Current risk")] - RiskValue[r.Field<string>("Forward risk")];

PS 我的地图计算使用扩展将对象转换为IEnumerable&lt;T&gt;,但您也可以使用new[] object

public static IEnumerable<T> AsSingleton<T>(this T first) 
    yield return first;

【讨论】:

【参考方案3】:

我会首先创建一个类并映射每个风险的值。根据显示的示例,对于 A 组,我得出的结论是,值映射是这样的:

A- 值:0 一个值:1 A+ 值:2 AA- 值:3 AA 值:4 AA+ 值:5 AAA 值:6

存储该数据的类:

class Score

    public int Value  get; set; 
    public string Name  get; set; 

示例代码显示将“A”组添加到列表中并正确计算风险差异。

        List<Score> scoreList = new List<Score>();
        scoreList.Add(new Score  Name = "A-", Value = 0 );
        scoreList.Add(new Score  Name = "A", Value = 1 );
        scoreList.Add(new Score  Name = "A+", Value = 2 );
        scoreList.Add(new Score  Name = "AA-", Value = 3 );
        scoreList.Add(new Score  Name = "AA", Value = 4 );
        scoreList.Add(new Score  Name = "AA+", Value = 5 );
        scoreList.Add(new Score  Name = "AAA", Value = 6 );

        //If Current = AAA and Forward = AA+ --> Difference = -1
        var difference = scoreList.SingleOrDefault(i => i.Name == "AA+").Value - scoreList.SingleOrDefault(i => i.Name == "AAA").Value;

        //If Current = AAA and Forward = AAA-- > Difference = 0
        var difference1 = scoreList.SingleOrDefault(i => i.Name == "AAA").Value - scoreList.SingleOrDefault(i => i.Name == "AAA").Value;

        //If Current = AAA and Forward = AA-- > Difference = -2
        var difference2 = scoreList.SingleOrDefault(i => i.Name == "AA").Value - scoreList.SingleOrDefault(i => i.Name == "AAA").Value;

        //If Current = AAA and Forward = AA - --> Difference = -3
        var difference3 = scoreList.SingleOrDefault(i => i.Name == "AA-").Value - scoreList.SingleOrDefault(i => i.Name == "AAA").Value;

        //If Current = AA + and Forward = AAA-- > Difference = +1
        var difference4 = scoreList.SingleOrDefault(i => i.Name == "AAA").Value - scoreList.SingleOrDefault(i => i.Name == "AA+").Value;

        //If Current = AA and Forward = AAA-- > Difference = +2
        var difference5 = scoreList.SingleOrDefault(i => i.Name == "AAA").Value - scoreList.SingleOrDefault(i => i.Name == "AA").Value;

        //If Current = A and Forward = AAA-- > Difference = +5
        var difference6 = scoreList.SingleOrDefault(i => i.Name == "AAA").Value - scoreList.SingleOrDefault(i => i.Name == "A").Value;

希望这有助于您入门。

【讨论】:

以上是关于计算字母评估量表的差异的主要内容,如果未能解决你的问题,请参考以下文章

量表选择的经书?

如何评估假设检验的好坏

无论 Grafana 上的时间间隔变化如何,都需要评估差异

人格障碍评估

NLP-00-3BLEU计算

NLP-00-3BLEU计算