Welcome to mirror list, hosted at ThFree Co, Russian Federation.

openlr_sample.cpp « openlr - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68efa6f9ee960c949188f16cfcac462d4ae0c1e5 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "openlr/openlr_sample.hpp"

#include "indexer/index.hpp"

#include "base/string_utils.hpp"

#include <cerrno>
#include <cstring>
#include <fstream>

namespace
{
void ParseMWMSegments(std::string const & line, uint32_t const lineNumber,
                      std::vector<openlr::SampleItem::MWMSegment> & segments, Index const & index)
{
  std::vector<string> parts;
  strings::ParseCSVRow(line, '=', parts);

  for (auto const & seg : parts)
  {
    std::vector<string> segParts;
    strings::ParseCSVRow(seg, '-', segParts);
    CHECK_EQUAL(segParts.size(), 5, ());

    auto const mwmId = index.GetMwmIdByCountryFile(platform::CountryFile(segParts[0]));

    uint32_t featureIndex;
    if (!strings::to_uint(segParts[1], featureIndex))
      MYTHROW(openlr::SamplePoolLoadError, ("Can't parse MWMSegment", seg, "line:", lineNumber));

    uint32_t segId;
    if (!strings::to_uint(segParts[2], segId))
      MYTHROW(openlr::SamplePoolLoadError, ("Can't parse MWMSegment", seg, "line:", lineNumber));

    bool const isForward = (segParts[3] == "fwd");
    double length = 0;
    if (!strings::to_double(segParts[4], length))
      MYTHROW(openlr::SamplePoolLoadError, ("Can't parse MWMSegment", seg, "line:", lineNumber));

    segments.push_back({FeatureID(mwmId, featureIndex), segId, isForward, length});
  }
}

void ParseSampleItem(std::string const & line, uint32_t const lineNumber, openlr::SampleItem & item,
                     Index const & index)
{
  std::vector<string> parts;
  strings::ParseCSVRow(line, '\t', parts);
  CHECK_GREATER_OR_EQUAL(parts.size(), 2, ());
  CHECK_LESS_OR_EQUAL(parts.size(), 3, ());

  auto nextFieldIndex = 0;
  if (parts.size() == 3)
  {
    item.m_evaluation = openlr::ParseItemEvaluation(parts[nextFieldIndex]);
    ++nextFieldIndex;
  }
  else
  {
    item.m_evaluation = openlr::ItemEvaluation::Unevaluated;
  }

  if (!strings::to_uint(parts[nextFieldIndex], item.m_partnerSegmentId.Get()))
  {
    MYTHROW(openlr::SamplePoolLoadError, ("Error: can't parse field", nextFieldIndex,
                                          "(number expected) in line:", lineNumber));
  }
  ++nextFieldIndex;

  ParseMWMSegments(parts[nextFieldIndex], lineNumber, item.m_segments, index);
}
}  // namepsace

namespace openlr
{
ItemEvaluation ParseItemEvaluation(std::string const & s)
{
  if (s == "Unevaluated")
    return openlr::ItemEvaluation::Unevaluated;

  if (s == "Positive")
    return openlr::ItemEvaluation::Positive;

  if (s == "Negative")
    return openlr::ItemEvaluation::Negative;

  if (s == "RelPositive")
    return openlr::ItemEvaluation::RelPositive;

  if (s == "RelNegative")
    return openlr::ItemEvaluation::RelNegative;

  if (s == "Ignore")
    return openlr::ItemEvaluation::Ignore;

  return openlr::ItemEvaluation::NotAValue;
}

std::string ToString(ItemEvaluation const e)
{
  switch (e)
  {
  case openlr::ItemEvaluation::Unevaluated: return "Unevaluated";
  case openlr::ItemEvaluation::Positive: return "Positive";
  case openlr::ItemEvaluation::Negative: return "Negative";
  case openlr::ItemEvaluation::RelPositive: return "RelPositive";
  case openlr::ItemEvaluation::RelNegative: return "RelNegative";
  case openlr::ItemEvaluation::Ignore: return "Ignore";
  default: return "NotAValue";
  }
}

SamplePool LoadSamplePool(std::string const & fileName, Index const & index)
{
  std::ifstream sample(fileName);
  if (!sample.is_open())
    MYTHROW(SamplePoolLoadError, ("Can't read from file", fileName, strerror(errno)));

  SamplePool pool;
  for (struct {uint32_t lineNumber = 0; string line; } st; getline(sample, st.line); ++st.lineNumber)
  {
    SampleItem item = SampleItem::Uninitialized();
    ParseSampleItem(st.line, st.lineNumber, item, index);
    pool.push_back(item);
  }

  return pool;
}

void SaveSamplePool(std::string const & fileName, SamplePool const & sample,
                    bool const saveEvaluation)
{
  LOG(LDEBUG, ("Saving sample to file:", fileName));
  std::ofstream out(fileName);
  if (!out.is_open())
    MYTHROW(SamplePoolSaveError, ("Can't write to file", fileName, strerror(errno)));
  out << std::fixed;  // Avoid scientific notation cause '-' is used as fields separator.
  for (auto const & item : sample)
  {
    if (saveEvaluation)
      out << ToString(item.m_evaluation) << '\t';

    out << item.m_partnerSegmentId.Get() << '\t';

    for (auto it = begin(item.m_segments); it != end(item.m_segments); ++it)
    {
      auto const & fid = it->m_fid;
      out << fid.m_mwmId.GetInfo()->GetCountryName() << '-'
          << fid.m_index << '-' << it->m_segId
          << '-' << (it->m_isForward ? "fwd" : "bwd")
          << '-' << it->m_length;

      if (next(it) != end(item.m_segments))
        out << '=';
    }
    out << endl;
  }
  if (out.fail())
    MYTHROW(SamplePoolSaveError, ("An error occured while writing file", fileName, strerror(errno)));
}
}  // namespace openlr