-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.cs
137 lines (110 loc) · 4.05 KB
/
User.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//===----------------------------------------------------------------------===//
//
// Violet Styler
//
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2021. violet-team. All Rights Reserved.
//
//===----------------------------------------------------------------------===//
using System;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace violet_styler
{
class User
{
public List<UserArticle> UserArticles = new List<UserArticle>();
public string UserAppId;
public User(string userAppId)
{
this.UserAppId = userAppId;
}
public double Avg() => UserArticles.Sum(x => x.Score()) / UserArticles.Count;
public double Min() => UserArticles.Min(x => x.Score());
public double Max() => UserArticles.Max(x => x.Score());
public double Var()
{
var avg = Avg();
var dd = UserArticles.Select(x => (x.Score() - avg) * (x.Score() - avg));
return dd.Sum() / UserArticles.Count;
}
public double Std() => Math.Sqrt(Var());
public override string ToString()
{
return $"{UserArticles.Count}, {Avg()}, {Std()}\n"; //+ string.Join("\n", UserArticles.Select(x => x.Score()));
}
public void Print()
{
var x = new int[Convert.ToInt32(Max() / 500) + 1];
UserArticles.ForEach(y => x[Convert.ToInt32(y.Score() / 500)]++);
for (var i = 0; i < x.Length; i++)
{
Console.WriteLine(new string('*', x[i]));
}
}
public void Organize()
{
var avg = Avg();
var std = Std();
var zs = avg - 1.96 * std; // 95%
var ze = avg + 2.56 * std; // 99%
UserArticles = UserArticles.Where(x => x.Score() > zs && x.Score() < ze).ToList();
}
// Merge MPP of Same Articles
public void Merge()
{
var dict = new Dictionary<int, UserArticle>();
UserArticles.ForEach(x =>
{
if (!dict.ContainsKey(x.ArticleId))
dict.Add(x.ArticleId, x);
else
{
for (var i = 0; i < x.MPP.value.Count; i++)
dict[x.ArticleId].MPP.value[i] += x.MPP.value[i];
}
});
UserArticles = dict.ToList().Select(x => x.Value).ToList();
}
// Article Conecntration Rate
private Dictionary<int, double> concentrationCache = null;
public Dictionary<int, double> Concentration()
{
if (concentrationCache != null) return concentrationCache;
var dict = new Dictionary<int, double>();
// 0 ~ 20%: 0~1
// 20 ~ 40%: 1~2
// 40 ~ 60%: 2~3
// 60 ~ 80%: 3~4
// 80 ~ 100%: 4~5
// var z = new double[] { -0.842, -0.253, 0.253, 0.842 };
var avg = Avg();
var std = Std();
UserArticles.ForEach(x =>
{
var percent = NormalDist.Phi((x.Score() - avg) / std);
dict.Add(x.ArticleId, percent * 5);
});
return concentrationCache = dict;
}
// User Confidence Level
public static Dictionary<string, double> UDI(List<User> users)
{
var dict = new Dictionary<string, double>();
users = users.Where(x => !double.IsNaN(x.Std())).ToList();
var avgs = users.Select(x => new Tuple<string, double>(x.UserAppId, x.Std())).ToList();
var avg = avgs.Average(x => x.Item2);
var std = NormalDist.Std(avgs.Select(x => x.Item2).ToList());
avgs.ForEach(x =>
{
var percent = NormalDist.Phi((x.Item2 - avg) / std);
dict.Add(x.Item1, percent * 5);
});
return dict;
}
}
}