autocorrect-rb 2.1.1.beta1-x86_64-darwin → 2.1.2.beta4-x86_64-darwin

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 651a37d878b3b87f6570cbc9445d4668403cb72844b7ed1a6a5803587edad1ad
4
- data.tar.gz: 609f760e0f1b625214091f66f2d7764b05f3821ec4e84777546f6a940d0f9319
3
+ metadata.gz: ce9de2eae2914cae8b0c123aa2cd20bee5d43f39756af796a5f41bf899db67c7
4
+ data.tar.gz: c0e6608734f67a3e91d093351238b78e3c1fed9766d12f6acf559028c7ea1396
5
5
  SHA512:
6
- metadata.gz: e9b87ccf96ff85e541e1454f6a4ed7127aeec7e846b9ae922357d85b224744041318413e2366510adda552c65e98ead2f7e4719fab456a5cc3c60136ff8174e7
7
- data.tar.gz: b1b4d1e7ac8ba1863ca802976a527af3bde0cd06d4bc457a96bd7ba63d5966cb83ee2d4a34fc7317dd6df05d19ff9554d9c087c68658f02b571ce91cc86016da
6
+ metadata.gz: c68cb0111a38f58e3d453e847c5769d359c18e58d16ffae7fac8cb745c3c20bbf32aeb99f5887c552b4b9a88df6cbc7634b4752644a84973a3f08f7f80be09a4
7
+ data.tar.gz: c8023f1028743a7fbb745da579308cc0c1ddf31910a8d784129ae158f21cff4ebda05b04e133d517d1500693e1e6e2dc61fe5849381a214248f82019ed4e2c9a
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # AutoCorrect for Node.js
1
+ # AutoCorrect for Ruby
2
2
 
