summaryrefslogtreecommitdiff
path: root/utils/fdict.h
blob: 9c8d7cde5889a7ce9f5d3e63797340910051c805 (plain)
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
#ifndef _FDICT_H_
#define _FDICT_H_

#include "config.h"

#include <iostream>
#include <string>
#include <vector>
#include "dict.h"

#ifdef HAVE_CMPH
#include "perfect_hash.h"
#include "string_to.h"
#endif

struct FD {
  // once the FD is frozen, new features not already in the
  // dictionary will return 0
  static void Freeze() {
    frozen_ = true;
  }
  static bool UsingPerfectHashFunction() {
#ifdef HAVE_CMPH
    return hash_;
#else
    return false;
#endif
  }
  static void EnableHash(const std::string& cmph_file) {
#ifdef HAVE_CMPH
    assert(dict_.max() == 0);  // dictionary must not have
                               // been added to
    hash_ = new PerfectHashFunction(cmph_file);
#endif
  }
>>>>>>> upstream/master
  static inline int NumFeats() {
#ifdef HAVE_CMPH
    if (hash_) return hash_->number_of_keys();
#endif
    return dict_.max() + 1;
  }
  static inline WordID Convert(const std::string& s) {
#ifdef HAVE_CMPH
    if (hash_) return (*hash_)(s);
#endif
    return dict_.Convert(s, frozen_);
  }
  static inline const std::string& Convert(const WordID& w) {
#ifdef HAVE_CMPH
    if (hash_) {
      static std::string tls;
      tls = to_string(w);
      return tls;
    }
#endif
    return dict_.Convert(w);
  }
  static std::string Convert(WordID const *i,WordID const* e);
  static std::string Convert(std::vector<WordID> const& v);

  // Escape any string to a form that can be used as the name
  // of a weight in a weights file
  static std::string Escape(const std::string& s);
  static Dict dict_;
 private:
  static bool frozen_;
#ifdef HAVE_CMPH
  static PerfectHashFunction* hash_;
#endif
};

#endif