"vscode:/vscode.git/clone" did not exist on "33b16ad178573e54eba44999c5af4740b57affd1"
Dictionary.h 1.66 KB
Newer Older
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
/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT-style license found in
 * https://github.com/flashlight/flashlight/blob/d385b2150872fd7bf106601475d8719a703fe9ee/LICENSE
 */

#pragma once

#include <istream>
#include <string>
#include <unordered_map>
#include <vector>

namespace torchaudio {
namespace lib {
namespace text {
// A simple dictionary class which holds a bidirectional map
// entry (strings) <--> integer indices. Not thread-safe !
class Dictionary {
 public:
  // Creates an empty dictionary
  Dictionary() {}

  explicit Dictionary(std::istream& stream);

  explicit Dictionary(const std::string& filename);

29
30
  explicit Dictionary(const std::vector<std::string>& tkns);

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
  size_t entrySize() const;

  size_t indexSize() const;

  void addEntry(const std::string& entry, int idx);

  void addEntry(const std::string& entry);

  std::string getEntry(int idx) const;

  void setDefaultIndex(int idx);

  int getIndex(const std::string& entry) const;

  bool contains(const std::string& entry) const;

  // checks if all the indices are contiguous
  bool isContiguous() const;

  std::vector<int> mapEntriesToIndices(
      const std::vector<std::string>& entries) const;

  std::vector<std::string> mapIndicesToEntries(
      const std::vector<int>& indices) const;

 private:
  // Creates a dictionary from an input stream
  void createFromStream(std::istream& stream);

  std::unordered_map<std::string, int> entry2idx_;
  std::unordered_map<int, std::string> idx2entry_;
  int defaultIndex_ = -1;
};

typedef std::unordered_map<int, Dictionary> DictionaryMap;
} // namespace text
} // namespace lib
} // namespace torchaudio