<commit_before>//
//  Message.swift
//  FBChatbot
//
//  Created by <NAME> on 30/05/17.
//
//

import Foundation
import CoreLocation
import JSON

class Message {
    var id: String?
    var text: String?
    var sender: String?
    var timeStamp: Int?
    var sequence: Int?
    var attachments: [Attachment]?
    var description: String {
        return getDescription()
    }
    
    struct Keys {
        static let ID = "mid"
        static let MessagingJSON = "messaging"
        static let MessageJSON = "message"
        static let Sequence = "seq"
        static let Text = "text"
        static let AttachmentsArray = "attachments"
        static let Sender = "sender"
        static let SenderID = "id"
        static let TimeStamp = "timestamp"
    }
    
    init() {}
    
    init(messagingJSON: JSON) {
        var auxString: String?
        if let messageJSON = messagingJSON[Keys.MessageJSON] {
            auxString = messageJSON[Keys.Sequence]?.string
            self.sequence = auxString != nil ? Int(auxString!) : nil
            self.id = messageJSON[Keys.ID]?.string
            self.text = messageJSON[Keys.Text]?.string
            if let attachmentsArrayDict = messageJSON[Keys.AttachmentsArray]?.array {
                attachments = [Attachment]()
                for attachmentJSON in attachmentsArrayDict {
                    attachments!.append(Attachment(attachmentJSON: attachmentJSON))
                }
            }
            auxString = messageJSON[Keys.TimeStamp]?.string
            self.timeStamp = auxString != nil ? Int(auxString!) : nil
        }
        if let senderDict = messagingJSON[Keys.Sender] {
            self.sender = senderDict[Keys.SenderID]?.string
        }
    }
    
    private func getDescription() -> String {
        var descriptionMessages = [String]()
        if id != nil {
            descriptionMessages.append("MessageID: \(id!)")
        }
        if text != nil {
            descriptionMessages.append("Text: \(text!)")
        }
        if sender != nil {
            descriptionMessages.append("SenderID: \(sender!)")
        }
        if timeStamp != nil {
            descriptionMessages.append("TimeStamp: \(timeStamp!)")
        }
        if sequence != nil {
            descriptionMessages.append("Sequence: \(sequence!)")
        }
        if attachments != nil {
            for attachment in attachments! {
                if attachment.description != "nil" {
                    descriptionMessages.append("Attachment: [\(attachment.description)]")
                }
            }
        }
        var descriptionString = ""
        for (counter, value) in descriptionMessages.enumerated() {
            descriptionString += value
            if counter < descriptionMessages.count - 1 {
                descriptionString += ", "
            }
        }
        
        return descriptionString != "" ? descriptionString : "nil"
    }
}

struct Attachment {
    var type: AttachmentType?
    private var attachmentValue: String?
    var attachmentURL: URL? {
        get {
            return getURL()
        }
    }
    var location: CLLocationCoordinate2D?
    var description: String {
        return getDescription()
    }
                
    
    struct Keys {
        static let TypeName = "type"
        static let Payload = "payload"
        static let URL = "url"
        static let CoordinateLat = "coordinates.lat"
        static let CoordinateLong = "coordinates.long"
    }
    
    private func getURL() -> URL? {
        if self.type != .fallback, self.type != .location, let urlString = self.attachmentValue {
            return URL(string: urlString)
        }
        
        return nil
    }
    
