<commit_before>//
//  AppDelegate.swift
//  NumberPad
//
//  Created by <NAME> on 12/13/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit
import DigitRecognizerSDK

let filePrefix = "SavedLibraries-"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var rootViewController: ViewController!


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.rootViewController = window?.rootViewController as ViewController
        
        if let path = NSBundle.mainBundle().pathForResource("bridger_train", ofType: "json") {
            loadData(path)
        }
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        saveData()
    }
    
    func documentsDirectory() -> String {
        return NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
    }
    
    func saveData() {
        let dataToSave = self.rootViewController.digitClassifier.dataToSave(true, saveNormalizedData: true)
        
        var saveNumber = 1
        if let lastSave = newestSavedData() {
            if let lastNumber = lastSave.substringFromIndex(filePrefix.endIndex).toInt() {
                saveNumber = lastNumber + 1
            }
        }
        
        let documentName = self.documentsDirectory().stringByAppendingPathComponent( filePrefix + String(saveNumber))
        
        NSJSONSerialization.dataWithJSONObject(dataToSave, options: nil, error: nil)!.writeToFile(documentName, atomically: false)
    }
    
    func loadData(path: String) {
        if let jsonLibrary = DTWDigitClassifier.jsonLibraryFromFile(path) {
            self.rootViewController.digitClassifier.loadData(jsonLibrary, loadNormalizedData: false)
        }
    }
    
    
    func newestSavedData() -> String? {
        let contents = NSFileManager.defaultManager().contentsOfDirectoryAtPath(self.documentsDirectory(), error: nil)
        if contents != nil {
            if let contents = contents as? [String] {
                let contents = contents.filter({ string in
                    return string.hasPrefix(filePrefix)
                }).sorted({ (string1, string2) in
                    return string1.compare(string2) == NSComparisonResult.OrderedAscending
                })
                
                return contents.last
            }<commit_msg>Load normalized data in Numberpad.<commit_after>//
//  AppDelegate.swift
//  NumberPad
//
//  Created by <NAME> on 12/13/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit
import DigitRecognizerSDK

let filePrefix = "SavedLibraries-"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var rootViewController: ViewController!


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.rootViewController = window?.rootViewController as ViewController
        
        if let path = NSBundle.mainBundle().pathForResource("bridger_train", ofType: "json") {
            loadData(path)
        }
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        saveData()
    }
    
    func documentsDirectory() -> String {
        return NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
    }
    
    func saveData() {
        let dataToSave = self.rootViewController.digitClassifier.dataToSave(true, saveNormalizedData: true)
        
        var saveNumber = 1
        if let lastSave = newestSavedData() {
            if let lastNumber = lastSave.substringFromIndex(filePrefix.endIndex).toInt() {
                saveNumber = lastNumber + 1
            }
        }
        
        let documentName = self.documentsDirectory().stringByAppendingPathComponent( filePrefix + String(saveNumber))
        
        NSJSONSerialization.dataWithJSONObject(dataToSave, options: nil, error: nil)!.writeToFile(documentName, atomically: false)
    }
    
    func loadData(path: String) {
        if let jsonLibrary = DTWDigitClassifier.jsonLibraryFromFile(path) {
            self.rootViewController.digitClassifier.loadData(jsonLibrary, loadNormalizedData: true)
        }
    }
    
    
    func newestSavedData() -> String? {
        let contents = NSFileManager.defaultManager().contentsOfDirectoryAtPath(self.documentsDirectory(), error: nil)
        if contents != nil {
            if let contents = contents as? [String] {
                let contents = contents.filter({ string in
                    return string.hasPrefix(filePrefix)
                }).sorted({ (string1, string2) in
                    return string1.compare(string2) == NSComparisonResult.OrderedAscending
                })
                
                return contents.last
            }
{'fraction_non_alphanumeric': 0.08605750295391887, 'fraction_numerical': 0.00669554942890902, 'mean_word_length': 2.581805359661495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '1331530', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1304, 'n_words': 290}