-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
140 lines (125 loc) · 3.98 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require 'wordnet'
require 'pickup'
IMPROVE_WORD_RETRIES = 10
STANDARD_IMPROVEMENTS_HASH = {'child' => 10, 'parent' => 10, 'sibling' => 10, 'synonym' => 10, 'define' => 3, 'nothing' => 40}
CONTINUE_IMPROVING_PROBABILITY = 0.8
MAX_CONTINUE_IMPROVING = 2
l = WordNet::Lexicon.new
# Convert a word string to a randomly chosen synonym set compatible with the word.
def string_to_synset(s, lexicon)
possible_synsets = lexicon.lookup_synsets(s.to_sym)
if possible_synsets.empty?
return nil
else
return possible_synsets.sample
end
end
# Convert a synonym set to a string containing a randomly chosen word compatible with it.
def synset_to_string(synset)
if synset.nil?
return '...'
else
possible_strings = synset.words
return possible_strings.sample
end
end
# Return a random synonym set that is a hypernym of the current synonym set, i.e., something more general.
def random_parent(synset)
if synset.nil?
return nil
else
possible_parents = synset.hypernyms
if possible_parents.empty?
return nil
else
return possible_parents.sample
end
end
end
# Return a random synonym set that is a hyponym of the current synonym set, i.e., something more specific.
def random_child(synset)
if synset.nil?
return nil
else
possible_children = synset.hyponyms
if possible_children.empty?
return nil
else
return possible_children.sample
end
end
end
# Return a random synonym set that is a hyponym of a hypernym of the current synonym set, i.e., something of the same kind.
def random_sibling(synset)
if synset.nil?
return nil
else
possible_siblings = []
synset.hypernyms.each do |hypernym|
hypernym.hyponyms.each do |hyponym|
possible_siblings << hyponym
end
end
if possible_siblings.empty?
return nil
else
return possible_siblings.sample
end
end
end
# Convert a word into a different word.
def improve_word(word, lexicon, times_tried=0, improvement='child')
if improvement == 'sibling'
improved = synset_to_string(random_sibling(string_to_synset(word, lexicon)))
elsif improvement == 'child'
improved = synset_to_string(random_child(string_to_synset(word, lexicon)))
elsif improvement == 'parent'
improved = synset_to_string(random_parent(string_to_synset(word, lexicon)))
elsif improvement == 'define'
corresponding_synset = string_to_synset(word, lexicon)
if corresponding_synset.nil?
improved = '...'
else
improved = corresponding_synset.definition
end
elsif improvement == 'nothing'
improved = word
elsif improvement == 'synonym'
improved = synset_to_string(string_to_synset(word, lexicon))
end
# If the process failed to find a modified word, retry a number of times and then just use the original word.
if improved == '...'
if times_tried < IMPROVE_WORD_RETRIES
return improve_word(word, lexicon, times_tried + 1)
else
return word
end
else
return improved
end
end
def probabilistic_improve_word(word, lexicon, improvements_hash=STANDARD_IMPROVEMENTS_HASH)
pickup = Pickup.new(improvements_hash)
picked_improvement = pickup.pick
return improve_word(word, lexicon, 0, picked_improvement)
end
def probabilistic_improve_all_words(text, lexicon, improvements_hash=STANDARD_IMPROVEMENTS_HASH)
return text.gsub(/\w+/) { |word| probabilistic_improve_word(word, lexicon, improvements_hash) }
end
# Convert a text into a different text by converting each word of the text into a different word.
def improve_text(text, lexicon)
text = probabilistic_improve_all_words(text, lexicon)
return text
end
# Test text conversion with a particular text.
def test_me_with(text, lexicon)
puts text
puts 'becomes'
puts improve_text(text, lexicon)
end
# Test text conversion with a list of words.
def test_me(lexicon)
test_text = "This is our test sentence, which we are using to test the program: that way, we can see whether or not the program works. "
test_me_with(test_text, lexicon)
end
test_me(l)