Skip to content

Commit

Permalink
Remove dependency on protocol buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
akihikodaki committed Jul 19, 2022
1 parent 51a2dff commit 50b0270
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 21 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cld3.find_language("здравствуйте") # => #<struct Struct::Result lang
### Prerequisites
* [Bundler](http://bundler.io/)
* C++ compiler
* [Protocol buffers](https://developers.google.com/protocol-buffers/)
* [Rake](https://ruby.github.io/rake/)
* [RubyGems](https://rubygems.org/)

Expand Down
13 changes: 3 additions & 10 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ ENV["RBS_TEST_TARGET"] = "CLD3::*"

# Copied from ext/cld3/ext/src/BUILD.gn
ext_name = FileList[
"feature_extractor.proto",
"sentence.proto",
"task_spec.proto",
"base.cc",
"base.h",
"casts.h",
Expand Down Expand Up @@ -92,6 +89,9 @@ int_path = FileList[
"README.md",
"Steepfile",
"cld3.gemspec",
"ext/cld3/cld_3/protos/feature_extractor.pb.h",
"ext/cld3/cld_3/protos/sentence.pb.h",
"ext/cld3/cld_3/protos/task_spec.pb.h",
"ext/cld3/extconf.rb",
"ext/cld3/libcld3.def",
"ext/cld3/nnet_language_identifier_c.cc",
Expand Down Expand Up @@ -131,14 +131,7 @@ end
desc "Prepare files for building gem and testing in intermediate directory"
task :prepare =>
ext_intermediate + int_intermediate << "intermediate/LICENSE_CLD3" do
rm_rf "intermediate/ext/cld3/cld_3/protos"
rm_rf "intermediate/ext/cld3/script_span"
rm_f "intermediate/ext/cld3/feature_extractor.pb.cc"
rm_f "intermediate/ext/cld3/feature_extractor.pb.h"
rm_f "intermediate/ext/cld3/sentence.pb.cc"
rm_f "intermediate/ext/cld3/sentence.pb.h"
rm_f "intermediate/ext/cld3/task_spec.pb.cc"
rm_f "intermediate/ext/cld3/task_spec.pb.h"
sh "cd intermediate && bundle config path vendor/bundle && bundle install"
end

Expand Down
100 changes: 100 additions & 0 deletions ext/cld3/cld_3/protos/feature_extractor.pb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Copyright 2022 Akihiko Odaki <akihiko.odaki@gmail.com>
All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef FEATURE_EXTRACTOR_PB_H_
#define FEATURE_EXTRACTOR_PB_H_

#include <cstdint>
#include <string>
#include <vector>

namespace chrome_lang_id {

class Parameter {
public:
const std::string& name() const { return name_; }
void set_name(std::string value) { name_ = std::move(value); }
const std::string& value() const { return value_; }
void set_value(std::string value) { value_ = std::move(value); }

private:
std::string name_;
std::string value_;
};

class FeatureFunctionDescriptor {
public:
const std::string& type() const { return type_; }

void set_type(std::string value) { type_ = std::move(value); }

const std::string& name() const { return name_; }

void set_name(std::string value) { name_ = std::move(value); }

bool has_argument() const { return true; }

std::int32_t argument() const { return argument_; }

void set_argument(int32_t value) { argument_ = value; }

int parameter_size() const { return parameter_.size(); }

const Parameter& parameter(int index) const { return parameter_[index]; }

Parameter* add_parameter() { return &parameter_.emplace_back(); }

int feature_size() const { return feature_.size(); }

FeatureFunctionDescriptor* mutable_feature(int index) {
return &feature_[index];
}

const FeatureFunctionDescriptor& feature(int index) const {
return feature_[index];
}

FeatureFunctionDescriptor* add_feature() { return &feature_.emplace_back(); }

private:
std::string type_;
std::string name_;
std::int32_t argument_;
std::vector<Parameter> parameter_;
std::vector<FeatureFunctionDescriptor> feature_;
};

class FeatureExtractorDescriptor {
public:
int feature_size() const { return feature_.size(); }

FeatureFunctionDescriptor* mutable_feature(int index) {
return &feature_[index];
}

const FeatureFunctionDescriptor& feature(int index) const {
return feature_[index];
}

FeatureFunctionDescriptor* add_feature() { return &feature_.emplace_back(); }

private:
std::vector<FeatureFunctionDescriptor> feature_;
};

}

#endif
35 changes: 35 additions & 0 deletions ext/cld3/cld_3/protos/sentence.pb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright 2022 Akihiko Odaki <akihiko.odaki@gmail.com>
All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef SENTENCE_PB_H_
#define SENTENCE_PB_H_

#include <string>

namespace chrome_lang_id {

class Sentence {
public:
const std::string& text() const { return text_; }
void set_text(std::string value) { text_ = std::move(value); }

private:
std::string text_;
};

}

#endif
106 changes: 106 additions & 0 deletions ext/cld3/cld_3/protos/task_spec.pb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* Copyright 2022 Akihiko Odaki <akihiko.odaki@gmail.com>
All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TASK_SPEC_PB_H_
#define TASK_SPEC_PB_H_

#include <string>
#include <vector>

namespace chrome_lang_id {

class TaskInput {
public:
class Part {
public:
const std::string& file_pattern() const { return file_pattern_; }

private:
std::string file_pattern_;
};

const std::string& name() const { return name_; }

void set_name(std::string value) { name_ = value; }

int file_format_size() const { return file_format_.size(); }

const std::string& file_format(int index) const {
return file_format_[index];
}

void add_file_format(std::string value) {
file_format_.push_back(std::move(value));
}

int record_format_size() const { return record_format_.size(); }

const std::string& record_format(int index) const {
return record_format_[index];
}

void add_record_format(std::string value) {
record_format_.push_back(std::move(value));
}

int part_size() const { return part_.size(); }
const Part& part(int index) const { return part_[index]; }

private:
std::string name_;
std::vector<std::string> file_format_;
std::vector<std::string> record_format_;
std::vector<Part> part_;
};

class TaskSpec {
public:
class Parameter {
public:
const std::string& name() const { return name_; }
void set_name(std::string value) { name_ = std::move(value); }
const std::string& value() const { return value_; }
void set_value(std::string value) { value_ = std::move(value); }

private:
std::string name_;
std::string value_;
};

int parameter_size() const { return parameter_.size(); }

Parameter* mutable_parameter(int index) { return &parameter_[index]; }

const Parameter& parameter(int index) const { return parameter_[index]; }

Parameter* add_parameter() { return &parameter_.emplace_back(); }

int input_size() const { return input_.size(); }

TaskInput* mutable_input(int index) { return &input_[index]; }

const TaskInput& input(int index) const { return input_[index]; }

TaskInput* add_input() { return &input_.emplace_back(); }

private:
std::vector<Parameter> parameter_;
std::vector<TaskInput> input_;
};

}

#endif
11 changes: 1 addition & 10 deletions ext/cld3/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,8 @@ def ln_fallback(source, destination)
end
end

# Check pkg-config first to inform the library is missing if so.
pkg_config("protobuf") or abort "Failed to locate protobuf"

FileUtils.mkdir_p("cld_3/protos")
FileUtils.mkdir_p("script_span")

[ "feature_extractor", "sentence", "task_spec" ].each {|name|
system "protoc", "#{name}.proto", "--cpp_out=.", exception: true
ln_fallback("#{name}.pb.h", "cld_3/protos/#{name}.pb.h")
}

[
"fixunicodevalue.h",
"generated_ulscript.h",
Expand All @@ -55,6 +46,6 @@ def ln_fallback(source, destination)
ln_fallback("#{name}", "script_span/#{name}")
}

$CXXFLAGS += " -fvisibility=hidden -std=c++11"
$CXXFLAGS += " -fvisibility=hidden -std=c++17"
$LIBRUBYARG = ""
create_makefile("libcld3")

0 comments on commit 50b0270

Please sign in to comment.