<commit_before>//
//  OSRMTextInstructions.swift
//  Voyage
//
//  Created by <NAME> on 01.11.16.
//  Copyright © 2016 Mapbox. All rights reserved.
//

import Foundation
import MapboxDirections

// Will automatically read localized Instructions.plist
let OSRMTextInstructionsStrings = NSDictionary(contentsOfFile: Bundle.main.path(forResource: "Instructions", ofType: "plist")!)!

class OSRMTextInstructions {
    let version: String
    let instructions: [ String: Any ]

    internal init(version: String) {
        self.version = version
        self.instructions = OSRMTextInstructionsStrings[version] as! [ String: Any ]
    }

    func capitalizeFirstLetter(string: String) -> String {
        return String(string.characters.prefix(1)).uppercased() + String(string.characters.dropFirst())
    }

    func laneConfig(intersection: Intersection) -> String? {
        guard let approachLanes = intersection.approachLanes else {
            return ""
        }

        guard let useableApproachLanes = intersection.usableApproachLanes else {
            return ""
        }

        // find lane configuration
        var config = Array(repeating: "x", count: approachLanes.count)
        for index in useableApproachLanes {
            config[index] = "o"
        }

        // reduce lane configurations to common cases
        var current = ""
        return config.reduce("", {
            (result: String?, lane: String) -> String? in
            if (lane != current) {
                current = lane
                return result! + lane
            } else {
                return result
            }
        })
    }

    func directionFromDegree(degree: Int?) -> String {
        guard let degree = degree else {
            // step had no bearing_after degree, ignoring
            return ""
        }

        // fetch locatized compass directions strings
        let directions = (instructions["constants"] as! NSDictionary)["direction"] as! [ String: String ]

        // Transform degrees to their translated compass direction
        switch degree {
        case 340..<360, 0..<20:
            return directions["north"]!
        case 20..<70:
            return directions["northeast"]!
        case 70..<110:
            return directions["east"]!
        case 110..<160:
            return directions["southeast"]!
        case 160..<200:
            return directions["south"]!
        case 200..<250:
            return directions["southwest"]!
        case 250..<290:
            return directions["west"]!
        case 290..<340:
            return directions["northwest"]!
        default:
            return "";
        }
    }

    func compile(step: RouteStep) -> String? {
        var type = step.maneuverType
        let modifier = step.maneuverDirection?.description
        let mode = step.transportType

        if type != .depart && type != .arrive && modifier == nil {
            return nil
        }

        if instructions[type?.description ?? ""] as? NSDictionary == nil {
            // OSRM specification assumes turn types can be added without
            // major version changes. Unknown types are to be treated as
            // type `turn` by clients
            type = .turn<commit_msg>osrm-text-instructions bump (#488)<commit_after>//
//  OSRMTextInstructions.swift
//  Voyage
//
//  Created by <NAME> on 01.11.16.
//  Copyright © 2016 Mapbox. All rights reserved.
//

import Foundation
import MapboxDirections

// Will automatically read localized Instructions.plist
let OSRMTextInstructionsStrings = NSDictionary(contentsOfFile: Bundle.main.path(forResource: "Instructions", ofType: "plist")!)!

class OSRMTextInstructions {
    let version: String
    let instructions: [ String: Any ]

    internal init(version: String) {
        self.version = version
        self.instructions = OSRMTextInstructionsStrings[version] as! [ String: Any ]
    }

    func capitalizeFirstLetter(string: String) -> String {
        return String(string.characters.prefix(1)).uppercased() + String(string.characters.dropFirst())
    }

    func laneConfig(intersection: Intersection) -> String? {
        guard let approachLanes = intersection.approachLanes else {
            return ""
        }

        guard let useableApproachLanes = intersection.usableApproachLanes else {
            return ""
        }

        // find lane configuration
        var config = Array(repeating: "x", count: approachLanes.count)
        for index in useableApproachLanes {
            config[index] = "o"
        }

        // reduce lane configurations to common cases
        var current = ""
        return config.reduce("", {
            (result: String?, lane: String) -> String? in
            if (lane != current) {
                current = lane
                return result! + lane
            } else {
                return result
            }
        })
    }

    func directionFromDegree(degree: Int?) -> String {
        guard let degree = degree else {
            // step had no bearing_after degree, ignoring
            return ""
        }

        // fetch locatized compass directions strings
        let directions = (instructions["constants"] as! NSDictionary)["direction"] as! [ String: String ]

        // Transform degrees to their translated compass direction
        switch degree {
        case 340..<360, 0...20:
            return directions["north"]!
        case 20..<70:
            return directions["northeast"]!
        case 70...110:
            return directions["east"]!
        case 110..<160:
            return directions["southeast"]!
        case 160...200:
            return directions["south"]!
        case 200..<250:
            return directions["southwest"]!
        case 250...290:
            return directions["west"]!
        case 290..<340:
            return directions["northwest"]!
        default:
            return "";
        }
    }

    func compile(step: RouteStep) -> String? {
        var type = step.maneuverType
        let modifier = step.maneuverDirection?.description
        let mode = step.transportType

        if type != .depart && type != .arrive && modifier == nil {
            return nil
        }

        if instructions[type?.description ?? ""] as? NSDictionary == nil {
            // OSRM specification assumes turn types can be added without
            // major version changes. Unknown types are to be treated as
            // type `turn` by clients
            type = .turn
{'fraction_non_alphanumeric': 0.10269438029253272, 'fraction_numerical': 0.018629715165511932, 'mean_word_length': 1.9621523027815777, 'pattern_counts': {'":': 0, '<': 19, '<?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': '2865777', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1633, 'n_words': 481}