~psgenfan/do-plugins/WolframAlpha

« back to all changes in this revision

Viewing changes to Wordnet/src/WordnetAction.cs

  • Committer: Robert Dyer
  • Date: 2011-01-24 20:21:35 UTC
  • Revision ID: psybers@gmail.com-20110124202135-wbb5dv33f9xltbg5
fix wordnet - bug 70744

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
namespace Simulacra
29
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
 
        }
 
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 { return new Type[] { typeof (ITextItem) }; }
 
68
                }
 
69
 
 
70
                /// <summary>
 
71
                /// Use wordRegex to determine whether item is definable.
 
72
                /// </summary>
 
73
                /// <param name="item">
 
74
                /// A <see cref="IItem"/> to define.
 
75
                /// </param>
 
76
                /// <returns>
 
77
                /// A <see cref="System.Boolean"/> indicating whether or not IITem
 
78
                /// can be defined.
 
79
                /// </returns>
 
80
                public override bool SupportsItem (Item item)
 
81
                {
 
82
                        string word;
 
83
 
 
84
                        word = null;
 
85
                        if (item is ITextItem)
 
86
                                word = (item as ITextItem).Text;
 
87
                        
 
88
                        return !string.IsNullOrEmpty (word) && wordRegex.IsMatch (word);
 
89
                }
 
90
 
 
91
                public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
 
92
                {
 
93
                        foreach (Item item in items) {
 
94
                                if (!(item is ITextItem))
 
95
                                        continue;
 
96
                                
 
97
                                System.Diagnostics.Process.Start ("wnb", (item as ITextItem).Text);
 
98
                        }
 
99
                        
 
100
                        return null;
 
101
                }
 
102
        }
110
103
}