Rubyで英語記事に含まれてる英単語を数えて出現数順にソートする

ためしにこの記事でやってみる。[Houzz wins “Best App” at the inaugural Google Play Awards](http://techcrunch.com/2016/05/19/houzz-wins-best-app-at-the-inaugural-google-play-awards/)

txtファイルをに本文をコピペ。

```test.txt
Interior design and decorating resource Houzz is the overall best Android app of the year, according to Google, which this evening announced the results of the first-ever Google Play Awards at its developer conference, Google I/O. While in previous years, the company had rounded up large numbers of apps for “Best of” lists, the new Google Play Awards instead follows Apple’s model of selecting winners across a variety of categories.

But where Apple chooses best apps for phone, tablet, and watch, along with selections like “most innovative,” Google took a somewhat different path. It selected 5 nominees across 10 categories, and didn’t differentiate between those built for tablet or phone.

Google also nominated apps that reflected a group of developers who were taking the best advantage of Google’s services, like “Best Use of Google Play Game Services,” plus those who did the best job of implementing Google’s newer design language, Material Design.

And Google didn’t limit itself to only one category focused on innovation, but chose several angles – including not only the “most innovative” app, but also “Standout Startup” and “Early Adopter. The move was likely meant to signal that not all of the best and most creative new apps are exclusive to iOS, or launched on iOS first.

The company live-streamed the ceremony this evening from Google I/O, which was also a change from previous years.

On stage, the company spoke of Google Play’s evolution and noted that the online storefront now reaches over a billion users daily. Apps are obviously a huge draw for this store – earlier this week, Google said that app installs from Google Play had reached 65 billion in the past year.

The winning apps were chosen by a panel of experts at Google, and rated on several factors, including app quality and innovation. To be considered, the apps either needed to have launched or had a major update over the last 12 months.

Houzz ended up winning the “Best App” title, beating out BuzzFeed News, Colorfy, TuneIn Radio and Yummly.

Meanwhile, the hugely popular Clash Royale walked away with the “Best Game” crown, topping Alphabear, Clash of Kings, MARVEL Future Fight, and Star Wars Galaxy of Heroes.

From the startup world, the smart airfare prediction engine Hopper won “Best Standout Startup” and millennial-focused investing app Robinhood grabbed the win for “Best Use of Material Design.”
```

こんな感じで書く。

```test.rb
array = []
hash = {}

File.open('test.txt', 'r') do |word|
  word.each_line do |line|
    array.push line.split("\s").map{|m| m.gsub(/\.|\”|\,|\“/, "")}
  end
end

array = array.flatten.compact.delete_if(&:empty?)
puts "文字数：#{array.count}"
puts "----------------------------------------------------------------------------------------------------"

array.each do |word|
  if hash.has_key? word
    hash[word] += 1
  else
    hash[word] = 1
  end
end

hash.sort_by{|_, v| -v}.each do |key, value|
  puts "#{key}(#{value})"
end
```

一応できた。※Rubyらしい書き方、是非教えて下さい。m(_ _)m

```
文字数：393
----------------------------------------------------------------------------------------------------
the(22)
of(16)
Google(13)
and(11)
a(8)
apps(6)
Best(6)
to(5)
for(5)
best(5)
app(5)
that(4)
Play(4)
this(4)
most(3)
or(3)
also(3)
had(3)
from(3)
company(3)
on(3)
The(3)
across(2)
are(2)
Awards(2)
at(2)
Startup(2)
Standout(2)
not(2)
I/O(2)
including(2)
in(2)
previous(2)
years(2)
evening(2)
which(2)
–(2)
up(2)
several(2)
but(2)
iOS(2)
year(2)
launched(2)
innovation(2)
new(2)
over(2)
only(2)
Design(2)
Material(2)
Game(2)
Use(2)
was(2)
Houzz(2)
Google’s(2)
categories(2)
were(2)
who(2)
billion(2)
winning(2)
phone(2)
tablet(2)
design(2)
those(2)
with(2)
didn’t(2)
like(2)
Clash(2)
innovative(2)
Meanwhile(1)
somewhat(1)
different(1)
path(1)
It(1)
selected(1)
5(1)
nominees(1)
10(1)
selections(1)
differentiate(1)
between(1)
along(1)
built(1)
watch(1)
chooses(1)
nominated(1)
Apple(1)
reflected(1)
group(1)
developers(1)
where(1)
But(1)
taking(1)
advantage(1)
variety(1)
services(1)
winners(1)
selecting(1)
Services(1)
plus(1)
did(1)
job(1)
implementing(1)
newer(1)
language(1)
model(1)
Apple’s(1)
And(1)
limit(1)
itself(1)
follows(1)
one(1)
category(1)
focused(1)
instead(1)
lists(1)
numbers(1)
chose(1)
large(1)
angles(1)
rounded(1)
While(1)
conference(1)
developer(1)
its(1)
Early(1)
Adopter(1)
first-ever(1)
move(1)
results(1)
likely(1)
meant(1)
signal(1)
all(1)
creative(1)
announced(1)
exclusive(1)
according(1)
Android(1)
first(1)
live-streamed(1)
ceremony(1)
overall(1)
change(1)
On(1)
stage(1)
spoke(1)
Play’s(1)
evolution(1)
noted(1)
online(1)
storefront(1)
now(1)
reaches(1)
is(1)
resource(1)
users(1)
daily(1)
Apps(1)
obviously(1)
huge(1)
draw(1)
store(1)
earlier(1)
week(1)
said(1)
installs(1)
reached(1)
65(1)
past(1)
decorating(1)
chosen(1)
by(1)
panel(1)
experts(1)
rated(1)
factors(1)
quality(1)
To(1)
be(1)
considered(1)
either(1)
needed(1)
have(1)
major(1)
update(1)
last(1)
12(1)
months(1)
ended(1)
App(1)
title(1)
beating(1)
out(1)
BuzzFeed(1)
News(1)
Colorfy(1)
TuneIn(1)
Radio(1)
Yummly(1)
took(1)
hugely(1)
popular(1)
Interior(1)
Royale(1)
walked(1)
away(1)
crown(1)
topping(1)
Alphabear(1)
Kings(1)
MARVEL(1)
Future(1)
Fight(1)
Star(1)
Wars(1)
Galaxy(1)
Heroes(1)
From(1)
startup(1)
world(1)
smart(1)
airfare(1)
prediction(1)
engine(1)
Hopper(1)
won(1)
millennial-focused(1)
investing(1)
Robinhood(1)
grabbed(1)
win(1)
```

人の名前とかアポストロフィとか大文字小文字の扱いとかいろいろ考えなくちゃならなそう。

ちょっと改良しました。
⇒ [Rubyで英語記事に含まれてる英単語を数えて出現数順にソートする（改良１）](http://qiita.com/murata0705/items/fc678fc6170eefb5a174)

**※追記**
コメントをいただいて修正。

```test.rb
array = []
hash = Hash.new(0) # キーがなかったときの値 を 0 に

File.open('test.txt', 'r') do |word|
  word.each_line do |line|
    array.push line.split("\s").map{|m| m.gsub(/\.|\”|\,|\“/, "")}
  end
end

array = array.flatten.compact.delete_if(&:empty?)
puts "文字数：#{array.count}"
puts "----------------------------------------------------------------------------------------------------"

array.each do |word|
  hash[word] += 1
end

hash.sort_by{|_, v| -v}.each do |word, frequency|
  puts "%3d %s" % [frequency, word]
end
```