@@ -36,7 +36,7 @@ public Similarity(string propertyName)
3636 if ( propertyName == null ) throw new ArgumentNullException ( "propertyName" ) ;
3737
3838 PropertyName = propertyName ;
39- ValueCount = 0 ;
39+ ValueCount = 1 ;
4040 _similarityValues = new Dictionary < string , SimilarityValue > ( ) ;
4141 }
4242
@@ -87,8 +87,7 @@ public void AddValue(string value)
8787 if ( value == null )
8888 value = "" ;
8989
90- SimilarityValue similarityValue ;
91- if ( ! _similarityValues . TryGetValue ( value , out similarityValue ) )
90+ if ( ! _similarityValues . TryGetValue ( value , out var similarityValue ) )
9291 {
9392 similarityValue = new SimilarityValue ( value ) ;
9493 _similarityValues . Add ( value , similarityValue ) ;
@@ -103,16 +102,6 @@ public void AddValue(string value)
103102 }
104103 }
105104
106- /// <summary>
107- /// Invoked during fetching.
108- /// </summary>
109- /// <param name="value">The value.</param>
110- public void AddValue ( SimilarityValue value )
111- {
112- if ( value == null ) throw new ArgumentNullException ( "value" ) ;
113-
114- _similarityValues . Add ( value . Value , value ) ;
115- }
116105
117106 /// <summary>
118107 /// Get the value with highest percentage count.
@@ -134,17 +123,22 @@ public void LoadValues(SimilarityValue[] values)
134123 {
135124 if ( values == null ) throw new ArgumentNullException ( "values" ) ;
136125
126+
127+ // Calculate the total.
128+ // Must do this first since we are empty
129+ // due to being loaded from the DB
130+ ValueCount = values . Sum ( x => x . Count ) ;
131+
137132 foreach ( var value in values )
138133 {
139- //TODO: Find out why this bug occur
140- // i.e. sometimes the exact same value is repeated over multiple values.
141- if ( _similarityValues . ContainsKey ( value . Value ) )
142- _similarityValues [ value . Value ] . IncreaseUsage ( ValueCount ) ;
134+ if ( _similarityValues . TryGetValue ( value . Value , out var similarityValue ) )
135+ similarityValue . IncreaseUsage ( ValueCount ) ;
143136 else
137+ {
144138 _similarityValues . Add ( value . Value , value ) ;
139+ value . Recalculate ( ValueCount ) ;
140+ }
145141 }
146- if ( ValueCount == 0 )
147- ValueCount = _similarityValues . Sum ( x => x . Value . Count ) ;
148142 }
149143
150144 /// <summary>
@@ -156,7 +150,7 @@ public void LoadValues(SimilarityValue[] values)
156150 /// <filterpriority>2</filterpriority>
157151 public override string ToString ( )
158152 {
159- return string . Format ( "{0 }[{1}]" , PropertyName , string . Join ( ", " , Values . Select ( x => x . Value ) ) ) ;
153+ return $ " { PropertyName } [{ string . Join ( ", " , Values . Select ( x => x . Value ) ) } ]" ;
160154 }
161155 }
162156}
0 commit comments