abbrev 0.1.0
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 +7 -0
- data/.github/workflows/test.yml +24 -0
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +10 -0
- data/abbrev.gemspec +22 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/abbrev.rb +132 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '0936bf5184cb0038579110215a1980ca424ce611181642b825afae1b7a937d89'
|
4
|
+
data.tar.gz: a93e2ed249fbad84108fda61af9cf7589b68e1a8aa0311503aea924a3b3dd21a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 192634a1378245585dbcc486d720d9b70dc0adad3b8acbaa74793f0312e15c263b27dd63461ae727ac7dfa4d4b0a758f13251cb6b4f9c335358b11d079d6016b
|
7
|
+
data.tar.gz: 85cefa2b2abc2e86bb62a90492ae38eeedf4bdb5481d24261943efc7da11b759a532683d2204d68db81a2a3db8d425557834e0f01414e08089a177ada5982cb8
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: test
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
name: build (${{ matrix.ruby }} / ${{ matrix.os }})
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: [ 2.7, 2.6, 2.5, head ]
|
11
|
+
os: [ ubuntu-latest, macos-latest ]
|
12
|
+
runs-on: ${{ matrix.os }}
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@master
|
15
|
+
- name: Set up Ruby
|
16
|
+
uses: ruby/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
19
|
+
- name: Install dependencies
|
20
|
+
run: |
|
21
|
+
gem install bundler --no-document
|
22
|
+
bundle install
|
23
|
+
- name: Run test
|
24
|
+
run: rake test
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Abbrev
|
2
|
+
|
3
|
+
Given a set of strings, calculate the set of unambiguous abbreviations for
|
4
|
+
those strings, and return a hash where the keys are all the possible
|
5
|
+
abbreviations and the values are the full strings.
|
6
|
+
|
7
|
+
Thus, given +words+ is "car" and "cone", the keys pointing to "car" would
|
8
|
+
be "ca" and "car", while those pointing to "cone" would be "co", "con", and
|
9
|
+
"cone".
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'abbrev'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install abbrev
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'abbrev'
|
31
|
+
|
32
|
+
Abbrev.abbrev(%w{ car cone })
|
33
|
+
#=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"}
|
34
|
+
```
|
35
|
+
|
36
|
+
## Development
|
37
|
+
|
38
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
39
|
+
|
40
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/abbrev.
|
45
|
+
|
data/Rakefile
ADDED
data/abbrev.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "abbrev"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Akinori MUSHA"]
|
5
|
+
spec.email = ["knu@idaemons.org"]
|
6
|
+
|
7
|
+
spec.summary = %q{Calculates a set of unique abbreviations for a given set of strings}
|
8
|
+
spec.description = %q{Calculates a set of unique abbreviations for a given set of strings}
|
9
|
+
spec.homepage = "https://github.com/ruby/abbrev"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "abbrev"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/abbrev.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
|
4
|
+
#
|
5
|
+
# All rights reserved. You can redistribute and/or modify it under
|
6
|
+
# the same terms as Ruby.
|
7
|
+
#
|
8
|
+
# $Idaemons: /home/cvs/rb/abbrev.rb,v 1.2 2001/05/30 09:37:45 knu Exp $
|
9
|
+
# $RoughId: abbrev.rb,v 1.4 2003/10/14 19:45:42 knu Exp $
|
10
|
+
# $Id$
|
11
|
+
#++
|
12
|
+
|
13
|
+
##
|
14
|
+
# Calculates the set of unambiguous abbreviations for a given set of strings.
|
15
|
+
#
|
16
|
+
# require 'abbrev'
|
17
|
+
# require 'pp'
|
18
|
+
#
|
19
|
+
# pp Abbrev.abbrev(['ruby'])
|
20
|
+
# #=> {"ruby"=>"ruby", "rub"=>"ruby", "ru"=>"ruby", "r"=>"ruby"}
|
21
|
+
#
|
22
|
+
# pp Abbrev.abbrev(%w{ ruby rules })
|
23
|
+
#
|
24
|
+
# _Generates:_
|
25
|
+
# { "ruby" => "ruby",
|
26
|
+
# "rub" => "ruby",
|
27
|
+
# "rules" => "rules",
|
28
|
+
# "rule" => "rules",
|
29
|
+
# "rul" => "rules" }
|
30
|
+
#
|
31
|
+
# It also provides an array core extension, Array#abbrev.
|
32
|
+
#
|
33
|
+
# pp %w{ summer winter }.abbrev
|
34
|
+
#
|
35
|
+
# _Generates:_
|
36
|
+
# { "summer" => "summer",
|
37
|
+
# "summe" => "summer",
|
38
|
+
# "summ" => "summer",
|
39
|
+
# "sum" => "summer",
|
40
|
+
# "su" => "summer",
|
41
|
+
# "s" => "summer",
|
42
|
+
# "winter" => "winter",
|
43
|
+
# "winte" => "winter",
|
44
|
+
# "wint" => "winter",
|
45
|
+
# "win" => "winter",
|
46
|
+
# "wi" => "winter",
|
47
|
+
# "w" => "winter" }
|
48
|
+
|
49
|
+
module Abbrev
|
50
|
+
|
51
|
+
# Given a set of strings, calculate the set of unambiguous abbreviations for
|
52
|
+
# those strings, and return a hash where the keys are all the possible
|
53
|
+
# abbreviations and the values are the full strings.
|
54
|
+
#
|
55
|
+
# Thus, given +words+ is "car" and "cone", the keys pointing to "car" would
|
56
|
+
# be "ca" and "car", while those pointing to "cone" would be "co", "con", and
|
57
|
+
# "cone".
|
58
|
+
#
|
59
|
+
# require 'abbrev'
|
60
|
+
#
|
61
|
+
# Abbrev.abbrev(%w{ car cone })
|
62
|
+
# #=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"}
|
63
|
+
#
|
64
|
+
# The optional +pattern+ parameter is a pattern or a string. Only input
|
65
|
+
# strings that match the pattern or start with the string are included in the
|
66
|
+
# output hash.
|
67
|
+
#
|
68
|
+
# Abbrev.abbrev(%w{car box cone crab}, /b/)
|
69
|
+
# #=> {"box"=>"box", "bo"=>"box", "b"=>"box", "crab" => "crab"}
|
70
|
+
#
|
71
|
+
# Abbrev.abbrev(%w{car box cone}, 'ca')
|
72
|
+
# #=> {"car"=>"car", "ca"=>"car"}
|
73
|
+
def abbrev(words, pattern = nil)
|
74
|
+
table = {}
|
75
|
+
seen = Hash.new(0)
|
76
|
+
|
77
|
+
if pattern.is_a?(String)
|
78
|
+
pattern = /\A#{Regexp.quote(pattern)}/ # regard as a prefix
|
79
|
+
end
|
80
|
+
|
81
|
+
words.each do |word|
|
82
|
+
next if word.empty?
|
83
|
+
word.size.downto(1) { |len|
|
84
|
+
abbrev = word[0...len]
|
85
|
+
|
86
|
+
next if pattern && pattern !~ abbrev
|
87
|
+
|
88
|
+
case seen[abbrev] += 1
|
89
|
+
when 1
|
90
|
+
table[abbrev] = word
|
91
|
+
when 2
|
92
|
+
table.delete(abbrev)
|
93
|
+
else
|
94
|
+
break
|
95
|
+
end
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
words.each do |word|
|
100
|
+
next if pattern && pattern !~ word
|
101
|
+
|
102
|
+
table[word] = word
|
103
|
+
end
|
104
|
+
|
105
|
+
table
|
106
|
+
end
|
107
|
+
|
108
|
+
module_function :abbrev
|
109
|
+
end
|
110
|
+
|
111
|
+
class Array
|
112
|
+
# Calculates the set of unambiguous abbreviations for the strings in +self+.
|
113
|
+
#
|
114
|
+
# require 'abbrev'
|
115
|
+
# %w{ car cone }.abbrev
|
116
|
+
# #=> {"car"=>"car", "ca"=>"car", "cone"=>"cone", "con"=>"cone", "co"=>"cone"}
|
117
|
+
#
|
118
|
+
# The optional +pattern+ parameter is a pattern or a string. Only input
|
119
|
+
# strings that match the pattern or start with the string are included in the
|
120
|
+
# output hash.
|
121
|
+
#
|
122
|
+
# %w{ fast boat day }.abbrev(/^.a/)
|
123
|
+
# #=> {"fast"=>"fast", "fas"=>"fast", "fa"=>"fast", "day"=>"day", "da"=>"day"}
|
124
|
+
#
|
125
|
+
# Abbrev.abbrev(%w{car box cone}, "ca")
|
126
|
+
# #=> {"car"=>"car", "ca"=>"car"}
|
127
|
+
#
|
128
|
+
# See also Abbrev.abbrev
|
129
|
+
def abbrev(pattern = nil)
|
130
|
+
Abbrev::abbrev(self, pattern)
|
131
|
+
end
|
132
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abbrev
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akinori MUSHA
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Calculates a set of unique abbreviations for a given set of strings
|
14
|
+
email:
|
15
|
+
- knu@idaemons.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/test.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- abbrev.gemspec
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/abbrev.rb
|
30
|
+
homepage: https://github.com/ruby/abbrev
|
31
|
+
licenses:
|
32
|
+
- Ruby
|
33
|
+
- BSD-2-Clause
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/ruby/abbrev
|
36
|
+
source_code_uri: https://github.com/ruby/abbrev
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.3.0
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.2.0.rc.1
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Calculates a set of unique abbreviations for a given set of strings
|
56
|
+
test_files: []
|