Reputation: 1121
So I have a problem... I am trying to consolidate 5 or so bilingual dictionaries, in HTML format, into a single, multilingual dictionary, with english as the source language. To do this, I decided to set up a dictionary, and map each non-english word to it's english counterpart(key) [see code below].
1 public void ConsolidateDictionary(string directoryPath)
2 {
3 DirectoryInfo directory = new DirectoryInfo(directoryPath);
4 string key = string.Empty;
5 string value = string.Empty;
6 Dictionary<string, List<string>> languages =
new Dictionary<string, List<string>>();
7 List<string> temp = new List<string>();
8 foreach (FileInfo file in directory.EnumerateFiles())
9 {
10 HtmlDocument doc = new HtmlDocument();
11 doc.Load(file.FullName);
12
13 foreach (HtmlNode node in doc.DocumentNode.SelectNodes(".//wordunit"))
14 {
15 foreach (HtmlNode child in node.SelectNodes(".//word"))
16 {
17 if (child.Attributes["language"].Value == "EN")
18 {
19 key = child.OuterHtml.ToString();
20 }
21 else
22 {
23 value = child.OuterHtml.ToString();
24 }
25 }
26
27 if (key != null && value != null)
28 {
29 if (languages.ContainsKey(key))
30 {
31 foreach (var item in languages[key])
32 {
33 temp.Add(item);
34 }
35 temp.Add(value);
36 languages.Remove(key);
37 languages.Add(key, temp);
38 temp.Clear();
39 }
40 else
41 {
42 temp.Add(value);
43 languages.Add(key, temp);
44 temp.Clear();
45 }
46 }
47 }
48 }
49 WriteFile(languages);
50 }
basically what is happening is, after each iteration of the foreach loop at line 15, the existing dictionary values are all nulled (but the keys remain). So, say that after the first iteration of the loop at line 15, the dictionary (called 'languages') contained: key: <word language="EN">Hello</word> Value: <word language="ES">Hola</word>
; when the second iteration comes around, the value is removed from the dictionary 'languages', leaving only:
key: <word language="EN">Hello</word>
Value: null
key: <word language="EN">Goodbye</word>
Value: <word language="ES">Chao</word>
(where the Goodbye-Chao pair were passed in as the key-value pair for the second iteration).
What could be causing this strange behavior... to my knowledge I'm not overwriting the values in my dictionary at all! Does anyone have any idea where I'm going wrong?
Upvotes: 0
Views: 1487
Reputation: 1103
You are setting temp the value to every key. You want to create a new object for temp each time you are assigning it. Once you call clear, you are wiping it on every item.
You are using the same list the entire time. So you add items to the first key, then clear it. This will clear everything you put into that value.
Fixed:
public void ConsolidateDictionary(string directoryPath)
{
DirectoryInfo directory = new DirectoryInfo(directoryPath);
string key = string.Empty;
string value = string.Empty;
Dictionary<string, List<string>> languages = new Dictionary<string, List<string>>();
List<string> temp = null;
foreach (FileInfo file in directory.EnumerateFiles())
{
HtmlDocument doc = new HtmlDocument();
doc.Load(file.FullName);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes(".//wordunit"))
{
foreach (HtmlNode child in node.SelectNodes(".//word"))
{
if (child.Attributes["language"].Value == "EN")
{
key = child.OuterHtml.ToString();
}
else
{
value = child.OuterHtml.ToString();
}
}
if (key != null && value != null)
{
if (languages.ContainsKey(key))
{
if(languages[key].Items.Contains(value) == false)
languages[key].Add(value);
}
else
{
languages.Add(key, new List<string>);
languages[key].Add(value);
}
}
}
}
WriteFile(languages);
}
Upvotes: 2
Reputation: 110071
temp.Add(value);
//languages.Add(key, temp);
temp.Clear();
Look at what you're doing to that poor List instance. Use a new List instance for each key.
if (!languages.ContainsKey(key))
{
languages.Add(key, new List<string>())
}
languages[key].Add(value);
Upvotes: 3