Class: Nlg::Paragraph

Inherits:
Object
  • Object
show all
Defined in:
lib/nlg/paragraph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset, defaults) ⇒ Paragraph

The parameters, dataset and defaults are both hashes. For example:

defaults = { “tense” => “present”,

"verb" => "have", 
"pronoun" => "it",
"voice" => "active",
"aspect" => "habitual",
"preposition" => "of" }

dataset =

 "objects"=>
   { "population"=>
       {"value"=>"57841284790", 
        "specifications"=>{}, 
     "rainfall"=>
       
        "specifications"=>{"complement"=>"mm"}, 
     "high temperature"=>
       
        "specifications"=>{"complement"=>"°C", 
        "conjugated_with"=>["low temperature"]}, 
     "low temperature"=>
       
        "specifications"=>{"complement"=>"°C", 
        "conjugated_with"=>["high temperature", "low temperature", "rainfall"]}, 
     "forts"=>
       "lohgad"], 
        "specifications"=>{"preposition"=>"named", 
        "conjugated_with"=>["high temperature", "low temperature", "rainfall"]}
   }
}

method to set defaults, build_conjugations and initialize other attributes



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nlg/paragraph.rb', line 39

def initialize(dataset, defaults)
  @subject = dataset['subject']
  @objects = {}
  @conjugations = {}
  @defaults = defaults
  @sentences = []

  # Iterating through all the objects and building conjugations.  
  dataset['objects'].each do |object_type, object_details|
    # A check to allow objects to be created with "conjugated" => false only if not already
    # present. build_conjugations method called which recursively builds the conjugations.
    unless @objects.has_key?(object_type)
      sentence_object = SentenceObject.new(object_type, object_details, "conjugated" => false)
      @objects[object_type] = sentence_object
      build_conjugations(object_type, dataset['objects']) 
    end 
  end
 
end

Instance Attribute Details

#datasetObject

Returns the value of attribute dataset.



4
5
6
# File 'lib/nlg/paragraph.rb', line 4

def dataset
  @dataset
end

#defaultsObject

Returns the value of attribute defaults.



4
5
6
# File 'lib/nlg/paragraph.rb', line 4

def defaults
  @defaults
end

#sentencesObject

Returns the value of attribute sentences.



4
5
6
# File 'lib/nlg/paragraph.rb', line 4

def sentences
  @sentences
end

Instance Method Details

#buildObject



59
60
61
62
63
64
65
66
# File 'lib/nlg/paragraph.rb', line 59

def build
  puts @conjugations.inspect, @objects.inspect
  # Paragraph generation by generating individual sentences.   
  @objects.each do |object_type, sentence_object|
    sentence = Sentence.new(:subject => @subject, :specifications => @defaults.merge(sentence_object.specifications))
    @sentences << sentence.build(object_type, sentence_object)
  end
end

#build_conjugations(object_type, objects, parent_object = nil) ⇒ Object

Method called internally in initialize to build conjugations among sentences of a paragraph. The example dataset will give the conjugations,

@conjugations ={"population"=>[], "forts"=>["low temperature", "rainfall", "high temperature"]}


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/nlg/paragraph.rb', line 72

def build_conjugations(object_type, objects, parent_object = nil)
  object_details = objects[object_type]
  
  # Condition for setting the parent_object to be passed recursively
  parent_object = object_type unless parent_object
 
  # Creating the @conjugations[parent_object] array recursively to finally
  # build the conjugations.  
  conjugated_with = object_details['conjugated_with']
  
  unless conjugated_with.nil? or conjugated_with.empty?
    conjugated_with.each do |conjugated_object|
      if objects.has_key?(conjugated_object)
       
        # Creating object with "conjugated" => true if its present in "conjugated_with" array. 
        unless @objects.has_key?(conjugated_object)
          sentence_object = SentenceObject.new(conjugated_object, objects[conjugated_object], "conjugated" => true)
          @objects[conjugated_object] = sentence_object
        end
        
        # Creating array if does not already exist.
        @conjugations[parent_object] ||= []
        
        # Checking if currently iterating object included in the array already/is the parent_object itself.
        unless @conjugations[parent_object].include?(conjugated_object) or conjugated_object == parent_object

          # If an already parsed object with "conjugated" = false comes as a conjugation
          # to the object being parsed.
          if @objects[conjugated_object].conjugated == false      
            @objects[conjugated_object].conjugated = true
            @conjugations[parent_object] = @conjugations[parent_object].concat(@conjugations[conjugated_object]).uniq
            @conjugations.delete(conjugated_object)
          end
          
          # Appending to the conjugations array for the parent_object.
          @conjugations[parent_object] << conjugated_object
  
          # recursive call for building conjugations for a parent_object
          build_conjugations(conjugated_object, objects, parent_object)
        end
      else
        raise NlgException.new "No object named #{conjugation_object}"
      end
    end
  else 
   
    # adding independent sentences which have no conjugations 
    @conjugations[object_type] = [] if @objects[object_type].conjugated == false
  end

end