<commit_before>//
//  CSVReader.swift
//  CSVReader
//
//  Created by <NAME> on 06/03/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public class CSVReader {
    
    private var _numberOfColumns: Int
    private var _numberOfRows: Int
    private var _lines = [String]()
    public var headers = [String]()
    
    public var numberOfColumns: Int {
        get {
            return _numberOfColumns
        }
    }
    
    public var numberOfRows: Int {
        get {
            return _numberOfRows
        }
    }
    
    init(fileName: String) {
        let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "csv")
        var csv = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
        csv = csv.stringByReplacingOccurrencesOfString("\r", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
        _lines = csv.componentsSeparatedByString("\n")
        _numberOfColumns = _lines[0].componentsSeparatedByString(",").count
        _numberOfRows = _lines.count
        headers = _lines[0].componentsSeparatedByString(",")
    }
    
    public func row(rowId: Int) -> [String: String] {
        var row = [String: String]()
        let vals = _lines[rowId].componentsSeparatedByString(",")
        var i = 0
        for header in headers {
            row[header] = vals[i++]
        }
        return row
    }
    
    // To do
    public func column(columnId: Int) {
        
    }
    <commit_msg>Changed the way that rows and columns are accessed. Added column support. Updated README example. Also -1 from row count to take headers into account.<commit_after>//
//  CSVReader.swift
//  CSVReader
//
//  Created by <NAME> on 06/03/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public class CSVReader {
    
    private var _numberOfColumns: Int
    private var _numberOfRows: Int
    private var _lines = [String]()
    public var headers = [String]()
    public var columns = [String: [String]]()
    public var rows = [[String: String]]()
    
    public var numberOfColumns: Int {
        get {
            return _numberOfColumns
        }
    }
    
    public var numberOfRows: Int {
        get {
            return _numberOfRows - 1
        }
    }
    
    init(fileName: String) {
        let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "csv")
        var csv = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
        csv = csv.stringByReplacingOccurrencesOfString("\r", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
        _lines = csv.componentsSeparatedByString("\n")
        _numberOfColumns = _lines[0].componentsSeparatedByString(",").count
        _numberOfRows = _lines.count
        headers = _lines[0].componentsSeparatedByString(",")
        setRows()
        setColumns()
    }
    
    private func setRows() {
        var rows = [[String: String]]()
        for i in 1..<_numberOfRows {
            var row = [String: String]()
            let vals = _lines[i].componentsSeparatedByString(",")
            var i = 0
            for header in headers {
                row[header] = vals[i++]
            }
            rows.append(row)
        }
        self.rows = rows
    }
    
    private func setColumns() {
        var columns = [String: [String]]()
        for header in headers {
            var colValue = [String]()
            for row in rows {
                colValue.append(row[header]!)
            }
            columns[header] = colValue
        }
        self.columns = columns
    }
    
{'fraction_non_alphanumeric': 0.10709394686387291, 'fraction_numerical': 0.00958641468090934, 'mean_word_length': 2.094915254237288, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '5416057', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1043, 'n_words': 280}