gunsnprices 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.
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "py"
4
+
5
+ module GunsNPrices
6
+ # Numbers from product titles: magazine capacity, barrel length, rounds per box and cost per round.
7
+ # @api private
8
+ module Specs
9
+ Rx = Py::Rx
10
+
11
+ def self.prep(text)
12
+ s = Py.lower(Py.unescape(Py.unescape(Py.str(text))))
13
+ s = s.gsub("”", '"').gsub("″", '"').gsub("“", '"').gsub("''", '"').gsub("’’", '"')
14
+ s = s.delete("®").delete("™")
15
+ Py.collapse_ws(s)
16
+ end
17
+
18
+ NUM = '(\d{1,3}(?:,\d{3})+|\d{1,5})'
19
+ AMMO = Rx.new(
20
+ '\b(?:ammo|ammunition|shotshells?|shot ?shells?|cartridges?|bullets?|primers?|slugs?|buckshot|pellets?|bbs?|' \
21
+ 'grains?|\d+\s*gr|per box|box of|rounds? box|rds?/box)\b|/\s*(?:box|bx|pk|pack|case|cs|tin)\b'
22
+ )
23
+
24
+ # --- magazine capacity ---
25
+ CAP_PLUS = Rx.new('(?<![\d.:/x])(\d{1,3})\s*\+\s*1(?!\d)')
26
+ CAP_RD = Rx.new('(?<![\d.:/x])(\d{1,3})\s*-?\s*(?:rd|rds|rnd|rnds|round|rounds|shot|shots)\b' \
27
+ '(?!\s*/?\s*(?:per\b|box|bx|pack|pk|case))')
28
+ # "13rd/15rd", "1-30Rd/1-10Rd Mag": ships with magazines of two sizes - take the larger one
29
+ CAP_PAIR = Rx.new('(?<![\d.:/x])(\d{1,3})\s*-?\s*(?:rd|rds|rnd|rnds|rounds?)?\s*/\s*(?:1?-)?(\d{1,3})\s*-?\s*' \
30
+ '(?:rd|rds|rnd|rnds|rounds?)\b')
31
+ CAP_MAG = Rx.new('\b(?:cap|capacity)\s*[:=]?\s*(\d{1,3})\b(?!\s*(?:"|in\b|gr\b))')
32
+
33
+ # --- barrel length ---
34
+ INCH = '(?:"|in\b(?!\s*(?:1\b|stock\b|one\b))\.?|inch(?:es)?\b|-inch\b|-in\b)'
35
+ LEN = '(\d{1,2}(?:\.\d{1,3})?)(?:[\s-]+(\d{1,2})/(\d{1,2}))?' # 18.5 / 16 1/2
36
+ BBL = '(?:bbl|brl|barrel)\b'
37
+ BAR_TAGGED = [
38
+ Rx.new("(?<![\\d.:/x\#$-])#{LEN}\\s*-?\\s*#{INCH}?\\s*#{BBL}"), # 20" barrel, 22 bbl
39
+ Rx.new("\\b(?:bbl|brl|barrel)\\s*(?:length)?\\s*[:=]?\\s*#{LEN}\\s*#{INCH}") # barrel: 20"
40
+ ].freeze
41
+ BAR_INCH = Rx.new("(?<![\\d.:/x\#$-])#{LEN}\\s*-?\\s*#{INCH}")
42
+ NOT_BARREL_AFTER = Rx.new(
43
+ '\s*-?\s*(?:chamber|chbr|chmbr|shells?|m-?lok|mlok|handguard|hg\b|rail|keymod|quad|lop\b|length of pull|' \
44
+ 'oal\b|twist|rate)', anchor: :start
45
+ )
46
+ NOT_BARREL_BEFORE = Rx.new('(?:lop|oal|overall|length of pull|pull|twist|1:|chamber|m-?lok|mlok|handguard|rail|' \
47
+ 'sight radius)\s*[:=]?\s*$')
48
+ SHOTGUN = Rx.new('\b(?:\d{1,2}|410)\s*(?:ga|gauge|g)\b|\.410|shotgun|o/u|over/under|sxs|side by side|' \
49
+ '\b(?:12|16|20|28|410)/\d\d\b')
50
+ # "O/U 12/28": gauge / barrel length (10 is left out: "10/22" is a Ruger rifle)
51
+ GAUGE_SLASH = Rx.new('(?<![a-z0-9./$#-])(?:12|16|20|28|410)/(1[89](?:\.5)?|2\d|3[0-4])(?![\d/.])' \
52
+ '(?!\s*(?:ga|gauge|rd|round))')
53
+
54
+ # --- rounds per box ---
55
+ B = '(?<![a-z0-9.#$/])'
56
+ RPB = [
57
+ Rx.new("#{B}#{NUM}\\s*-?\\s*(?:rd|rds|round|rounds|rnd|rnds|ct|count|pcs|pc|cartridges|shells|shotshells|bullets)\\b" \
58
+ '(?!\s*(?:mag|magazine|capacity|cap)\b)'),
59
+ Rx.new("#{B}#{NUM}\\s*(?:/\\s*|per\\s+)(?:box|bx|pack|pk|case|cs|tin|can|bag|ct)\\b"),
60
+ Rx.new("(?:box|pack|case|tin|bx|bag|can) of\\s*#{NUM}"),
61
+ Rx.new("#{B}#{NUM}\\s*-?\\s*(?:pack|pk|packs)\\b")
62
+ ].freeze
63
+ # distributor notation "50/10", "20/25": rounds per box / boxes per case
64
+ BOX_CASE = Rx.new('(?<![\d.\-/])(5|10|15|18|20|25|30|40|50|100|200|250|500|1000)\s*/\s*(\d{1,3})(?![\d/.\"])')
65
+ CALIBER_SLASH = [%w[30 30], %w[45 70], %w[30 06], %w[22 250], %w[25 06], %w[38 55], %w[44 40], %w[32 20],
66
+ %w[38 40], %w[25 20], %w[32 40]].freeze
67
+ NOT_CASE_COUNTS = %w[2 4 8 16 32].freeze
68
+
69
+ class << self
70
+ def looks_like_ammo?(title)
71
+ AMMO.match?(prep(title))
72
+ end
73
+
74
+ def magazine_capacity(title)
75
+ s = prep(title)
76
+ m = CAP_PAIR.search(s)
77
+ if m
78
+ n = [Py.int(m[1]), Py.int(m[2])].max
79
+ return n if n >= 1 && n <= 110
80
+ end
81
+ [CAP_PLUS, CAP_RD, CAP_MAG].each do |rx|
82
+ rx.each_match(s) do |mm|
83
+ n = Py.int(mm[1])
84
+ return n if n >= 1 && n <= 110
85
+ end
86
+ end
87
+ nil
88
+ end
89
+
90
+ def barrel_length(title)
91
+ s = prep(title)
92
+ BAR_TAGGED.each do |rx|
93
+ rx.each_match(s) do |m|
94
+ v = length(m)
95
+ return v if v >= 1.5 && v <= 36
96
+ end
97
+ end
98
+ m = GAUGE_SLASH.search(s)
99
+ return Py.float(m[1]) if m
100
+
101
+ shotgun = SHOTGUN.match?(s)
102
+ BAR_INCH.each_match(s) do |mm|
103
+ v = length(mm)
104
+ next unless v >= 1.5 && v <= 36
105
+ next if NOT_BARREL_AFTER.search(s, mm.end(0)) ||
106
+ NOT_BARREL_BEFORE.match?(s[[0, mm.begin(0) - 20].max...mm.begin(0)])
107
+ next if shotgun && v <= 3.5 # 3" chamber
108
+
109
+ return v
110
+ end
111
+ nil
112
+ end
113
+
114
+ def rounds_per_box(title)
115
+ s = prep(title)
116
+ RPB.each do |rx|
117
+ m = rx.search(s)
118
+ next unless m
119
+
120
+ n = Py.int(m[1].delete(","))
121
+ return n if n >= 1 && n <= 20_000
122
+ end
123
+ BOX_CASE.each_match(s) do |m|
124
+ return Py.int(m[1]) if !CALIBER_SLASH.include?([m[1], m[2]]) && !NOT_CASE_COUNTS.include?(m[2])
125
+ end
126
+ nil
127
+ end
128
+
129
+ def cost_per_round(price, rounds)
130
+ return nil unless Py.present?(price) && Py.present?(rounds)
131
+
132
+ price = Float(price)
133
+ return nil if price <= 0 || rounds <= 0
134
+
135
+ Py.round4(price / rounds)
136
+ end
137
+
138
+ private
139
+
140
+ def length(m)
141
+ v = Py.float(m[1])
142
+ if Py.present?(m[2]) && Py.present?(m[3])
143
+ num = Py.int(m[2])
144
+ den = Py.int(m[3])
145
+ v += num.fdiv(den) if [2, 4, 8, 16].include?(den) && num < den
146
+ end
147
+ v
148
+ end
149
+ end
150
+ end
151
+ end