2
* Copyright (C) 2009 Jordi Mas i Hernàndez <jmas@softcatala.org>
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; either version 2 of the
7
* License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* General Public License for more details.
14
* You should have received a copy of the GNU General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
23
using System.Collections.Generic;
28
namespace gbrainy.Core.Main.Verbal
30
static public class AnalogiesFactory
32
static Dictionary <int, Analogy> [] analogies_arrays;
33
static bool read = false;
35
public const char Separator = '|';
37
static AnalogiesFactory ()
39
analogies_arrays = new Dictionary <int, Analogy> [(int) Analogy.Type.Last];
41
for (int i = 0; i < (int) Analogy.Type.Last; i++)
42
analogies_arrays[i] = new Dictionary <int, Analogy> ();
45
static public Dictionary <int, Analogy> Get (Analogy.Type type)
50
return analogies_arrays [(int) type];
53
static public void Read ()
57
List <string> answers;
61
StreamReader myStream = new StreamReader (Defines.DATA_DIR + Defines.VERBAL_ANALOGIES);
62
XmlTextReader reader = new XmlTextReader (myStream);
63
answers = new List <string> ();
65
analogy = new Analogy ();
66
while (reader.Read ())
68
name = reader.Name.ToLower ();
71
if (reader.NodeType == XmlNodeType.Element) {
72
analogy = new Analogy ();
76
if (reader.NodeType == XmlNodeType.EndElement) {
77
analogy.answers = answers.ToArray ();
78
analogies_arrays [(int) analogy.type].Add (analogies_arrays [(int) analogy.type].Count, analogy);
83
if (reader.NodeType != XmlNodeType.Element)
88
type = reader.GetAttribute ("type");
90
if (String.IsNullOrEmpty (type) == false) {
91
switch (type.ToLower ()) {
92
case "multipleoptions":
93
analogy.type = Analogy.Type.MultipleOptions;
95
case "pairofwordsoptions":
96
analogy.type = Analogy.Type.PairOfWordsOptions;
98
case "pairofwordscompare":
99
analogy.type = Analogy.Type.PairOfWordsCompare;
102
analogy.type = Analogy.Type.QuestionAnswer;
106
analogy.question = reader.ReadElementString ();
109
if (reader.NodeType == XmlNodeType.Element)
110
analogy.tip = reader.ReadElementString ();
114
if (reader.NodeType == XmlNodeType.Element)
115
analogy.rationale = reader.ReadElementString ();
119
if (reader.NodeType != XmlNodeType.Element)
124
right = reader.GetAttribute ("correct");
126
if (String.IsNullOrEmpty (right) == false)
127
if (right.ToLower () == "yes")
128
analogy.right = answers.Count;
130
answers.Add (reader.ReadElementString ());
138
for (int i = 0; i < (int) Analogy.Type.Last; i++)
140
cnt += analogies_arrays[i].Count;
142
if (analogies_arrays[i].Count > 0) {
143
Console.WriteLine (Catalog.GetString ("Read {0} verbal analogies of type {1}"), analogies_arrays[i].Count,
144
analogies_arrays[i][0].type.ToString ());
148
Console.WriteLine (Catalog.GetString ("Read a total of {0} verbal analogies"), cnt);
153
Console.WriteLine ("Error loading {0}. Exception {1}", Defines.DATA_DIR + Defines.VERBAL_ANALOGIES, e.Message);
162
static void CheckEmpty ()
164
Analogy empty = new Analogy ();
165
empty.question = Catalog.GetString ("There are no verbal analogies available.");
166
bool all_empty = true;
168
for (int i = 0; i < (int) Analogy.Type.Last; i++)
170
if (analogies_arrays[i].Count > 0) {
176
if (all_empty == true)
177
analogies_arrays[0].Add (0, empty);