summary refs log tree commit diff stats
path: root/generator/form.cpp
blob: 6be9d478163d7564d5224cac6112c322e0cd0a08 (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
#include "form.h"
#include <algorithm>
#include <list>
#include "database.h"
#include "field.h"
#include "pronunciation.h"

namespace verbly {
  namespace generator {

    int form::nextId_ = 0;

    form::form(std::string text) :
      id_(nextId_++),
      text_(text),
      complexity_(std::count(std::begin(text), std::end(text), ' ') + 1),
      proper_(std::any_of(std::begin(text), std::end(text), std::isupper))
    {
    }

    void form::addPronunciation(const pronunciation& p)
    {
      pronunciations_.insert(&p);
    }

    database& operator<<(database& db, const form& arg)
    {
      // Serialize the form first.
      {
        std::list<field> fields;
        fields.emplace_back("form_id", arg.getId());
        fields.emplace_back("form", arg.getText());
        fields.emplace_back("complexity", arg.getComplexity());
        fields.emplace_back("proper", arg.isProper());

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

      // Then, serialize the form/pronunciation relationship.
      for (const pronunciation* p : arg.getPronunciations())
      {
        std::list<field> fields;
        fields.emplace_back("form_id", arg.getId());
        fields.emplace_back("pronunciation_id", p->getId());

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

      return db;
    }

  };
};