<commit_before>//
//  ViewController.swift
//  Spreken
//
//  Created by <NAME> on 9/10/16.
//  Copyright © 2016 manderson-productions. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    fileprivate static let kTodoCellIdentifier = "todoCellIdentifier"
    fileprivate var speechInput: SpeechInput!
    fileprivate var speechOutput: SpeechOutput!
    fileprivate var todos = [Todo]()

    @IBOutlet weak var recordButton: UIButton!
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.recordButton.isEnabled = false

        let localeIdentifier = "en-US"
        self.speechInput = SpeechInput(locale: Locale(identifier: localeIdentifier))
        self.speechInput.authorize { (status) in
            DispatchQueue.main.async {
                switch status {
                case .authorized:
                    print("Authorized")
                    self.recordButton.isEnabled = true
                    break
                case .denied:
                    print("Denied")
                    break
                case .notDetermined:
                    print("Not Determined")
                    break
                case .restricted:
                    print("Restricted")
                    break
                }
            }
        }

        let voiceFilter = AVSpeechSynthesisVoice.speechVoices().filter { $0.language == "en-US" }
        if let voice = AVSpeechSynthesisVoice(language: voiceFilter[0].language) {
            self.speechOutput = SpeechOutput(voice: voice)
        } else {
            fatalError("Voice not found!")
        }
    }

    // MARK: IBActions

    @IBAction func beginRecording(_ sender: UIButton) {
        let result = self.speechInput.beginRecognition()
        if !result {
            print("Some problem occurred with recognition startup...")
        }
        self.speechInput.recognitionHandler = { [weak self] bestGuess in
            guard let strongself = self else { return }
            strongself.speechOutput.speak(string: bestGuess)
            strongself.todos.insert(Todo(title: bestGuess, timestamp: Date()), at: 0)
            let indexPath = IndexPath(row: 0, section: 0)
            strongself.tableView.insertRows(at: [indexPath], with: .automatic)
        }
    }

    @IBAction func endRecording(_ sender: UIButton) {
        self.speechInput.endRecognition()
    }

    // MARK: UITableViewDelegate/Datasource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ViewController.kTodoCellIdentifier, for: indexPath)
        let todo = todos[indexPath.row]
        cell.textLabel?.text = todo.title
        cell.detailTextLabel?.text = DateFormat.sharedInstance.stringFromDate(date: todo.timestamp)

        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected row at: \(indexPath)")
        tableView.deselectRow(at: indexPath, animated: true)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.todos.count
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            self.todos.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

    func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
}
<commit_msg>Allow editing via voice recognition 'edit'<commit_after>//
//  ViewController.swift
//  Spreken
//
//  Created by <NAME> on 9/10/16.
//  Copyright © 2016 manderson-productions. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    fileprivate static let kTodoCellIdentifier = "todoCellIdentifier"
    fileprivate var speechInput: SpeechInput!
    fileprivate var speechOutput: SpeechOutput!
    fileprivate var todos = [Todo]()

    @IBOutlet weak var recordButton: UIButton!
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.recordButton.isEnabled = false

        let localeIdentifier = "en-US"
        self.speechInput = SpeechInput(locale: Locale(identifier: localeIdentifier))
        self.speechInput.authorize { (status) in
            DispatchQueue.main.async {
                switch status {
                case .authorized:
                    print("Authorized")
                    self.recordButton.isEnabled = true
                    break
                case .denied:
                    print("Denied")
                    break
                case .notDetermined:
                    print("Not Determined")
                    break
                case .restricted:
                    print("Restricted")
                    break
                }
            }
        }

        let voiceFilter = AVSpeechSynthesisVoice.speechVoices().filter { $0.language == "en-US" }
        if let voice = AVSpeechSynthesisVoice(language: voiceFilter[0].language) {
            self.speechOutput = SpeechOutput(voice: voice)
        } else {
            fatalError("Voice not found!")
        }
    }

    fileprivate func setTableViewEditing(editing: Bool) {
        self.tableView.setEditing(editing, animated: true)
    }

    // MARK: LongPressGesture

    func longPressGestureHandler(recognizer: UILongPressGestureRecognizer) {
        if recognizer.state == .began {
            self.setTableViewEditing(editing: !self.tableView.isEditing)
        }
    }

    // MARK: Object Control

    fileprivate func shouldSetEditingMode(string: String) -> Bool {
        return string.lowercased() == "edit"
    }

    // MARK: IBActions

    @IBAction func beginRecording(_ sender: UIButton) {
        let result = self.speechInput.beginRecognition()
        if !result {
            print("Some problem occurred with recognition startup...")
        }
        self.speechInput.recognitionHandler = { [weak self] bestGuess in
            guard let strongself = self else { return }

            if strongself.shouldSetEditingMode(string: bestGuess) {
                strongself.tableView.setEditing(!strongself.tableView.isEditing, animated: true)
                return
            }
            strongself.speechOutput.speak(string: bestGuess)
            strongself.todos.insert(Todo(title: bestGuess, timestamp: Date()), at: 0)
            let indexPath = IndexPath(row: 0, section: 0)
            strongself.tableView.insertRows(at: [indexPath], with: .automatic)
        }
    }

    @IBAction func endRecording(_ sender: UIButton) {
        self.speechInput.endRecognition()
    }

    // MARK: UITableViewDelegate/Datasource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ViewController.kTodoCellIdentifier, for: indexPath)
        let todo = todos[indexPath.row]
        cell.textLabel?.text = todo.title
        cell.detailTextLabel?.text = DateFormat.sharedInstance.stringFromDate(date: todo.timestamp)
        let longpressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureHandler(recognizer:)))
        longpressGesture.minimumPressDuration = 0.4
        longpressGesture.cancelsTouchesInView = false
        cell.addGestureRecognizer(longpressGesture)
        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected row at: \(indexPath)")
        tableView.deselectRow(at: indexPath, animated: true)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.todos.count
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            self.todos.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

    func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
        return true
    }
}

{'fraction_non_alphanumeric': 0.08827539195637356, 'fraction_numerical': 0.0036355373778686664, 'mean_word_length': 2.3319454958364876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '2333231', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2325, 'n_words': 553}