Ruby code to parse and combine text files

I use this ruby code to parse several tab-delimitted text files that contain individual raters’ perceptions of a target (in this case a video). The rater id is embedded in the filename. The target video number is also embedded in the filename.


1
<br />#! /usr/bin/env ruby<br /><br />out = Dir.glob('*.txt')<br /><br /># open the file to write to and add the column headers<br />columns = "grouptratertmintengagetpreparetdivergetconvergetexecutetcentralizetattentivettonetactivationn" <br />File.open("./all_ratings.txt", 'w') { |f| f.write(columns) }<br /><br />out.each do |filename|<br />  rater = filename.split('.')[0].split('_')[0]<br />  group = filename.split('.')[0].split('_')[1]  <br /> <br />  # Assign a number for the rater<br />  case rater.downcase<br />    when "rater1"<br />      rater_id = 1<br />    when "rater2"<br />      rater_id = 2<br />    when "rater3"<br />      rater_id = 3<br />    when "rater4"<br />      rater_id = 4<br />    end<br />    puts "rater: " + rater + "(#{rater_id})" + " group: " + group<br /><br />    # Open the file<br />    f = File.open(filename, "r").read<br /> <br />    # Split by lines - This will make sure that the end of line from Mac Classic is n<br />    str = f.gsub!(/rn?/, "n").split("n")<br /> <br />    # Identify the line number that starts the data entry for this file by finding a specific expression in the text of the file<br /> <br />    linenum = 0<br />    exp = "- Low marked by sluggishness"<br />    line = str[linenum]<br />    puts line<br />    until line.include?(exp)    <br />      line = str[linenum] <br />      linenum += 1<br />    end<br /> <br />    linenum.upto(linenum+30) do |currentline|<br />      min = (currentline-linenum)+1<br />      # add the rater_id and the group_id to the line<br />      line = group.to_s + "t" + rater_id.to_s + "t" + str[currentline] + "n"<br />      File.open("./all_ratings.txt", 'a') { |f| f.write(line) }<br />    end<br />end<br /><br />