3
- The Native Node.js version of [AutoCorrect](https://github.com/huacnlee/autocorrect) built on [NAPI.RS](https://napi.rs).
3
+ The Native Ruby version of [AutoCorrect](https://github.com/huacnlee/autocorrect).
4
4
 
5
5
  - Rust - [autocorrect](https://github.com/huacnlee/autocorrect)
6
- - Ruby - [autocorrect-rb](https://github.com/huacnlee/autocorrect/tree/main/autocorrect-py)
6
+ - Ruby - [autocorrect-rb](https://github.com/huacnlee/autocorrect/tree/main/autocorrect-rb)
7
7
  - Go - [autocorrect-go](https://github.com/longbridgeapp/autocorrect)
8
8
  - Python - [autocorrect-py](https://github.com/huacnlee/autocorrect/tree/main/autocorrect-py)
9
9
  - Node.js - [autocorrect-node](https://github.com/huacnlee/autocorrect/tree/main/autocorrect-node)
@@ -12,28 +12,65 @@ The Native Node.js version of [AutoCorrect](https://github.com/huacnlee/autocorr
12
12
  ## Installation
13
13
 
14
14
  ```bash
15
- $ yarn add autocorrect-node
15
+ $ bundle add autocorrect-rb
16
16
  ```
17
17
 
18
18
  ## Usage
19
19
 
20
- ```js
21
- const autocorrect = require('autocorrect-node');
22
-
23
- const out = autocorrect.format('Hello你好.');
24
- console.log(out);
25
- // Hello 你好。
26
-
27
- out = autocorrect.formatFor("let title = 'Hello你好。'", 'js');
28
- // let title = 'Hello 你好。'
29
-
30
- const result = autocorrect.lintFor("let title = 'Hello你好。'", 'js');
31
- console.log(result);
32
- // {
33
- // filepath: 'js',
34
- // lines: [
35
- // { l: 1, c: 13, new: "'Hello 你好。'", old: "'Hello你好。'", severity: 1 }
36
- // ],
37
- // error: ''
38
- // }
20
+ ```rb
21
+ require('autocorrect-rb');
22
+
23
+ out = AutoCorrect.format('Hello你好.')
24
+ puts out
25
+ # Hello 你好。
26
+
27
+ out = AutoCorrect.format_for("title = 'Hello你好。'", 'rb')
28
+ puts out
29
+ # title = 'Hello 你好。'
30
+
31
+ result = AutoCorrect.lint_lor("title = 'Hello你好。'", 'rb')
32
+ puts result
33
+ # {
34
+ # filepath: 'rb',
35
+ # lines: [
36
+ # { l: 1, c: 13, new: "'Hello 你好。'", old: "'Hello你好。'", severity: 1 }
37
+ # ],
38
+ # error: ''
39
+ # }
40
+ ```
41
+
42
+ ## Benchmarks
43
+
44
+ 🎊 Rust version is 3x faster than the Ruby (pure) version.
45
+
46
+ > NOTE: In this case Rust version has more complex rules.
47
+
48
+ ### Rust version
49
+
50
+ ```bash
51
+ Warming up --------------------------------------
52
+ format 50 chars 11.348k i/100ms
53
+ format 100 chars 6.033k i/100ms
54
+ format 400 chars 1.772k i/100ms
55
+ format_html 545.000 i/100ms
56
+ Calculating -------------------------------------
57
+ format 50 chars 111.904k (± 3.1%) i/s - 567.400k in 5.075674s
58
+ format 100 chars 58.684k (± 2.1%) i/s - 295.617k in 5.039837s
59
+ format 400 chars 17.266k (± 2.9%) i/s - 86.828k in 5.033234s
60
+ format_html 5.448k (± 1.5%) i/s - 27.250k in 5.002853s
61
+ ```
62
+
63
+ ### Pure [Ruby version](https://rubygems.org/gems/auto-correct/versions/1.0.0) result:
64
+
65
+ ```bash
66
+ Warming up --------------------------------------
67
+ format 50 chars 3.167k i/100ms
68
+ format 100 chars 1.588k i/100ms
69
+ format 400 chars 496.000 i/100ms
70
+ format_html 166.000 i/100ms
71
+ Calculating -------------------------------------
72
+ format 50 chars 31.589k (± 2.5%) i/s - 158.350k in 5.016131s
73
+ format 100 chars 16.122k (± 1.2%) i/s - 80.988k in 5.024082s
74
+ format 400 chars 4.946k (± 2.6%) i/s - 24.800k in 5.017711s
75
+ format_html 1.659k (± 1.7%) i/s - 8.300k in 5.003164s
39
76
  ```
data/Rakefile CHANGED
@@ -7,6 +7,7 @@ rescue LoadError
7
7
  end
8
8
 
9
9
  require "bundler/gem_tasks"
10
+ require "rubygems/package_task"
10
11
  require "rake/testtask"
11
12
  require "rake/extensiontask"
12
13
  require "bundler"
@@ -19,22 +20,57 @@ CROSS_PLATFORMS = %w[
19
20
  x86_64-linux
20
21
  ]
21
22
 
22
- spec = Bundler.load_gemspec("autocorrect-rb.gemspec")
23
-
24
23
  Rake::TestTask.new(:test) do |t|
25
24
  t.libs << "test"
26
25
  t.libs << "lib"
27
26
  t.test_files = FileList["test/**/*_test.rb"]
28
27
  end
29
28
 
30
- Rake::ExtensionTask.new("autocorrect") do |ext|
31
- ext.gem_spec = spec
29
+ spec = Bundler.load_gemspec("autocorrect-rb.gemspec")
30
+
31
+ # Make a autocorrect-rb-0.0.0.gem
32
+ # Disable release gem for platform: ruby, because autocorrect rust source build is complex.
33
+ # Gem::PackageTask.new(spec).define
34
+
35
+ # Make a native gem, eg. autocorrect-rb-0.0.0-x86_64-linux.gem
36
+ Rake::ExtensionTask.new("autocorrect", spec) do |ext|
37
+ # Compile output autocorrect.so into lib/autocorrect/autocorrect.so
32
38
  ext.lib_dir = "lib/autocorrect"
33
39
  ext.source_pattern = "*.{rs,toml}"
34
40
  ext.cross_compile = true
35
41
  ext.cross_platform = CROSS_PLATFORMS
36
42
  end
37
43
 
38
- task build: :compile
44
+ task :bench do
45
+ require "benchmark/ips"
46
+ require "./lib/autocorrect-rb"
47
+
48
+ html = open("./test/fixtures/example.txt").read
49
+
50
+ Benchmark.ips do |x|
51
+ x.report("format 50 chars") do |n|
52
+ n.times do
53
+ AutoCorrect.format("【野村:重申吉利汽车(00175)“买入”评级 上调目标价至17.9港元】智通财经APP获悉,野村发布报告称")
54
+ end
55
+ end
56
+ x.report("format 100 chars") do |n|
57
+ n.times do
58
+ AutoCorrect.format("【野村:重申吉利汽车(00175)“买入”评级 上调目标价至17.9港元】智通财经APP获悉,野村发布报告称,【野村:重申吉利汽车(00175)“买入”评级 上调目标价至17.9港元】智通财经APP获悉,野村发布报告称")
59
+ end
60
+ end
61
+ x.report("format 400 chars") do |n|
62
+ n.times do
63
+ AutoCorrect.format("【野村:重申吉利汽车(00175)“买入”评级 上调目标价至17.9港元】智通财经APP获悉,野村发布报告称,上调吉利汽车(00175)目标价12.58%,由15.9港元升至17.9港元,并维持吉汽为行业首选股,重申对其“买入”评级,坚信吉汽长远可成为行业赢家。 该行称,随着公司销量持续复苏及产品组合改善,预计今年销量可达148万辆,同比升9%,较公司原定目标销量141万辆为高。 该行又称称,上调公司今明两年每股盈利预测各13%及升毛利率0.1个百分点,以反映销量较预期高2%及产品组合改善,主要是由领克品牌带动。公司自去年8月开始已持续投资领克品牌及进行市场推广,带动领克销量环比有所改变,预期今明两年领克将占整体销量的11%及14%。 该行表示,由于低端国产车品牌在欠缺新车款及科技下,行业整合度将提升。另外,公司从去年第二季到12月为止,一直都积极推动经销商去库存,这将有利公司今年利润率复苏。")
64
+ end
65
+ end
66
+
67
+ x.report("format_html") do |n|
68
+ n.times do
69
+ AutoCorrect.format_for(html, "text.html")
70
+ end
71
+ end
72
+ end
73
+ end
74
+
39
75
  task test: :compile
40
- task default: %i[compile test]
76
+ task default: %i[test]
@@ -0,0 +1,13 @@
1
+ [package]
2
+ edition = "2021"
3
+ name = "autocorrect-rb"
4
+ version = "2.1.1"
5
+
6
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
+ [lib]
8
+ crate-type = ["cdylib"]
9
+ name = "autocorrect"
10
+
11
+ [dependencies]
12
+ autocorrect = {path = "../../../autocorrect", version = ">=1.0.0"}
13
+ magnus = "0.3"
@@ -0,0 +1,4 @@
1
+ require "mkmf"
2
+ require "rb_sys/mkmf"
3
+
4
+ create_rust_makefile("autocorrect/autocorrect")
@@ -0,0 +1,125 @@
1
+ use magnus::{define_class, function, Error, Object};
2
+
3
+ #[derive(Debug, Clone)]
4
+ pub struct LineResult {
5
+ line: usize,
6
+ col: usize,
7
+ new: String,
8
+ old: String,
9
+ severity: usize,
10
+ }
11
+
12
+ impl LineResult {
13
+ pub fn line(&self) -> usize {
14
+ self.line
15
+ }
16
+
17
+ pub fn col(&self) -> usize {
18
+ self.col
19
+ }
20
+
21
+ pub fn get_new(&self) -> String {
22
+ self.new.clone()
23
+ }
24
+
25
+ pub fn old(&self) -> String {
26
+ self.old.clone()
27
+ }
28
+
29
+ pub fn severity(&self) -> usize {
30
+ self.severity
31
+ }
32
+
33
+ pub fn inspect(&self) -> String {
34
+ format!("{:?}", self)
35
+ }
36
+
37
+ pub fn to_hash(&self) -> Result<magnus::RHash, Error> {
38
+ let hash = magnus::RHash::new();
39
+ hash.aset("line", self.line())?;
40
+ hash.aset("col", self.col())?;
41
+ hash.aset("new", self.get_new())?;
42
+ hash.aset("old", self.old())?;
43
+ hash.aset("severity", self.severity())?;
44
+ Ok(hash)
45
+ }
46
+ }
47
+
48
+ #[derive(Debug, Clone)]
49
+ pub struct LintResult {
50
+ pub filepath: String,
51
+ pub lines: Vec<LineResult>,
52
+ pub error: String,
53
+ }
54
+
55
+ impl LintResult {
56
+ pub fn filepath(&self) -> String {
57
+ self.filepath.clone()
58
+ }
59
+
60
+ pub fn lines(&self) -> Vec<LineResult> {
61
+ self.lines.clone()
62
+ }
63
+
64
+ pub fn error(&self) -> String {
65
+ self.error.clone()
66
+ }
67
+
68
+ pub fn inspect(&self) -> String {
69
+ format!("{:?}", self)
70
+ }
71
+
72
+ pub fn to_hash(&self) -> Result<magnus::RHash, Error> {
73
+ let hash = magnus::RHash::new();
74
+ hash.aset("filepath", self.filepath())?;
75
+ hash.aset(
76
+ "lines",
77
+ self.lines()
78
+ .iter()
79
+ .map(|l| l.to_hash().unwrap())
80
+ .collect::<Vec<magnus::RHash>>(),
81
+ )?;
82
+ hash.aset("error", self.error())?;
83
+ Ok(hash)
84
+ }
85
+ }
86
+
87
+ pub fn format(input: String) -> String {
88
+ autocorrect::format(&input)
89
+ }
90
+
91
+ pub fn format_for(input: String, filename_or_ext: String) -> String {
92
+ autocorrect::format_for(&input, &filename_or_ext).out
93
+ }
94
+
95
+ pub fn lint_for(input: String, filename_or_ext: String) -> magnus::RHash {
96
+ let result = autocorrect::lint_for(&input, &filename_or_ext);
97
+
98
+ LintResult {
99
+ filepath: filename_or_ext,
100
+ lines: result
101
+ .lines
102
+ .iter()
103
+ .map(|l| LineResult {
104
+ line: l.line,
105
+ col: l.col,
106
+ new: l.new.clone(),
107
+ old: l.old.clone(),
108
+ severity: l.severity.clone() as usize,
109
+ })
110
+ .collect::<_>(),
111
+ error: result.error,
112
+ }
113
+ .to_hash()
114
+ .unwrap()
115
+ }
116
+
117
+ #[magnus::init(name = "autocorrect")]
118
+ fn init() -> Result<(), Error> {
119
+ let class = define_class("AutoCorrect", Default::default())?;
120
+ class.define_singleton_method("format", function!(format, 1))?;
121
+ class.define_singleton_method("format_for", function!(format_for, 2))?;
122
+ class.define_singleton_method("lint_for", function!(lint_for, 2))?;
123
+
124
+ Ok(())
125
+ }
@@ -3,10 +3,5 @@ begin
3
3
  ruby_version = /(\d+\.\d+)/.match(::RUBY_VERSION)
4
4
  require_relative "autocorrect/#{ruby_version}/autocorrect"
5
5
  rescue LoadError
6
- # fall back to the extension compiled upon installation.
7
- # use "require" instead of "require_relative" because non-native gems will place C extension files
8
- # in Gem::BasicSpecification#extension_dir after compilation (during normal installation), which
9
- # is in $LOAD_PATH but not necessarily relative to this file
10
- # (see https://github.com/sparklemotion/nokogiri/issues/2300 for more)
11
6
  require "autocorrect/autocorrect"
12
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autocorrect-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1.beta1
4
+ version: 2.1.2.beta4
5
5
  platform: x86_64-darwin
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-12 00:00:00.000000000 Z
11
+ date: 2022-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb_sys
@@ -34,6 +34,9 @@ extra_rdoc_files: []
34
34
  files:
35
35
  - README.md
36
36
  - Rakefile
37
+ - ext/autocorrect/Cargo.toml
38
+ - ext/autocorrect/extconf.rb
39
+ - ext/autocorrect/src/lib.rs
37
40
  - lib/autocorrect-rb.rb
38
41
  - lib/autocorrect/2.7/autocorrect.bundle
39
42
  - lib/autocorrect/3.0/autocorrect.bundle