phrase_lookup 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +2 -4
- data.tar.gz.sig +0 -0
- data/lib/phrase_lookup.rb +92 -14
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d53e25ba45ea872bcadcdf60d42d70ca9e2947ac
|
4
|
+
data.tar.gz: dd1dfa771d4c6e9680ca543afc99fa63db05b003
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2711085687c382bc577bc34868f5ddd64fd301b058a99a9e13d2868b3f2bac78ede02517af1cfa5a91cc2cfff2976b5072026e3c70ca6bdeb2978a639189a85
|
7
|
+
data.tar.gz: c16def56b22e5dcc2da1440545699475745473e6675b12613ef5f2b1e6ab9c34bd215cf7ac00bc3a7bf59cfbd89e2430ef8c67366b44ac937ce8cbc334c88458
|
checksums.yaml.gz.sig
CHANGED
@@ -1,4 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
!��I��H#�o-��;,wENG=y��נ/���*�1߄�xL��I�~^k]V>;�����>�s��t�e2g��
|
4
|
-
�S&0z�U�Y��X{.S��nD��ܡ0��om
|
1
|
+
�lg���<r �{��� ������.�1�
|
2
|
+
��T���G��?H�q�L��nW������N@V!j�_�A#1�*3��Z�Vi����l=�����vB3"�Z�A�L'#y5����o����%��ɮ2P����]q�vQ�Y�[����'��V�3�qqG��3���'�̨-���d��?4˩$��&˯A��T������l�lc�!32��s���]>��afH�
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/phrase_lookup.rb
CHANGED
@@ -5,39 +5,117 @@
|
|
5
5
|
# description: Returns a list of matching phrases from plain text
|
6
6
|
# idea: return the list sorted by rank
|
7
7
|
|
8
|
+
require 'yaml'
|
8
9
|
require 'rxfhelper'
|
9
10
|
|
10
11
|
|
11
12
|
class PhraseLookup
|
12
13
|
|
13
|
-
def initialize(raw_s=nil)
|
14
|
+
def initialize(raw_s=nil)
|
15
|
+
|
16
|
+
@master = if raw_s then
|
17
|
+
|
18
|
+
s = RXFHelper.read(raw_s).first
|
19
|
+
|
20
|
+
if s.lstrip =~ /^---/ then # it's YAML
|
21
|
+
|
22
|
+
Master.new(s)
|
23
|
+
|
24
|
+
elsif s =~ /: / # it's the contents of a log file
|
25
|
+
|
26
|
+
m = Master.new
|
27
|
+
m.create s
|
28
|
+
m
|
29
|
+
|
30
|
+
else # it's plain text
|
31
|
+
|
32
|
+
m = Master.new
|
33
|
+
m.create_from_txt s
|
34
|
+
m
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
14
40
|
|
15
|
-
|
41
|
+
end
|
42
|
+
|
43
|
+
class Master
|
44
|
+
|
45
|
+
def initialize(yaml=nil)
|
16
46
|
|
17
|
-
|
47
|
+
@h = yaml ? YAML.load(yaml) : {}
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
def create(s)
|
52
|
+
|
53
|
+
@h = parse_log(s)
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_from_txt(s)
|
58
|
+
|
59
|
+
@h = parse(s)
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def update(s)
|
18
64
|
|
19
|
-
s,
|
20
|
-
parse s
|
65
|
+
parse_log(s).each {|k,v| @h.has_key?(k) ? @h[k] += v : @h[k] = v }
|
21
66
|
|
22
67
|
end
|
23
68
|
|
69
|
+
def save(filename='master.yaml')
|
70
|
+
File.write filename, @h.sort.to_h.to_yaml
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_h()
|
74
|
+
@h.clone
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def parse(raw_s)
|
80
|
+
|
81
|
+
s, _ = RXFHelper.read(raw_s)
|
82
|
+
|
83
|
+
a = s.downcase.gsub(/\?/,'').lines.map(&:strip).compact
|
84
|
+
a.uniq.inject({}) {|r,x| r.merge x => a.count(x)}
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
def parse_log(raw_s)
|
89
|
+
|
90
|
+
s, _ = RXFHelper.read(raw_s)
|
91
|
+
|
92
|
+
a = s.downcase.gsub(/\?/,'').lines\
|
93
|
+
.map {|x| x.strip[/(?<=: )[^|]+(?= )/i]}.compact
|
94
|
+
a.uniq.inject({}) {|r,x| r.merge x => a.count(x)}
|
95
|
+
|
96
|
+
end
|
24
97
|
end
|
25
98
|
|
26
99
|
def lookup(s, limit: 10)
|
100
|
+
|
101
|
+
h = @master.to_h
|
102
|
+
a = h.keys
|
103
|
+
|
104
|
+
a1 = a.grep /^#{s}/i
|
105
|
+
a2 = a.grep /\b#{s}/i
|
27
106
|
|
28
|
-
a1
|
29
|
-
|
30
|
-
|
31
|
-
return (a1 + a2).uniq.take 10
|
107
|
+
return (a1 + a2).uniq.sort_by {|word| -h[word]}.take limit
|
108
|
+
|
32
109
|
end
|
33
110
|
|
34
111
|
alias q lookup
|
35
|
-
|
36
|
-
def
|
37
|
-
|
38
|
-
@a = s.strip.lines.map(&:strip)
|
39
|
-
|
112
|
+
|
113
|
+
def master()
|
114
|
+
@master
|
40
115
|
end
|
41
116
|
|
117
|
+
def save(filename='phrase_lookup.yaml')
|
118
|
+
@master.save filename
|
119
|
+
end
|
42
120
|
end
|
43
121
|
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|