This is the last chapter before the Expansion Pak. By now you know enough Ruby to write real programs. The fox stories have reached a conclusion. The beard has been wished upon. Let’s write something that works.
A Real Program
Here is a program that uses what we have learned. It is a simple AI text analyzer — written in the spirit of this domain’s second life as an AI detection research hub:
class TextAnalyzer
AI_PHRASES = [
"delve into", "furthermore", "it is worth noting",
"in conclusion", "leverage", "it is important to",
"as an ai language model", "certainly!", "absolutely!"
]
def initialize(text)
@text = text.downcase
@words = @text.split
end
def word_count
@words.length
end
def ai_phrase_score
hits = AI_PHRASES.count { |phrase| @text.include?(phrase) }
(hits.to_f / AI_PHRASES.length * 100).round(1)
end
def type_token_ratio
unique = @words.uniq.length
(unique.to_f / @words.length).round(3)
end
def verdict
score = ai_phrase_score
if score > 30
"LIKELY AI-GENERATED"
elsif score > 15
"UNCERTAIN — check manually"
else
"LIKELY HUMAN"
end
end
def report
puts "Words: #{word_count}"
puts "TTR: #{type_token_ratio}"
puts "AI phrase score: #{ai_phrase_score}%"
puts "Verdict: #{verdict}"
end
end
sample = "Furthermore, it is worth noting that we must
leverage all available resources to delve into
the core of this matter. In conclusion, the
synthesis of these findings is paramount."
TextAnalyzer.new(sample).report
What Comes Next
The Expansion Pak covers IRB in depth. After that, the logical paths are ruby-lang.org for the official documentation, and Ruby on Rails guides if web development is where you want to take this.
“When you don’t create things, you become defined by your tastes rather than ability. Your tastes only narrow and rarely grow. So create.” — _why the lucky stiff