質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

2091閲覧

rubyプログラミングの空白処理について

ruby_11

総合スコア37

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2017/06/21 06:54

編集2017/06/21 08:20

英語文章を分析するためにRubyを使ってプログラムを書いています。

また、英語はTEDのサイトのスクプトを使用しています。

実際に、単語と単語を空白で区切り、配列に格納する段階ですが、スクリプトをそのまま解析すると空白で区切ることができる部分とできない部分があります。

区切ることができない部分の空白を一度削除し、再び空白を作って実行すると上手く出力できるのですが、どこを改善すれば上手く出力できるのでしょうか?

スクリプトを保存する段階で問題があるのでしょうか?

例えば、出力結果1では、name doesn'tが空白で区切ることができていませんが、テキスト内で、1度空白を削除し、空白を作って出力すると、出力結果2のように上手く出力できます。

イメージ説明
イメージ説明
イメージ説明
イメージ説明

追加の質問です。

ruby

1filename = ARGV[0] 2file = File.open(filename) 3text = file.read 4file.close 5 6text1 = text.gsub(/\,|\./, "") 7column = text1.split(/ /) 8puts column 9print "I : ", column.select{|item1| item1 == "I"}.length, "\n"

0:11
Growing up, I didn't always understand why my parents made me follow the rules that they did. Like, why did I really have to mow the lawn? Why was homework really that important? Why couldn't I put jelly beans in my oatmeal?
0:26
My childhood was abound with questions like this. Normal things about being a kid and realizing that sometimes, it was best to listen to my parents even when I didn't exactly understand why. And it's not that they didn't want me to think critically. Their parenting always sought to reconcile the tension between having my siblings and I understand the realities of the world, while ensuring that we never accepted the status quo as inevitable.
0:50
I came to realize that this, in and of itself, was a very purposeful form of education. One of my favorite educators, Brazilian author and scholar Paulo Freire, speaks quite explicitly about the need for education to be used as a tool for critical awakening and shared humanity. In his most famous book, "Pedagogy of the Oppressed," he states, "No one can be authentically human while he prevents others from being so."
1:19
I've been thinking a lot about this lately, this idea of humanity, and specifically, who in this world is afforded the privilege of being perceived as fully human. Over the course of the past several months, the world has watched as unarmed black men, and women, have had their lives taken at the hands of police and vigilante. These events and all that has transpired after them have brought me back to my own childhood and the decisions that my parents made about raising a black boy in America that growing up, I didn't always understand in the way that I do now.
1:52
I think of how hard it must have been, how profoundly unfair it must have felt for them to feel like they had to strip away parts of my childhood just so that I could come home at night.
2:04
For example, I think of how one night, when I was around 12 years old, on an overnight field trip to another city, my friends and I bought Super Soakers and turned the hotel parking lot into our own water-filled battle zone. We hid behind cars, running through the darkness that lay between the streetlights, boundless laughter ubiquitous across the pavement. But within 10 minutes, my father came outside, grabbed me by my forearm and led me into our room with an unfamiliar grip. Before I could say anything, tell him how foolish he had made me look in front of my friends, he derided me for being so naive. Looked me in the eye, fear consuming his face, and said, "Son, I'm sorry, but you can't act the same as your white friends. You can't pretend to shoot guns. You can't run around in the dark. You can't hide behind anything other than your own teeth."
2:59
I know now how scared he must have been, how easily I could have fallen into the empty of the night, that some man would mistake this water for a good reason to wash all of this away.
3:12
These are the sorts of messages I've been inundated with my entire life: Always keep your hands where they can see them, don't move too quickly, take off your hood when the sun goes down. My parents raised me and my siblings in an armor of advice, an ocean of alarm bells so someone wouldn't steal the breath from our lungs, so that they wouldn't make a memory of this skin. So that we could be kids, not casket or concrete. And it's not because they thought it would make us better than anyone else it's simply because they wanted to keep us alive.
3:38
All of my black friends were raised with the same message, the talk, given to us when we became old enough to be mistaken for a nail ready to be hammered to the ground, when people made our melanin synonymous with something to be feared.
3:51
But what does it do to a child to grow up knowing that you cannot simply be a child? That the whims of adolescence are too dangerous for your breath, that you cannot simply be curious, that you are not afforded the luxury of making a mistake, that someone's implicit bias might be the reason you don't wake up in the morning.
4:09
But this cannot be what defines us. Because we have parents who raised us to understand that our bodies weren't meant for the backside of a bullet, but for flying kites and jumping rope, and laughing until our stomachs burst. We had teachers who taught us how to raise our hands in class, and not just to signal surrender, and that the only thing we should give up is the idea that we aren't worthy of this world. So when we say that black lives matter, it's not because others don't, it's simply because we must affirm that we are worthy of existing without fear, when so many things tell us we are not. I want to live in a world where my son will not be presumed guilty the moment he is born, where a toy in his hand isn't mistaken for anything other than a toy.
4:46
And I refuse to accept that we can't build this world into something new, some place where a child's name doesn't have to be written on a t-shirt, or a tombstone, where the value of someone's life isn't determined by anything other than the fact that they had lungs, a place where every single one of us can breathe.
5:03
Thank you.

