|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace JiebaNet.Segmenter.Common |
| 6 | +{ |
| 7 | + public interface ICounter<T> |
| 8 | + { |
| 9 | + int Count { get; } |
| 10 | + int Total { get; } |
| 11 | + int this[T key] { get; set; } |
| 12 | + IEnumerable<KeyValuePair<T, int>> Elements { get; } |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Lists the n most common elements from the most common to the least. |
| 16 | + /// </summary> |
| 17 | + /// <param name="n">Number of elements, list all elements if n is less than 0.</param> |
| 18 | + /// <returns></returns> |
| 19 | + IEnumerable<KeyValuePair<T, int>> MostCommon(int n = -1); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Subtracts items from a counter. |
| 23 | + /// </summary> |
| 24 | + /// <param name="items"></param> |
| 25 | + void Subtract(IEnumerable<T> items); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Subtracts counts from another counter. |
| 29 | + /// </summary> |
| 30 | + /// <param name="other"></param> |
| 31 | + void Subtract(ICounter<T> other); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Adds items to a counter. |
| 35 | + /// </summary> |
| 36 | + /// <param name="items"></param> |
| 37 | + void Add(IEnumerable<T> items); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Adds another counter. |
| 41 | + /// </summary> |
| 42 | + /// <param name="other"></param> |
| 43 | + void Add(ICounter<T> other); |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Union is the maximum of value in either of the input <see cref="ICounter{T}"/>. |
| 47 | + /// </summary> |
| 48 | + /// <param name="other">The other counter.</param> |
| 49 | + ICounter<T> Union(ICounter<T> other); |
| 50 | + |
| 51 | + void Remove(T key); |
| 52 | + void Clear(); |
| 53 | + bool Contains(T key); |
| 54 | + } |
| 55 | + |
| 56 | + public class Counter<T>: ICounter<T> |
| 57 | + { |
| 58 | + private Dictionary<T, int> data = new Dictionary<T, int>(); |
| 59 | + |
| 60 | + public Counter() {} |
| 61 | + |
| 62 | + public Counter(IEnumerable<T> items) |
| 63 | + { |
| 64 | + CountItems(items); |
| 65 | + } |
| 66 | + |
| 67 | + public int Count => data.Count; |
| 68 | + public int Total => data.Values.Sum(); |
| 69 | + public IEnumerable<KeyValuePair<T, int>> Elements => data; |
| 70 | + |
| 71 | + public int this[T key] |
| 72 | + { |
| 73 | + get => data.ContainsKey(key) ? data[key] : 0; |
| 74 | + set => data[key] = value; |
| 75 | + } |
| 76 | + |
| 77 | + public IEnumerable<KeyValuePair<T, int>> MostCommon(int n = -1) |
| 78 | + { |
| 79 | + var pairs = data.Where(pair => pair.Value > 0).OrderByDescending(pair => pair.Value); |
| 80 | + return n < 0 ? pairs : pairs.Take(n); |
| 81 | + } |
| 82 | + |
| 83 | + public void Subtract(IEnumerable<T> items) |
| 84 | + { |
| 85 | + SubtractItems(items); |
| 86 | + } |
| 87 | + |
| 88 | + public void Subtract(ICounter<T> other) |
| 89 | + { |
| 90 | + SubtractPairs(other.Elements); |
| 91 | + } |
| 92 | + |
| 93 | + public void Add(IEnumerable<T> items) |
| 94 | + { |
| 95 | + CountItems(items); |
| 96 | + } |
| 97 | + |
| 98 | + public void Add(ICounter<T> other) |
| 99 | + { |
| 100 | + CountPairs(other.Elements); |
| 101 | + } |
| 102 | + |
| 103 | + public ICounter<T> Union(ICounter<T> other) |
| 104 | + { |
| 105 | + var result = new Counter<T>(); |
| 106 | + foreach (var pair in data) |
| 107 | + { |
| 108 | + var count = pair.Value; |
| 109 | + var otherCount = other[pair.Key]; |
| 110 | + var newCount = count < otherCount ? otherCount : count; |
| 111 | + result[pair.Key] = newCount; |
| 112 | + } |
| 113 | + |
| 114 | + foreach (var pair in other.Elements) |
| 115 | + { |
| 116 | + if (!Contains(pair.Key)) |
| 117 | + { |
| 118 | + result[pair.Key] = pair.Value; |
| 119 | + } |
| 120 | + } |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + public void Remove(T key) |
| 125 | + { |
| 126 | + if (data.ContainsKey(key)) |
| 127 | + { |
| 128 | + data.Remove(key); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public void Clear() |
| 133 | + { |
| 134 | + data.Clear(); |
| 135 | + } |
| 136 | + |
| 137 | + public bool Contains(T key) |
| 138 | + { |
| 139 | + return data.ContainsKey(key); |
| 140 | + } |
| 141 | + |
| 142 | + #region Private Methods |
| 143 | + |
| 144 | + private void CountItems(IEnumerable<T> items) |
| 145 | + { |
| 146 | + foreach (var item in items) |
| 147 | + { |
| 148 | + data[item] = data.GetDefault(item, 0) + 1; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private void CountPairs(IEnumerable<KeyValuePair<T, int>> pairs) |
| 153 | + { |
| 154 | + foreach (var pair in pairs) |
| 155 | + { |
| 156 | + this[pair.Key] += pair.Value; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private void SubtractItems(IEnumerable<T> items) |
| 161 | + { |
| 162 | + foreach (var item in items) |
| 163 | + { |
| 164 | + data[item] = data.GetDefault(item, 0) - 1; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void SubtractPairs(IEnumerable<KeyValuePair<T, int>> pairs) |
| 169 | + { |
| 170 | + foreach (var pair in pairs) |
| 171 | + { |
| 172 | + this[pair.Key] -= pair.Value; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + #endregion |
| 177 | + } |
| 178 | +} |
0 commit comments