<commit_before>require 'csv'
require_relative 'parse_line'
require_relative 'get_column_widths'

filename = ARGV[0]

if filename == nil then
	puts "Usage: ruby parse_file.rb filename.txt."
	Process.exit(1)
end

if not File.file?(filename) then
	puts "Filename '" + filename + "' doesn't seem to exist or is not a file."
	Process.exit(2)
end

column_names_line = nil
column_names = nil
column_sizes = nil

CSV do |csv|
	File.open(filename, "r").each do |line|
		next if /^Records affected/ =~ line  # Last line in database export.

		next if /^\s*$/ =~ line
		if /^[=\s]+$/ =~ line then
			next if column_sizes != nil
			column_sizes = get_column_widths line
			column_names = extract_values column_names_line, column_sizes
			csv << column_names
			next
		end

		next if line == column_names_line

		if column_sizes != nil then
			csv_values = extract_values line, column_sizes
			csv << csv_values
		else
			column_names_line = line
		end
	end
end<commit_msg>Enforce win1250 encoding<commit_after>require 'csv'
require_relative 'parse_line'
require_relative 'get_column_widths'

filename = ARGV[0]

if filename == nil then
	puts "Usage: ruby parse_file.rb filename.txt."
	Process.exit(1)
end

if not File.file?(filename) then
	puts "Filename '" + filename + "' doesn't seem to exist or is not a file."
	Process.exit(2)
end

column_names_line = nil
column_names = nil
column_sizes = nil

CSV do |csv|
	File.open(filename, "r", :encoding => 'windows-1250').each do |line|
		next if /^Records affected/ =~ line  # Last line in database export.

		next if /^\s*$/ =~ line
		if /^[=\s]+$/ =~ line then
			next if column_sizes != nil
			column_sizes = get_column_widths line
			column_names = extract_values column_names_line, column_sizes
			csv << column_names
			next
		end

		next if line == column_names_line

		if column_sizes != nil then
			csv_values = extract_values line, column_sizes
			csv << csv_values
		else
			column_names_line = line
		end
	end
end
{'fraction_non_alphanumeric': 0.10153061224489796, 'fraction_numerical': 0.007142857142857143, 'mean_word_length': 3.890274314214464, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655489', 'n_tokens_mistral': 745, 'n_tokens_neox': 674, 'n_words': 209}