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

namespace verbly {
  
  const object lemma::objectType = object::lemma;
  
  const std::list<std::string> lemma::select = {"lemma_id"};
  
  const field lemma::id = field::integerField(object::lemma, "lemma_id");
  
  const field lemma::word = field::joinField(object::lemma, "lemma_id", object::word);
  
  const field lemma::formJoin = field::joinField(object::lemma, "form_id", object::form);
  const field lemma::inflectionCategory = field::integerField(object::lemma, "category");
  
  filter operator%=(lemma::inflection_field check, filter joinCondition)
  {
    return (lemma::formJoin %= joinCondition) && (lemma::inflectionCategory == check.getCategory());
  }
  
  lemma::lemma(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true)
  {
    id_ = sqlite3_column_int(row, 0);
  }
  
  const form& lemma::getBaseForm() const
  {
    if (!valid_)
    {
      throw std::domain_error("Bad access to uninitialized lemma");
    }
    
    if (!forms_.count(inflection::base))
    {
      initializeForm(inflection::base);
    }
    
    return forms_.at(inflection::base).front();
  }
  
  bool lemma::hasInflection(inflection category) const
  {
    return !getInflections(category).empty();
  }
  
  const std::vector<form>& lemma::getInflections(inflection category) const
  {
    if (!valid_)
    {
      throw std::domain_error("Bad access to uninitialized lemma");
    }
    
    if (!forms_.count(category))
    {
      initializeForm(category);
    }
    
    return forms_.at(category);
  }
  
  void lemma::initializeForm(inflection infl) const
  {
    forms_[infl] = db_->forms(form::lemma(infl) %= *this, false, -1).all();
  }
  
};