summary refs log tree commit diff stats
path: root/generator/frame.cpp
blob: f75e3ba9a04cede017cd710e15a7453801ae730b (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
#include "frame.h"
#include "database.h"
#include "field.h"

namespace verbly {
  namespace generator {

    int frame::nextId_ = 0;

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

    void frame::push_back(part fp)
    {
      parts_.push_back(std::move(fp));
    }

    database& operator<<(database& db, const frame& arg)
    {
      std::list<field> fields;
      fields.emplace_back("frame_id", arg.getId());

      nlohmann::json jsonParts;
      for (const part& p : arg)
      {
        nlohmann::json jsonPart;
        jsonPart["type"] = static_cast<int>(p.getType());

        switch (p.getType())
        {
          case part::type::noun_phrase:
          {
            jsonPart["role"] = p.getNounRole();
            jsonPart["selrestrs"] = p.getNounSelrestrs().toJson();
            jsonPart["synrestrs"] = p.getNounSynrestrs();

            break;
          }

          case part::type::preposition:
          {
            jsonPart["choices"] = p.getPrepositionChoices();
            jsonPart["literal"] = p.isPrepositionLiteral();

            break;
          }

          case part::type::literal:
          {
            jsonPart["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;
          }
        }

        jsonParts.push_back(std::move(jsonPart));
      }

      fields.emplace_back("data", jsonParts.dump());

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

      return db;
    }

  };
};