~iwarford/do-plugins/fart-plugin-fwiw

« back to all changes in this revision

Viewing changes to Wordnet/src/WordnetAction.cs

  • Committer: Jason Jones
  • Date: 2008-12-24 04:45:02 UTC
  • mfrom: (335.1.9 do-plugins)
  • Revision ID: jasonedwardjones@gmail.com-20081224044502-ra56ym06cp1iqs7t
MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* WordnetAction.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this source distribution.
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 * WordnetAction is just a slight modification of DefineWordAction, to use
 
20
 * Wordnet instead of the default dictionary.
 
21
 */
 
22
 
 
23
using System;
 
24
using System.Collections.Generic;
 
25
using System.Text.RegularExpressions;
 
26
using Do.Universe;
 
27
 
 
28
namespace Simulacra
 
29
{
 
30
        /// <summary>
 
31
        /// Given an ITextItem, WordnetAcion will look up the Text
 
32
        /// contents of the ITextItem using the gnome-dictionary.
 
33
        /// </summary>
 
34
        public class WordnetAction : Act
 
35
        {
 
36
                /// <summary>
 
37
                /// Should match those and only those strings that can be
 
38
                /// looked up in a dictionary.
 
39
                /// YES: "war", "peace", "hoi polloi"
 
40
                /// NO: "war9", "2 + 4", "___1337__"
 
41
                /// </summary>
 
42
                const string wordPattern = @"^([^\W0-9_]+([ -][^\W0-9_]+)?)$";
 
43
 
 
44
                Regex wordRegex;
 
45
 
 
46
                public WordnetAction ()
 
47
                {
 
48
                        wordRegex = new Regex (wordPattern, RegexOptions.Compiled);
 
49
                }
 
50
 
 
51
                public override string Name {
 
52
                        get { return "Wordnet"; }
 
53
                }
 
54
 
 
55
                public override string Description
 
56
                {
 
57
                        get { return "Get the Wordnet overview for the given word."; }
 
58
                }
 
59
 
 
60
                public override string Icon
 
61
                {
 
62
                        get { return "accessories-dictionary"; }
 
63
                }
 
64
 
 
65
                public override IEnumerable<Type> SupportedItemTypes
 
66
                {
 
67
                        get {
 
68
                                return new Type[] {
 
69
                                        typeof (ITextItem),
 
70
                                };
 
71
                        }
 
72
                }
 
73
 
 
74
                /// <summary>
 
75
                /// Use wordRegex to determine whether item is definable.
 
76
                /// </summary>
 
77
                /// <param name="item">
 
78
                /// A <see cref="IItem"/> to define.
 
79
                /// </param>
 
80
                /// <returns>
 
81
                /// A <see cref="System.Boolean"/> indicating whether or not IITem
 
82
                /// can be defined.
 
83
                /// </returns>
 
84
                public override bool SupportsItem (Item item)
 
85
                {
 
86
                        string word;
 
87
 
 
88
                        word = null;
 
89
                        if (item is ITextItem) {
 
90
                                word = (item as ITextItem).Text;
 
91
                        }
 
92
                        return !string.IsNullOrEmpty (word) && wordRegex.IsMatch (word);
 
93
                }
 
94
 
 
95
                public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
 
96
                {
 
97
                        string word, cmd;
 
98
                        foreach (Item item in items) {
 
99
                                if (item is ITextItem) {
 
100
                                        word = (item as ITextItem).Text;
 
101
                                } else {
 
102
                                        continue;
 
103
                                }
 
104
                                cmd = string.Format ("wnb \"{0}\"", word);
 
105
                                System.Diagnostics.Process.Start (cmd);
 
106
                        }
 
107
                        return null;
 
108
                }
 
109
        }
 
110
}