summary refs log tree commit diff stats
path: root/lib/form.cpp
blob: eeb940aac9809b5cf5f3312418988f969c96dcee (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
#include "form.h"
#include <algorithm>
#include "filter.h"
#include "database.h"
#include "query.h"

namespace verbly {

  const object form::objectType = object::form;

  const std::list<std::string> form::select = {"form_id", "form", "complexity", "proper", "length", "frequency"};

  const field form::id = field::integerField(object::form, "form_id");
  const field form::text = field::stringField(object::form, "form");
  const field form::complexity = field::integerField(object::form, "complexity");
  const field form::proper = field::booleanField(object::form, "proper");
  const field form::length = field::integerField(object::form, "length");
  const field form::frequency = field::integerField(object::form, "frequency");

  const field form::pronunciations = field::joinThrough(object::form, "form_id", object::pronunciation, "forms_pronunciations", "pronunciation_id");

  const field form::anagrams = field::joinField(object::form, "anagram_set_id", object::form);
  const field form::antogram = field::selfJoin(object::form, "reverse_form_id", "form_id");

  const field form::merographs = field::selfJoin(object::form, "form_id", "merography", "merograph_id", "holograph_id");
  const field form::holographs = field::selfJoin(object::form, "form_id", "merography", "holograph_id", "merograph_id");

  field form::words(inflection category)
  {
    return field::joinThroughWhere(object::form, "form_id", object::word, "lemmas_forms", "lemma_id", "category", static_cast<int>(category));
  }

  form::form(const database& db, hatkirby::row row) : valid_(true)
  {
    id_ = std::get<int>(row[0]);
    text_ = std::get<std::string>(row[1]);
    complexity_ = std::get<int>(row[2]);
    proper_ = (std::get<int>(row[3]) == 1);
    length_ = std::get<int>(row[4]);

    if (!std::holds_alternative<std::nullptr_t>(row[5]))
    {
      hasFreq_ = true;
      frequency_ = std::get<int>(row[5]);
    }

    pronunciations_ = db.pronunciations(*this, pronunciation::id, -1).all();
  }

  bool form::startsWithVowelSound() const
  {
    if (!valid_)
    {
      throw std::domain_error("Bad access to uninitialized form");
    }

    if (!pronunciations_.empty())
    {
      return std::any_of(
        std::begin(pronunciations_),
        std::end(pronunciations_),
        [] (const pronunciation& p) {
          return p.getPhonemes().front().find_first_of("012") !=
            std::string::npos;
        });
    } else {
      // If the word is not in CMUDICT, fall back to checking whether the first
      // letter is a vowel. Not perfect but will work in most cases.
      char ch = std::tolower(text_.front());
      return (ch == 'a') ||
             (ch == 'e') ||
             (ch == 'i') ||
             (ch == 'o') ||
             (ch == 'u');
    }
  }

};