    init(attachmentJSON: JSON) {
        let auxValue = attachmentJSON[Keys.TypeName]?.string ?? ""
        self.type = AttachmentType(rawValue: auxValue) ?? .none
        if let payloadJSON = attachmentJSON[Keys.Payload] {
            switch self.type! {
            case .audio, .file, .image, .video:
                    self.attachmentValue = payloadJSON[Keys.URL]?.string
                break
            case .location:
                var auxString = payloadJSON[Keys.CoordinateLat]?.string ?? "0.0"
                let latitude = Double(auxString) ?? 0.0
                auxString = payloadJSON[Keys.CoordinateLong]?.string ?? "0.0"
                let longitude = Double(auxString) ?? 0.0
                self.location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                self.attachmentValue = "Latitude: \(latitude), Longitude: \(longitude)"<commit_msg>Removed CoreLocation framework as it doesn't exist on Linux<commit_after>//
//  Message.swift
//  FBChatbot
//
//  Created by <NAME> on 30/05/17.
//
//

import Foundation
import JSON

class Message {
    var id: String?
    var text: String?
    var sender: String?
    var timeStamp: Int?
    var sequence: Int?
    var attachments: [Attachment]?
    var description: String {
        return getDescription()
    }
    
    struct Keys {
        static let ID = "mid"
        static let MessagingJSON = "messaging"
        static let MessageJSON = "message"
        static let Sequence = "seq"
        static let Text = "text"
        static let AttachmentsArray = "attachments"
        static let Sender = "sender"
        static let SenderID = "id"
        static let TimeStamp = "timestamp"
    }
    
    init() {}
    
    init(messagingJSON: JSON) {
        var auxString: String?
        if let messageJSON = messagingJSON[Keys.MessageJSON] {
            auxString = messageJSON[Keys.Sequence]?.string
            self.sequence = auxString != nil ? Int(auxString!) : nil
            self.id = messageJSON[Keys.ID]?.string
            self.text = messageJSON[Keys.Text]?.string
            if let attachmentsArrayDict = messageJSON[Keys.AttachmentsArray]?.array {
                attachments = [Attachment]()
                for attachmentJSON in attachmentsArrayDict {
                    attachments!.append(Attachment(attachmentJSON: attachmentJSON))
                }
            }
            auxString = messageJSON[Keys.TimeStamp]?.string
            self.timeStamp = auxString != nil ? Int(auxString!) : nil
        }
        if let senderDict = messagingJSON[Keys.Sender] {
            self.sender = senderDict[Keys.SenderID]?.string
        }
    }
    
    private func getDescription() -> String {
        var descriptionMessages = [String]()
        if id != nil {
            descriptionMessages.append("MessageID: \(id!)")
        }
        if text != nil {
            descriptionMessages.append("Text: \(text!)")
        }
        if sender != nil {
            descriptionMessages.append("SenderID: \(sender!)")
        }
        if timeStamp != nil {
            descriptionMessages.append("TimeStamp: \(timeStamp!)")
        }
        if sequence != nil {
            descriptionMessages.append("Sequence: \(sequence!)")
        }
        if attachments != nil {
            for attachment in attachments! {
                if attachment.description != "nil" {
                    descriptionMessages.append("Attachment: [\(attachment.description)]")
                }
            }
        }
        var descriptionString = ""
        for (counter, value) in descriptionMessages.enumerated() {
            descriptionString += value
            if counter < descriptionMessages.count - 1 {
                descriptionString += ", "
            }
        }
        
        return descriptionString != "" ? descriptionString : "nil"
    }
}

struct Attachment {
    var type: AttachmentType?
    private var attachmentValue: String?
    var attachmentURL: URL? {
        get {
            return getURL()
        }
    }
    var location: (latitude: Double, longitude: Double)?
    var description: String {
        return getDescription()
    }
                
    
    struct Keys {
        static let TypeName = "type"
        static let Payload = "payload"
        static let URL = "url"
        static let CoordinateLat = "coordinates.lat"
        static let CoordinateLong = "coordinates.long"
    }
    
    private func getURL() -> URL? {
        if self.type != .fallback, self.type != .location, let urlString = self.attachmentValue {
            return URL(string: urlString)
        }
        
        return nil
    }
    
    init(attachmentJSON: JSON) {
        let auxValue = attachmentJSON[Keys.TypeName]?.string ?? ""
        self.type = AttachmentType(rawValue: auxValue) ?? .none
        if let payloadJSON = attachmentJSON[Keys.Payload] {
            switch self.type! {
            case .audio, .file, .image, .video:
                    self.attachmentValue = payloadJSON[Keys.URL]?.string
                break
            case .location:
                var auxString = payloadJSON[Keys.CoordinateLat]?.string ?? "0.0"
                let latitude = Double(auxString) ?? 0.0
                auxString = payloadJSON[Keys.CoordinateLong]?.string ?? "0.0"
                let longitude = Double(auxString) ?? 0.0
                self.location = (latitude: latitude, longitude: longitude)
                self.attachmentValue = "Latitude: \(latitude), Longitude: \(longitude)"
{'fraction_non_alphanumeric': 0.1026974325641859, 'fraction_numerical': 0.0034665800021666123, 'mean_word_length': 1.8266993263931415, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '922333', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2368, 'n_words': 602}