上の英語テキストでIの単語数を出力しているのですが、実際にIの数は18個で、結果としては、15個しか出力できていません。

プログラムの改善点を教えていただけませんか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/06/21 07:16

対象のテキストファイルの中身を追記していただけると回答しやすいかもしれません。
ruby_11

2017/06/21 07:22

対象のテキストファイルの中身は2つ目の画像です。
退会済みユーザー

退会済みユーザー

2017/06/21 07:27

画像を手打ちしても良いですが、それだと恐らく再現しないと思います。p "name doesn't".split(/ /)を試してみましたが["name", "doesn't"]と出力されています。
ruby_11

2017/06/21 07:32

ありがとうございます。p "name doesn't".split(/ /)で区切ることができましたが、そのようなプログラムを書かなくても、初めから区切ることができるようなプログラムを書きたいのですが、どうすればいいでしょうか? 
退会済みユーザー

退会済みユーザー

2017/06/21 07:34

今、質問者様が示されているtext1の内容を手打ちして検証しようとしているので少々お待ちくださいませ。
guest

回答1

0

ベストアンサー

Ruby

1text =<<-EOS 24:46 3And I refuse to accept that we can't build this world into something new, some place where a child's name doesn't have to be written on a t-shirt, or a tombstone, where the value of someone's life isn't determined by anything other than the fact that they had lungs, a place where every single one of us can breathe. 4EOS 5 6text1 = text.gsub(/\,|\./, "") 7column = text1.split(/ /) 8 9puts column 10print "I:", column.select { |item1| item1 == "I" }.size, "\n" 11

実行結果例

4:46 And I refuse to accept that we can't build this world into something new some place where a child's name doesn't have to be written on a t-shirt or a tombstone where the value of someone's life isn't determined by anything other than the fact that they had lungs a place where every single one of us can breathe I:1

試してみました。スペースで分割できています。
質問者様のテキストファイルに原因があるような気がします。

# 追記

Ruby

