Merge pull request #194 from google/sr
Introduced new Filesystem API phase 2.
This commit is contained in:
Коммит
cd937f0747
|
@ -5,6 +5,7 @@ Makefile.in
|
||||||
/py-compile
|
/py-compile
|
||||||
/test-driver
|
/test-driver
|
||||||
/ylwrap
|
/ylwrap
|
||||||
|
/build
|
||||||
|
|
||||||
/autom4te.cache
|
/autom4te.cache
|
||||||
/autoscan.log
|
/autoscan.log
|
||||||
|
|
|
@ -64,12 +64,6 @@ static constexpr int64 kint64max = ((int64)(0x7FFFFFFFFFFFFFFF));
|
||||||
|
|
||||||
static constexpr uint32 kUnicodeError = 0xFFFD;
|
static constexpr uint32 kUnicodeError = 0xFFFD;
|
||||||
|
|
||||||
#ifdef OS_WIN
|
|
||||||
#define OUTPUT_MODE std::ios::binary | std::ios::out
|
|
||||||
#else
|
|
||||||
#define OUTPUT_MODE std::ios::out
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(OS_WIN) && defined(UNICODE) && defined(_UNICODE)
|
#if defined(OS_WIN) && defined(UNICODE) && defined(_UNICODE)
|
||||||
#define WPATH(path) (::sentencepiece::win32::Utf8ToWide(path).c_str())
|
#define WPATH(path) (::sentencepiece::win32::Utf8ToWide(path).c_str())
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -13,8 +13,16 @@
|
||||||
// limitations under the License.!
|
// limitations under the License.!
|
||||||
|
|
||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
#ifdef OS_WIN
|
||||||
|
#define OUTPUT_MODE std::ios::binary | std::ios::out
|
||||||
|
#else
|
||||||
|
#define OUTPUT_MODE std::ios::out
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace sentencepiece {
|
namespace sentencepiece {
|
||||||
namespace filesystem {
|
namespace filesystem {
|
||||||
|
|
||||||
|
@ -38,7 +46,15 @@ class PosixReadableFile : public ReadableFile {
|
||||||
return static_cast<bool>(std::getline(*is_, *line));
|
return static_cast<bool>(std::getline(*is_, *line));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ReadAll(std::string *line) { return true; }
|
bool ReadAll(std::string *line) {
|
||||||
|
if (is_ == &std::cin) {
|
||||||
|
LOG(ERROR) << "ReadAll is not supported for stdin.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
line->assign(std::istreambuf_iterator<char>(*is_),
|
||||||
|
std::istreambuf_iterator<char>());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
util::Status status_;
|
util::Status status_;
|
||||||
|
@ -72,7 +88,7 @@ class PosixWritableFile : public WritableFile {
|
||||||
private:
|
private:
|
||||||
util::Status status_;
|
util::Status status_;
|
||||||
std::ostream *os_;
|
std::ostream *os_;
|
||||||
}; // namespace filesystem
|
};
|
||||||
|
|
||||||
std::unique_ptr<ReadableFile> NewReadableFile(absl::string_view filename) {
|
std::unique_ptr<ReadableFile> NewReadableFile(absl::string_view filename) {
|
||||||
return port::MakeUnique<PosixReadableFile>(filename);
|
return port::MakeUnique<PosixReadableFile>(filename);
|
||||||
|
|
|
@ -43,13 +43,11 @@ SentencePieceProcessor::SentencePieceProcessor() {}
|
||||||
SentencePieceProcessor::~SentencePieceProcessor() {}
|
SentencePieceProcessor::~SentencePieceProcessor() {}
|
||||||
|
|
||||||
util::Status SentencePieceProcessor::Load(util::min_string_view filename) {
|
util::Status SentencePieceProcessor::Load(util::min_string_view filename) {
|
||||||
std::ifstream ifs(WPATH(filename.data()), std::ios::binary | std::ios::in);
|
auto input = filesystem::NewReadableFile(string_util::ToSV(filename));
|
||||||
if (!ifs) {
|
RETURN_IF_ERROR(input->status());
|
||||||
return util::StatusBuilder(util::error::NOT_FOUND)
|
std::string proto;
|
||||||
<< "\"" << filename.data() << "\": " << util::StrError(errno);
|
CHECK_OR_RETURN(input->ReadAll(&proto));
|
||||||
}
|
return LoadFromSerializedProto(proto);
|
||||||
|
|
||||||
return Load(&ifs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SentencePieceProcessor::LoadOrDie(util::min_string_view filename) {
|
void SentencePieceProcessor::LoadOrDie(util::min_string_view filename) {
|
||||||
|
@ -57,10 +55,9 @@ void SentencePieceProcessor::LoadOrDie(util::min_string_view filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
util::Status SentencePieceProcessor::Load(std::istream *is) {
|
util::Status SentencePieceProcessor::Load(std::istream *is) {
|
||||||
CHECK_OR_RETURN(is) << "input ifstream is null";
|
return util::StatusBuilder(util::error::UNIMPLEMENTED)
|
||||||
auto model_proto = port::MakeUnique<ModelProto>();
|
<< "std::stream API is deprecated. Use LoadFromSerializedProto() "
|
||||||
CHECK_OR_RETURN(model_proto->ParseFromIstream(is)) << "Model file is broken";
|
<< "to load model from any serialized blob object.";
|
||||||
return Load(std::move(model_proto));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
util::Status SentencePieceProcessor::Load(const ModelProto &model_proto) {
|
util::Status SentencePieceProcessor::Load(const ModelProto &model_proto) {
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <fstream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
Загрузка…
Ссылка в новой задаче