-
Notifications
You must be signed in to change notification settings - Fork 1
/
policy_deviser.rb
85 lines (69 loc) · 2.43 KB
/
policy_deviser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'engtagger'
require 'wordnet'
class PolicyDeviser
def self.generate_policy
country = choose_country
noun = nil
while !noun do
noun, username = find_noun_and_username_for_country(country)
end
case [:urge, :behove].sample
when :urge
"We #{ urge_predicate } #{ country[:name] } to privatise its #{ noun }. /cc @#{ username }"
when :behove
"It #{ behove_predicate } #{ country[:name] } to privatise its #{ noun }. /cc @#{ username }"
end
end
private
def self.urge_predicate
adverb = ['strongly','emphatically','enthusiastically','passionately','exuberantly','fervently',nil].sample
verb = ['urge','pressure','encourage','admonish','beseech','exhort'].sample
[adverb, verb].compact.join(' ')
end
def self.behove_predicate
['behoves', 'is incumbent on', 'would be prudent for', 'is advisable for', 'is sensible for'].sample
end
def self.privatise_synonym
['privatise','subcontract'].sample
end
def self.find_noun_and_username_for_country(country)
$twitter.search("lang:en -rt #{ country[:demonym] }", result_type: "recent", count: 100).take(100).to_a.shuffle.each do |tweet|
word = get_noun_after_demonym(tweet.text.downcase, country[:demonym])
next unless word && word.size > 2
puts " >> " + tweet.text
return [word, tweet.user.username]
end
nil
end
def self.get_noun_after_demonym(tweet_text, country_demonym)
tgr = EngTagger.new
tagged = tgr.add_tags(tweet_text)
nouns = tgr.get_noun_phrases(tagged).keys.sort_by { |i| i.length }.reverse
nouns.each do |noun|
o_noun = noun.scan(/^[^[@\.,&]]+/).first # ignore weird stuff
next unless o_noun
next unless tweet_text.include?("#{ country_demonym } #{ o_noun }")
next unless is_noun?(o_noun.split(' ').last)
return o_noun
end
nil
end
def self.is_noun?(word)
lemma = WordNet::Lemma.find(word, :noun)
return !lemma.nil?
end
def self.choose_country
[
{name: 'Portugal', demonym: 'portuguese'},
{name: 'Ireland', demonym: 'irish'},
{name: 'Italy', demonym: 'italian'},
{name: 'Greece', demonym: 'greek'},
{name: 'Spain', demonym: 'spanish'},
{name: 'Portugal', demonym: "portugal's"},
{name: 'Ireland', demonym: "ireland's"},
{name: 'Italy', demonym: "italy's"},
{name: 'Greece', demonym: "greece's"},
{name: 'Spain', demonym: "spain's"}
].sample
end
end