~ubuntu-branches/ubuntu/lucid/gbrainy/lucid

« back to all changes in this revision

Viewing changes to src/Core/Main/Verbal/AnalogiesFactory.cs

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-01-12 11:21:24 UTC
  • mfrom: (13.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100112112124-o4ztomxa0xfh2ulj
Tags: 1.30-1ubuntu1
* debian/control:
  - Revert build-depends to lucid versions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Jordi Mas i Hernàndez <jmas@softcatala.org>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
using System;
 
21
using System.Xml;
 
22
using System.IO;
 
23
using System.Collections.Generic;
 
24
 
 
25
using Cairo;
 
26
using Mono.Unix;
 
27
 
 
28
namespace gbrainy.Core.Main.Verbal
 
29
{
 
30
        static public class AnalogiesFactory
 
31
        {
 
32
                static Dictionary <int, Analogy> [] analogies_arrays;
 
33
                static bool read = false;
 
34
        
 
35
                public const char Separator = '|';
 
36
 
 
37
                static AnalogiesFactory ()
 
38
                {
 
39
                        analogies_arrays = new Dictionary <int, Analogy> [(int) Analogy.Type.Last];
 
40
 
 
41
                        for (int i = 0; i < (int) Analogy.Type.Last; i++)
 
42
                                analogies_arrays[i] = new Dictionary <int, Analogy> ();
 
43
                }
 
44
 
 
45
                static public Dictionary <int, Analogy> Get (Analogy.Type type)
 
46
                {
 
47
                        if (read == false)
 
48
                                Read ();
 
49
 
 
50
                        return analogies_arrays [(int) type];
 
51
                }
 
52
 
 
53
                static public void Read ()
 
54
                {
 
55
                        Analogy analogy;
 
56
                        string name;
 
57
                        List <string> answers;
 
58
 
 
59
                        try 
 
60
                        {
 
61
                                StreamReader myStream = new StreamReader (Defines.DATA_DIR + Defines.VERBAL_ANALOGIES);
 
62
                                XmlTextReader reader = new XmlTextReader (myStream);
 
63
                                answers = new List <string> ();
 
64
 
 
65
                                analogy = new Analogy ();
 
66
                                while (reader.Read ())
 
67
                                {
 
68
                                        name = reader.Name.ToLower ();
 
69
                                        switch (name) {
 
70
                                        case "analogy":
 
71
                                                if (reader.NodeType == XmlNodeType.Element) {
 
72
                                                        analogy = new Analogy ();
 
73
                                                        answers.Clear ();
 
74
                                                }
 
75
                                                else {
 
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);
 
79
                                                        }
 
80
                                                }
 
81
                                                break;
 
82
                                        case "_question":
 
83
                                                if (reader.NodeType != XmlNodeType.Element)
 
84
                                                        return;
 
85
 
 
86
                                                string type;
 
87
 
 
88
                                                type = reader.GetAttribute ("type");
 
89
                
 
90
                                                if (String.IsNullOrEmpty (type) == false) {
 
91
                                                        switch (type.ToLower ()) {
 
92
                                                        case "multipleoptions":
 
93
                                                                analogy.type = Analogy.Type.MultipleOptions;
 
94
                                                                break;
 
95
                                                        case "pairofwordsoptions":
 
96
                                                                analogy.type = Analogy.Type.PairOfWordsOptions;
 
97
                                                                break;
 
98
                                                        case "pairofwordscompare":
 
99
                                                                analogy.type = Analogy.Type.PairOfWordsCompare;
 
100
                                                                break;
 
101
                                                        default:
 
102
                                                                analogy.type = Analogy.Type.QuestionAnswer;
 
103
                                                                break;
 
104
                                                        }
 
105
                                                }
 
106
                                                analogy.question = reader.ReadElementString ();
 
107
                                                break;
 
108
                                        case "_tip":
 
109
                                                if (reader.NodeType == XmlNodeType.Element)
 
110
                                                        analogy.tip = reader.ReadElementString ();
 
111
 
 
112
                                                break;
 
113
                                        case "_rationale":
 
114
                                                if (reader.NodeType == XmlNodeType.Element)
 
115
                                                        analogy.rationale = reader.ReadElementString ();
 
116
 
 
117
                                                break;
 
118
                                        case "_answer":
 
119
                                                if (reader.NodeType != XmlNodeType.Element)
 
120
                                                        break;
 
121
        
 
122
                                                string right;
 
123
 
 
124
                                                right = reader.GetAttribute ("correct");
 
125
                
 
126
                                                if (String.IsNullOrEmpty (right) == false)
 
127
                                                        if (right.ToLower () == "yes")
 
128
                                                                analogy.right = answers.Count;
 
129
                                        
 
130
                                                answers.Add (reader.ReadElementString ());
 
131
                                                break;
 
132
                                        }
 
133
                                }
 
134
 
 
135
                                read = true;
 
136
 
 
137
                                int cnt = 0;    
 
138
                                for (int i = 0; i < (int) Analogy.Type.Last; i++) 
 
139
                                {
 
140
                                        cnt += analogies_arrays[i].Count;
 
141
 
 
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 ());
 
145
                                        }
 
146
                                }
 
147
                        
 
148
                                Console.WriteLine (Catalog.GetString ("Read a total of {0} verbal analogies"), cnt);
 
149
                        }
 
150
 
 
151
                        catch (Exception e)
 
152
                        {
 
153
                                Console.WriteLine ("Error loading {0}. Exception {1}", Defines.DATA_DIR + Defines.VERBAL_ANALOGIES, e.Message);
 
154
                        }
 
155
 
 
156
                        finally
 
157
                        {
 
158
                                CheckEmpty ();
 
159
                        }
 
160
                }
 
161
 
 
162
                static void CheckEmpty ()
 
163
                {
 
164
                        Analogy empty = new Analogy ();
 
165
                        empty.question = Catalog.GetString ("There are no verbal analogies available.");
 
166
                        bool all_empty = true;
 
167
 
 
168
                        for (int i = 0; i < (int) Analogy.Type.Last; i++)
 
169
                        {
 
170
                                if (analogies_arrays[i].Count > 0) {
 
171
                                        all_empty = false;
 
172
                                        break;  
 
173
                                }
 
174
                        }
 
175
 
 
176
                        if (all_empty == true)
 
177
                                analogies_arrays[0].Add (0, empty);
 
178
                }
 
179
        }
 
180
}