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

namespace verbly {
  namespace generator {

    int form::nextId_ = 0;

    form::form(std::string text, int anagram_set_id) :
      id_(nextId_++),
      text_(text),
      complexity_(std::count(std::begin(text), std::end(text), ' ') + 1),
      proper_(std::any_of(std::begin(text), std::end(text), ::isupper)),
      length_(text.length()),
      anagram_set_id_(anagram_set_id)
    {
    }

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

    hatkirby::database& operator<<(hatkirby::database& db, const form& arg)
    {
      // Serialize the form first.
      {
        std::list<hatkirby::column> fields = {
          { "form_id", arg.getId() },
          { "form", arg.getText() },
          { "complexity", arg.getComplexity() },
          { "proper", arg.isProper() },
          { "length", arg.getLength() },
          { "anagram_set_id", arg.getAnagramSetId() },
          { "reverse_form_id", arg.getReverseId() }
        };

        if (arg.getFrequency() > 0)
        {
          fields.emplace_back("frequency", arg.getFrequency());
        }

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

      // Then, serialize the form/pronunciation relationship.
      for (const pronunciation* p : arg.getPronunciations())
      {
        db.insertIntoTable(
          "forms_pronunciations",
          {
            { "form_id", arg.getId() },
            { "pronunciation_id", p->getId() }
          });
      }

      return db;
    }

  };
};