root/trunk/lib/fm/linguistics.rb

Revision 285, 3.6 kB (checked in by ged, 4 months ago)
  • More build system updates
  • Cleanup of some documentation headers
  • More spec conversion/cleanup.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Rev Author URL Id
Line 
1#!/usr/bin/ruby
2
3require 'linguistics'
4
5require 'fm'
6require 'fm/logger'
7
8#
9# A mixin which adds natural language methods to including classes.
10#
11# == Subversion ID
12#
13# $Id$
14#
15# == Authors
16#
17# * Michael Granger <ged@FaerieMUD.org>
18#
19# :include: LICENSE
20#
21#---
22#
23# Please see the file LICENSE for licensing details.
24#
25module FaerieMUD::Linguistics
26
27        # SVN Revision
28        SVNRev = %q$Rev$
29
30        # SVN Id
31        SVNId = %q$Id$
32
33        # Load the English-language modules
34        Linguistics::use( :en, :installProxy => true )
35
36
37        ### Anything that mixes this module gets the Linguistics default languages.
38        def self::included( mod )
39                FaerieMUD::Logger.debug "Installing linguistic functions in %s" % [mod.name]
40                mod.extend( ::Linguistics )
41        end
42
43
44        ###############
45        module_function
46        ###############
47
48        ### Return a simple noun for the given +object+. If the object responds to
49        ### the #noun method, the return value from that will be used -- otherwise
50        ### the result will be derived from the name of the object's class.
51        def make_noun( object )
52                noun = nil
53                if object.respond_to?( :noun )
54                        noun = object.noun
55                else
56                        noun = guess_object_noun( object )
57                end
58
59                FaerieMUD::Logger.debug "Making a noun out of %p: %p" % [ object, noun ]
60                return noun
61        end
62
63
64        ### Return a noun phrase describing the specified +objects+.
65        def make_noun_phrase( *objects )
66                objects.flatten.conjunction {|obj| make_noun(obj) }
67        end
68
69
70        ### Guess and return an appropriate noun for the given +object+ based on the
71        ### name of its class.
72        def guess_object_noun( object )
73                object.class.name.
74                        sub( /.*::/, '' ).
75                        en.camel_case_to_english
76        end
77
78
79        ### This is a really simplistic version of what should really happen.
80        ### Ideally, verbs will be able to respond with the appropriate word form of
81        ### themselves. However, the NLG engine should be able to pervert the canonical
82        ### form to achieve tainted or ecstatic views, etc.
83
84        ### Conjugate the given +verb+ (a FaerieMUD::Verb object) and return it.
85        def conjugate_verb( verb, subject=nil )
86                if /(\w+)Verb/.match( verb.class.name )
87                        $1.downcase
88                else
89                        verb.class.name.downcase
90                end
91        end
92
93
94        ### Linguistic sentence class -- instances of this class are used to
95        ### encapsulate single-sentence chunks of linguistic content in the
96        ### natural-language system.
97        class Sentence
98                include FaerieMUD::Linguistics
99
100                ### Create the sentence. Can be constructed either with an explicit
101                ### subject, verb, and optional direct object, or with a
102                ### FaerieMUD::Event that will provide these parts.
103                def initialize( *args )
104                        if args.length == 1
105                                init_from_event( args.first )
106                        else
107                                init_from_parts( *args )
108                        end
109                end
110
111
112                # The subject of the sentence (a FaerieMUD::GameObject object)
113                attr_reader :subject
114
115                # The FaerieMUD;;Verb instance associated with the sentence
116                attr_reader :verb
117
118                # The FaerieMUD::GameObject which is the recipient of the action in the
119                # sentence, if any.
120                attr_reader :direct_object
121
122
123                ### Return the sentence as a String
124                def to_s
125                        parts = [
126                                make_noun_phrase( @subject ),
127                                @verb.inflect_for( @subject )
128                        ]
129
130                        parts << make_noun_phrase( @direct_object ) if @direct_object
131
132                        return parts.join( " " ) + "."
133                end
134
135
136                #########
137                protected
138                #########
139
140                ### Set the members of the sentence from the parts of the given +event+
141                ### (a FaerieMUD::Event object).
142                def init_from_event( event )
143                        init_from_parts( event.instigator, event.verb, event.target )
144                end
145
146                ### Set the members of the sentence from the given +subject+, +verb+,
147                ### and +direct_object+.
148                def init_from_parts( subject, verb, direct_object=nil )
149                        @subject = subject
150                        @verb = verb
151                        @direct_object = direct_object
152                end
153
154
155        end
156
157end # module FaerieMUD::Linguistics
158
159
160__END__
Note: See TracBrowser for help on using the browser.