1text =<<-EOS 20:11 3Growing up, I didn't always understand why my parents made me follow the rules that they did. Like, why did I really have to mow the lawn? Why was homework really that important? Why couldn't I put jelly beans in my oatmeal? 40:26 5My childhood was abound with questions like this. Normal things about being a kid and realizing that sometimes, it was best to listen to my parents even when I didn't exactly understand why. And it's not that they didn't want me to think critically. Their parenting always sought to reconcile the tension between having my siblings and I understand the realities of the world, while ensuring that we never accepted the status quo as inevitable. 60:50 7I came to realize that this, in and of itself, was a very purposeful form of education. One of my favorite educators, Brazilian author and scholar Paulo Freire, speaks quite explicitly about the need for education to be used as a tool for critical awakening and shared humanity. In his most famous book, "Pedagogy of the Oppressed," he states, "No one can be authentically human while he prevents others from being so." 81:19 9I've been thinking a lot about this lately, this idea of humanity, and specifically, who in this world is afforded the privilege of being perceived as fully human. Over the course of the past several months, the world has watched as unarmed black men, and women, have had their lives taken at the hands of police and vigilante. These events and all that has transpired after them have brought me back to my own childhood and the decisions that my parents made about raising a black boy in America that growing up, I didn't always understand in the way that I do now. 101:52 11I think of how hard it must have been, how profoundly unfair it must have felt for them to feel like they had to strip away parts of my childhood just so that I could come home at night. 122:04 13For example, I think of how one night, when I was around 12 years old, on an overnight field trip to another city, my friends and I bought Super Soakers and turned the hotel parking lot into our own water-filled battle zone. We hid behind cars, running through the darkness that lay between the streetlights, boundless laughter ubiquitous across the pavement. But within 10 minutes, my father came outside, grabbed me by my forearm and led me into our room with an unfamiliar grip. Before I could say anything, tell him how foolish he had made me look in front of my friends, he derided me for being so naive. Looked me in the eye, fear consuming his face, and said, "Son, I'm sorry, but you can't act the same as your white friends. You can't pretend to shoot guns. You can't run around in the dark. You can't hide behind anything other than your own teeth." 142:59 15I know now how scared he must have been, how easily I could have fallen into the empty of the night, that some man would mistake this water for a good reason to wash all of this away. 163:12 17These are the sorts of messages I've been inundated with my entire life: Always keep your hands where they can see them, don't move too quickly, take off your hood when the sun goes down. My parents raised me and my siblings in an armor of advice, an ocean of alarm bells so someone wouldn't steal the breath from our lungs, so that they wouldn't make a memory of this skin. So that we could be kids, not casket or concrete. And it's not because they thought it would make us better than anyone else it's simply because they wanted to keep us alive. 183:38 19All of my black friends were raised with the same message, the talk, given to us when we became old enough to be mistaken for a nail ready to be hammered to the ground, when people made our melanin synonymous with something to be feared. 203:51 21But what does it do to a child to grow up knowing that you cannot simply be a child? That the whims of adolescence are too dangerous for your breath, that you cannot simply be curious, that you are not afforded the luxury of making a mistake, that someone's implicit bias might be the reason you don't wake up in the morning. 224:09 23But this cannot be what defines us. Because we have parents who raised us to understand that our bodies weren't meant for the backside of a bullet, but for flying kites and jumping rope, and laughing until our stomachs burst. We had teachers who taught us how to raise our hands in class, and not just to signal surrender, and that the only thing we should give up is the idea that we aren't worthy of this world. So when we say that black lives matter, it's not because others don't, it's simply because we must affirm that we are worthy of existing without fear, when so many things tell us we are not. I want to live in a world where my son will not be presumed guilty the moment he is born, where a toy in his hand isn't mistaken for anything other than a toy. 244:46 25And I refuse to accept that we can't build this world into something new, some place where a child's name doesn't have to be written on a t-shirt, or a tombstone, where the value of someone's life isn't determined by anything other than the fact that they had lungs, a place where every single one of us can breathe. 265:03 27Thank you. 28EOS 29 30p text.split(' ').count('I') # => 18

プログラム全体

Ruby

1# encoding: utf-8 2 3column = open(ARGV[0], &:read).delete(',.').split(' ') 4puts column 5puts "I:#{column.count('I')}" 6

対象になっているページ
アメリカで黒人の息子を育てる方法

投稿2017/06/21 07:48

編集2017/06/21 08:39
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ruby_11

2017/06/21 08:04

もう一度TEDからテキストをダウンロードし、出力してみると上手くいきました。 ありがとうございます。
退会済みユーザー

退会済みユーザー

2017/06/21 08:07

いえいえ。どういたしまして。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問