LoginSignup
2
1

More than 5 years have passed since last update.

Rubyで言語処理100本ノック 第1章:準備運動 (03 ~ 05)

Posted at

引き続き、Rubyで言語処理100本ノックにチャレンジしたいと思います。

03. 円周率

"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.

word = "Now I need a drink, alcoholic of course, 
        after the heavy lectures involving quantum mechanics."

puts word.split(/\W+/).map(&:size)
結果
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

答えが円周率に...!

正規表現 : \W
単語構成文字 [a-zA-Z_0-9] 以外の文字

04. 元素記号

"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,
取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.

word = "Hi He Lied Because Boron Could Not 
        Oxidize Fluorine. New Nations Might Also 
        Sign Peace Security Clause. Arthur King Can."

array = [1,5,6,7,8,9,15,16,19]
hash = {}

word.split(/\W+/).each.with_index(1) do |str, i|
  key = array.include?(i) ? str.slice(0) : str.slice(0..1)

  hash[key] = i
end

puts hash
結果
{"H"  => 1,  "He" => 2,  "Li" => 3,  "Be" => 4,  "B"  => 5, 
 "C"  => 6,  "N"  => 7,  "O"  => 8,  "F"  => 9,  "Ne" => 10, 
 "Na" => 11, "Mi" => 12, "Al" => 13, "Si" => 14, "P"  => 15, 
 "S"  => 16, "Cl" => 17, "Ar" => 18, "K"  => 19, "Ca" => 20}

05. n-gram

与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.
この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ.

class Array
  def n_gram(n)
    self.each_cons(n).map(&:join)
  end
end

word = "I am an NLPer"

# 単語bi-gram
puts word.split(/\W+/).n_gram(2)
# 文字bi-gram
puts word.chars.n_gram(2)
結果
# 単語bi-gram
["Iam", "aman", "anNLPer"]
# 文字bi-gram
["I ", " a", "am", "m ", " a", "an", "n ", " N", "NL", "LP", "Pe", "er"]

問題文の理解に苦戦しました。。
次回で第1章:準備運動の完結編にしたいと思います。

こちらは前回までの挑戦です。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1