<commit_before>//
//  Parser.swift
//  nstack-translations-generator
//
//  Created by <NAME> on 14/02/16.
//  Copyright © 2016 Nodes. All rights reserved.
//

import Foundation

struct ParserOutput {
    var JSON: [String: AnyObject]
    var mainKeys: [String]
    var language: [String: AnyObject]
    var isFlat: Bool
}

struct Parser {
    static func parseResponseData(_ data: Data) throws -> ParserOutput {
        let object = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions(rawValue: 0))
        guard let dictionary = object as? [String: AnyObject] else {
            throw NSError(domain: Generator.errorDomain, code: ErrorCode.parserError.rawValue, userInfo:
                [NSLocalizedDescriptionKey : "The data isn't in the correct format. Translations JSON file should have a dictionary as it's root object."])
        }

        var content: [String: AnyObject]? = dictionary
        content = content?["data"] as? [String : AnyObject] ?? content
        if let t = content?["Translation"] as? [String : AnyObject] {
            content = t
        } else if let t = content?["translations"] as? [String : AnyObject] {
            content = t
        }
        
        guard let langsDictionary = content, let first = langsDictionary.first else {
            throw NSError(domain: Generator.errorDomain, code: ErrorCode.parserError.rawValue, userInfo:
                [NSLocalizedDescriptionKey : "Parsed JSON wasn't containing translations data."])
        }
        
        let testKey = first.key[first.key.startIndex..<first.key.index(first.key.startIndex, offsetBy: 2)] // substring 0..2
        let isWrappedInLanguages = Locale.isoLanguageCodes.contains(String(testKey))
        
        var language: [String: AnyObject]
        
        if isWrappedInLanguages, let val = first.value as? [String: AnyObject] {
            // the nested lang is value
            language = val
        } else {
            // the root obj is the translations
            language = langsDictionary
        }
<commit_msg>Fix incorrect guessing of wrapping of loc's inside lang dict<commit_after>//
//  Parser.swift
//  nstack-translations-generator
//
//  Created by <NAME> on 14/02/16.
//  Copyright © 2016 Nodes. All rights reserved.
//

import Foundation

struct ParserOutput {
    var JSON: [String: AnyObject]
    var mainKeys: [String]
    var language: [String: AnyObject]
    var isFlat: Bool
}

struct Parser {
    static func parseResponseData(_ data: Data) throws -> ParserOutput {
        let object = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions(rawValue: 0))
        guard let dictionary = object as? [String: AnyObject] else {
            throw NSError(domain: Generator.errorDomain, code: ErrorCode.parserError.rawValue, userInfo:
                [NSLocalizedDescriptionKey : "The data isn't in the correct format. Translations JSON file should have a dictionary as it's root object."])
        }

        var content: [String: AnyObject]? = dictionary
        content = content?["data"] as? [String : AnyObject] ?? content
        if let t = content?["Translation"] as? [String : AnyObject] {
            content = t
        } else if let t = content?["translations"] as? [String : AnyObject] {
            content = t
        }
        
        guard let langsDictionary = content, let first = langsDictionary.first else {
            throw NSError(domain: Generator.errorDomain, code: ErrorCode.parserError.rawValue, userInfo:
                [NSLocalizedDescriptionKey : "Parsed JSON wasn't containing translations data."])
        }

        // Check if key is either "en" or "en-UK"
        let isPossibleLanguageKey = first.key.count == 2 || (first.key.count == 5 && first.key.contains("-"))
        let testKey = first.key[first.key.startIndex..<first.key.index(first.key.startIndex, offsetBy: 2)] // substring 0..2
        let isWrappedInLanguages = isPossibleLanguageKey && Locale.isoLanguageCodes.contains(String(testKey))
        
        var language: [String: AnyObject]
        
        if isWrappedInLanguages, let val = first.value as? [String: AnyObject] {
            // the nested lang is value
            language = val
        } else {
            // the root obj is the translations
            language = langsDictionary
        }

{'fraction_non_alphanumeric': 0.09995414947271894, 'fraction_numerical': 0.0068775790921595595, 'mean_word_length': 2.5213882163034707, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3011402', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1179, 'n_words': 369}