tags_finder 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tags_finder/job.rb +29 -0
- data/lib/tags_finder/parser.rb +73 -0
- data/lib/tags_finder/result.rb +35 -0
- data/lib/tags_finder/version.rb +6 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b82e2d98ef05deaac97bc0844f3acd47038c7e26d7c2415ae10412b8364cb5ee
|
4
|
+
data.tar.gz: cd201c99dc20c355f57a5d1620a8edd59b8555b920519f3c613f60e444f2355e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97e8f96d68ff7459b281f844abbdae9e678c807c2c9a9c4f815c21ae5ed3feccc34b00d0623ffd9d1d2f04eab1ee3f8bb621bfe5d34eeb0d7223461769552e95
|
7
|
+
data.tar.gz: dd41c7015025d6caf87eb19b86378922c59b2d791da9389eac981e520e788911a4b29c8ac7d20d194d69673881066caf25e81434ceb17ef3c807f16036298d45
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :nodoc:
|
4
|
+
class TagsFinder
|
5
|
+
# :nodoc:
|
6
|
+
class Job
|
7
|
+
attr_reader :pid
|
8
|
+
|
9
|
+
def initialize(&block)
|
10
|
+
@block = block
|
11
|
+
@read, @write = IO.pipe
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
@pid = fork do
|
16
|
+
@read.close
|
17
|
+
@write.write(Marshal.dump(@block.call))
|
18
|
+
end
|
19
|
+
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def result
|
24
|
+
@write.close
|
25
|
+
res = @read.read
|
26
|
+
res.empty? ? nil : Marshal.load(res)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :nodoc:
|
4
|
+
class TagsFinder
|
5
|
+
# :nodoc:
|
6
|
+
class Parser
|
7
|
+
attr_reader :results
|
8
|
+
|
9
|
+
def self.build_html_tags_reg(tags:, gtags:)
|
10
|
+
@@html_tags_reg = %r{<#{gtags}\s+([^>]*)>|<#{tags}\s+([^>]*)>|(</#{gtags}>)|(</#{tags}>)}i
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(path, search_tags, path_tags)
|
14
|
+
@path = path
|
15
|
+
@search_tags = search_tags
|
16
|
+
@path_tags = path_tags
|
17
|
+
@gtags = []
|
18
|
+
@tag = nil
|
19
|
+
@results = []
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse
|
23
|
+
File.readlines(@path).each_with_index do |line, line_number|
|
24
|
+
next unless (matches = line.match(@@html_tags_reg))
|
25
|
+
|
26
|
+
next push_gtags(matches[1]) if matches[1]
|
27
|
+
next set_tags(matches[2], line_number) if matches[2]
|
28
|
+
next pop_gtags(line_number) if matches[3]
|
29
|
+
|
30
|
+
push_tag(line_number)
|
31
|
+
end
|
32
|
+
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def push_gtags(gtags)
|
39
|
+
@gtags << gtags.gsub(/\s+/, ' ').split
|
40
|
+
end
|
41
|
+
|
42
|
+
def pop_gtags(line_number)
|
43
|
+
if @gtags.empty?
|
44
|
+
raise TagsFinder::Error,
|
45
|
+
"Unexpected gtag closure at #{@path}:#{line_number + 1}"
|
46
|
+
end
|
47
|
+
|
48
|
+
@gtags.pop
|
49
|
+
end
|
50
|
+
|
51
|
+
def set_tags(tags, line_number)
|
52
|
+
@tag = tags.gsub(/\s+/, ' ').split
|
53
|
+
@start_line = line_number
|
54
|
+
end
|
55
|
+
|
56
|
+
def push_tag(line_number)
|
57
|
+
unless @tag
|
58
|
+
raise TagsFinder::Error,
|
59
|
+
"Unexpected tag closure at #{@path}:#{line_number + 1}"
|
60
|
+
end
|
61
|
+
|
62
|
+
@results << Result.new(
|
63
|
+
path: @path,
|
64
|
+
start_line: @start_line,
|
65
|
+
end_line: line_number,
|
66
|
+
tags: (@path_tags + (@gtags.last || []) + @tag).uniq,
|
67
|
+
search_tags: @search_tags
|
68
|
+
)
|
69
|
+
|
70
|
+
@tag = nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :nodoc:
|
4
|
+
class TagsFinder
|
5
|
+
# :nodoc:
|
6
|
+
class Result
|
7
|
+
attr_reader :path, :start_line, :end_line, :tags, :search_tags
|
8
|
+
|
9
|
+
def initialize(
|
10
|
+
path:, start_line:, end_line:, tags:, search_tags:
|
11
|
+
)
|
12
|
+
@path = path
|
13
|
+
@start_line = start_line
|
14
|
+
@end_line = end_line
|
15
|
+
@tags = tags
|
16
|
+
@search_tags = search_tags
|
17
|
+
end
|
18
|
+
|
19
|
+
def score
|
20
|
+
@score ||= begin
|
21
|
+
similarities = @search_tags.map do |search_tag|
|
22
|
+
@tags.map do |tag|
|
23
|
+
String::Similarity.cosine(search_tag, tag)
|
24
|
+
end.max || 0
|
25
|
+
end
|
26
|
+
|
27
|
+
similarities.sum / search_tags.length.to_f
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def extract
|
32
|
+
File.read(@path).split("\n")[@start_line..@end_line] * "\n"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tags_finder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joakim Carrilho
|
@@ -31,6 +31,10 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
33
|
- lib/tags_finder.rb
|
34
|
+
- lib/tags_finder/job.rb
|
35
|
+
- lib/tags_finder/parser.rb
|
36
|
+
- lib/tags_finder/result.rb
|
37
|
+
- lib/tags_finder/version.rb
|
34
38
|
homepage: https://github.com/Kimoja/tags_finder
|
35
39
|
licenses:
|
36
40
|
- MIT
|