用Ruby找到句子中的常用词

时间:2021-10-12 12:46:23

I have a task to find words that are in each sentence.

我的任务是找到每个句子中的单词。

Given a string and we want to divide the string into sentences and then determine which words, if any, are in all the sentences.

给定一个字符串,我们想要将字符串分割成句子,然后确定所有句子中的哪个单词(如果有的话)。

Here is my solution:

这是我的解决方案:

# encoding: utf-8
text = ''
File.foreach("lab2.in") do |line|
    text += line
end
hash = Hash.new
text = text.gsub(/[\n,]/,'').split(/[!.?]/)
number = 0
text.each do |sen|
        number += 1
        words = sen.split(/ /)
        words.each do |word|
                if hash[word]
                        hash[word] += "#{number}"
                else
                        hash[word] = "#{number}"
                end
        end
end
flag = false
needle = ''
count = text.length
for i in 1..count
        needle += "#{i}"
end
hash.each do |word|
        if word[1].squeeze == needle
                puts "this word is \"#{word[0]}\""
                flag = true
        end
end
if !flag
        puts "There no such word"
end

How this task can be solved maybe more prettily? I'm interested in Ruby library methods. A simple solution, like character-by-character cycle I already know.

怎样才能更巧妙地解决这个问题呢?我对Ruby库方法感兴趣。一个简单的解决方案,比如我已经知道的逐字符循环。

For example, with input like:

例如,输入如下:

lorem ipsum dolor and another lorem! sit amet lorem? and another lorem.

The output will be:

的输出将会是:

this word is "lorem"

1 个解决方案

#1


5  

You could do this (I modified your example slightly):

你可以这样做(我稍微修改了一下你的例子):

str = "a lorem ipsum lorem dolor sit amet. a tut toje est lorem! a i tuta toje lorem?"  

 str.split(/[.!?]/).map(&:split).reduce(:&)
  #=> ["a", "lorem"] 

We have:

我们有:

d = str.split(/[.!?]/)
  #=> ["a lorem ipsum lorem dolor sit amet",
  #    " a tut toje est lorem",
  #    " a i tuta toje lorem"] 
e = d.map(&:split)
  #=> [["a", "lorem", "ipsum", "lorem", "dolor", "sit", "amet"],
  #    ["a", "tut", "toje", "est", "lorem"],
  #    ["a", "i", "tuta", "toje", "lorem"]] 
e.reduce(:&)
  #=> ["a", "lorem"] 

To make it case-insensitive, change str.split... to str.downcase.split....

要使它不区分大小写,请更改string .split…对str.downcase.split ....

#1


5  

You could do this (I modified your example slightly):

你可以这样做(我稍微修改了一下你的例子):

str = "a lorem ipsum lorem dolor sit amet. a tut toje est lorem! a i tuta toje lorem?"  

 str.split(/[.!?]/).map(&:split).reduce(:&)
  #=> ["a", "lorem"] 

We have:

我们有:

d = str.split(/[.!?]/)
  #=> ["a lorem ipsum lorem dolor sit amet",
  #    " a tut toje est lorem",
  #    " a i tuta toje lorem"] 
e = d.map(&:split)
  #=> [["a", "lorem", "ipsum", "lorem", "dolor", "sit", "amet"],
  #    ["a", "tut", "toje", "est", "lorem"],
  #    ["a", "i", "tuta", "toje", "lorem"]] 
e.reduce(:&)
  #=> ["a", "lorem"] 

To make it case-insensitive, change str.split... to str.downcase.split....

要使它不区分大小写,请更改string .split…对str.downcase.split ....