用Ruby更改段落中的每个单词

用Ruby更改段落中的每个单词,ruby,algorithm,paragraphs,Ruby,Algorithm,Paragraphs,所以我用Ruby编写代码,我有几句话: The sky above the port was the color of television, tuned to a dead channel. "It's not like I'm using," Case heard someone say, as he shouldered his way through the crowd around the door of the Chat. "It's like my body's developed

所以我用Ruby编写代码,我有几句话:

The sky above the port was the color of television, tuned to a dead channel. "It's not like I'm using," Case heard someone say, as he shouldered his way through the crowd around the door of the Chat. "It's like my body's developed this massive drug deficiency." It was a Sprawl voice and a Sprawl joke. The Chatsubo was a bar for professional expatriates; you could drink there for a week and never hear two words in Japanese.
我需要在不改变结构的情况下修改段落中的每个单词。我最初的想法是将空格分开,然后重新加入,但问题是你也会得到标点符号。如果你为了得到这个单词而拆分,那么很难重新合并,因为你不知道正确的标点符号

有没有比传统的拆分、映射、加入组合更好的方法?或者可能只是一个好的拆分正则表达式,以便很容易重新加入?

将gsub与块一起使用:

str = %q(The sky above the port was the color of television, tuned to a dead channel.
"It's not like I'm using," Case heard someone say, as he shouldered his way through the crowd
around the door of the Chat. "It's like my body's developed this massive drug deficiency."
It was a Sprawl voice and a Sprawl joke. The Chatsubo was a bar for professional expatriates;
you could drink there for a week and never hear two words in Japanese.)

puts str.gsub(/\w+/){|word| word.tr('aeiou','uoaei') }

结果:

Tho sky ubevo tho pert wus tho celer ef tolovasaen, tinod te u doud chunnol.
"It's net lako I'm isang," Cuso hourd semoeno suy, us ho sheildorod has wuy threigh tho crewd
ureind tho deer ef tho Chut. "It's lako my bedy's dovolepod thas mussavo drig dofacaoncy."
It wus u Spruwl veaco und u Spruwl jeko. Tho Chutsibe wus u bur fer prefossaenul oxputrautos;
yei ceild drank thoro fer u wook und novor hour twe werds an Jupunoso.
这个#tr方法在没有regex的情况下也可以工作,但是你知道了。

使用带块的gsub:

str = %q(The sky above the port was the color of television, tuned to a dead channel.
"It's not like I'm using," Case heard someone say, as he shouldered his way through the crowd
around the door of the Chat. "It's like my body's developed this massive drug deficiency."
It was a Sprawl voice and a Sprawl joke. The Chatsubo was a bar for professional expatriates;
you could drink there for a week and never hear two words in Japanese.)

puts str.gsub(/\w+/){|word| word.tr('aeiou','uoaei') }

结果:

Tho sky ubevo tho pert wus tho celer ef tolovasaen, tinod te u doud chunnol.
"It's net lako I'm isang," Cuso hourd semoeno suy, us ho sheildorod has wuy threigh tho crewd
ureind tho deer ef tho Chut. "It's lako my bedy's dovolepod thas mussavo drig dofacaoncy."
It wus u Spruwl veaco und u Spruwl jeko. Tho Chutsibe wus u bur fer prefossaenul oxputrautos;
yei ceild drank thoro fer u wook und novor hour twe werds an Jupunoso.

好的,这个#tr方法在没有正则表达式的情况下可以工作,但是你明白了。

我会用正则表达式匹配单词边界之间的单词,以避免影响标点或空格,例如:

s = "This is a test, ok?  Yes, fine!"
s.gsub!(/\b(\w+)\b/) {|x| "_#{x}_"}
s = "_This_ _is_ _a_ _test_, _ok_?  _Yes_, _fine_!"

我会用正则表达式匹配单词边界之间的单词,以避免影响标点或空格,例如:

s = "This is a test, ok?  Yes, fine!"
s.gsub!(/\b(\w+)\b/) {|x| "_#{x}_"}
s = "_This_ _is_ _a_ _test_, _ok_?  _Yes_, _fine_!"

我想这取决于你想对每个单词做什么……你想让这个句子有意义吗?因为否则你可以把空格和所有已知的标点分开,替换所有的记号,然后再回答。我想这取决于你想对每个单词做什么……你想让这个句子有意义吗?因为否则你可以把空格和所有已知的标点符号分开,替换掉所有的记号,然后回答。完全没有在文档中看到gsub。我一定是瞎了。而且,%q非常棒,非常好。谢谢哦,太棒了。完全没有在文档中看到gsub。我一定是瞎了。而且,%q非常棒,非常好。谢谢这与上面的答案非常相似,只是正则表达式略有不同。为什么你选择了
/\b(\w+)\b/
而不是
/\w+/
?+1个比我更好的改变单词的例子。但正如Icco所说,为什么要使用复杂的正则表达式?这与上面的答案非常相似,只是正则表达式略有不同。为什么你选择了
/\b(\w+)\b/
而不是
/\w+/
?+1个比我更好的改变单词的例子。但正如Icco所说,为什么要使用复杂的正则表达式?