summary refs log tree commit diff stats
path: root/generator/group.cpp
blob: aa28d429bbf28ca82a353753968366bb7d2f4e4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "group.h"
#include <stdexcept>
#include <list>
#include <json.hpp>
#include "database.h"
#include "field.h"
#include "frame.h"

namespace verbly {
  namespace generator {

    int group::nextId_ = 0;

    group::group() : id_(nextId_++)
    {
    }

    group::group(const group& parent) :
      id_(nextId_++),
      roles_(parent.roles_),
      roleNames_(parent.roleNames_)
    {
      for (const frame& f : parent.frames_)
      {
        frames_.push_back(frame::duplicate(f));
      }
    }

    void group::addRole(role r)
    {
      std::string name = r.getName();
      roles_[name] = std::move(r);
      roleNames_.insert(std::move(name));
    }

    void group::addFrame(frame f)
    {
      frames_.push_back(std::move(f));
    }

    bool group::hasRole(std::string name) const
    {
      // Rarely, a noun phrase part may use a role that is not defined in the
      // group. See confess-37.10 "NP V NP ADJ".
      return (roles_.count(name) == 1);
    }

    const role& group::getRole(std::string name) const
    {
      return roles_.at(name);
    }

    database& operator<<(database& db, const group& arg)
    {
      // Serialize each frame
      for (const frame& f : arg.getFrames())
      {
        // First, serialize the group/frame relationship
        {
          std::list<field> fields;

          fields.emplace_back("frame_id", f.getId());
          fields.emplace_back("group_id", arg.getId());
          fields.emplace_back("length", f.getLength());

          db.insertIntoTable("frames", std::move(fields));
        }

        // Then, serialize the frame parts in the context of the group
        for (int partIndex = 0; partIndex < f.getLength(); partIndex++)
        {
          const part& p = f[partIndex];

          std::list<field> fields;
          fields.emplace_back("part_id", p.getId());
          fields.emplace_back("frame_id", f.getId());
          fields.emplace_back("part_index", partIndex);
          fields.emplace_back("type", static_cast<int>(p.getType()));

          switch (p.getType())
          {
            case part::type::noun_phrase:
            {
              fields.emplace_back("role", p.getNounRole());

              selrestr partSelrestr;
              if (p.getNounSelrestrs().getType() != selrestr::type::empty)
              {
                partSelrestr = p.getNounSelrestrs();
              } else if (arg.hasRole(p.getNounRole()))
              {
                partSelrestr = arg.getRole(p.getNounRole()).getSelrestrs();
              }

              fields.emplace_back("selrestrs", partSelrestr.toJson().dump());

              // Short interlude to serialize the synrestrs
              for (const std::string& s : p.getNounSynrestrs())
              {
                std::list<field> synrestrFields;

                synrestrFields.emplace_back("part_id", p.getId());
                synrestrFields.emplace_back("synrestr", s);

                db.insertIntoTable("synrestrs", std::move(synrestrFields));
              }

              break;
            }

            case part::type::preposition:
            {
              fields.emplace_back("prepositions", nlohmann::json(p.getPrepositionChoices()).dump());
              fields.emplace_back("preposition_literality", p.isPrepositionLiteral() ? 1 : 0);

              break;
            }

            case part::type::literal:
            {
              fields.emplace_back("literal_value", p.getLiteralValue());

              break;
            }

            case part::type::verb:
            case part::type::adjective:
            case part::type::adverb:
            {
              break;
            }

            case part::type::invalid:
            {
              // Invalid parts should not be serialized.
              assert(false);

              break;
            }
          }

          db.insertIntoTable("parts", std::move(fields));
        }
      }

      return db;
    }

  };
};