小编ska*_*ad0的帖子

用Ruby查找句子中的常用词

我有一个任务是找到每个句子中的单词.

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

这是我的解决方案:

# 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 …
Run Code Online (Sandbox Code Playgroud)

ruby

2
推荐指数
1
解决办法
229
查看次数

Splice只在部分时间工作

这里我有一个数组未定义的元素.我试图打印这个数组的随机元素并剪切它.在这里我的代码.

function rand(min, max){
   return (Math.floor(Math.random() * (max - min + 1)) + min).toFixed(0);
}
$('#do').click(function(){
    var count = chamarr.length;
    var num = 0;
    if (count == 1) {
      $('#output').html('Nothing can found');
    } else {
      num = rand(1,chamarr.length);
      $('#output').html(chamarr[num]);
      chamarr.splice(num,1);
    }
 });
Run Code Online (Sandbox Code Playgroud)

当我记录一个阵列被切割时,我看到它总是好的,但有时候元素没有切割!

javascript splice

0
推荐指数
1
解决办法
181
查看次数

标签 统计

javascript ×1

ruby ×1

splice ×1