summary refs log tree commit diff stats
path: root/lib/word.cpp
blob: 49e34a11fd5c921410ef2834c58d462b0e7fbe17 (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
#include "verbly.h"
#include <algorithm>

namespace verbly {
  
  rhyme::rhyme(std::string prerhyme, std::string phonemes) : _prerhyme(prerhyme), _rhyme(phonemes)
  {
    
  }
  
  std::string rhyme::get_prerhyme() const
  {
    return _prerhyme;
  }
  
  std::string rhyme::get_rhyme() const
  {
    return _rhyme;
  }
  
  bool rhyme::operator==(const rhyme& other) const
  {
    return std::tie(_prerhyme, _rhyme) == std::tie(other._prerhyme, other._rhyme);
  }
  
  word::word()
  {
    
  }
  
  word::word(const data& _data, int _id) : _data(&_data), _id(_id), _valid(true)
  {
    
  }
  
  std::list<rhyme> word::get_rhymes() const
  {
    assert(_valid == true);
    
    return rhymes;
  }
  
  bool word::starts_with_vowel_sound() const
  {
    assert(_valid == true);
    
    if (pronunciations.size() > 0)
    {
      return std::any_of(std::begin(pronunciations), std::end(pronunciations), [] (std::list<std::string> phonemes) {
        return (phonemes.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 = tolower(base_form().front());
      return (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u');
    }
  }
  
};