samedi 4 novembre 2017

Anonimization and randomization of user vs file in Swift

I am working on an educational script in swift. The aim is to facilitate collaboration between peers in a university level class on statistics. Each student is required to make suggestions (anonymously) to one of their colleagues' work. The script I have been writing works so that all markdown files in the directory are anonymized (ie, author and email yaml keys are removed) and new files with the contents are created and renamed with an email address.

So far I was able to remove author: and email: yaml keys from each file and I was also able to rename the files in the directory. However, I am struggling to create a way of randomizing files and emails. The point is that the original file should be passed on to another student, but without the last one knowing the author. The receiving student should be decided randomly. This randomization is what I am struggling with.

The MWE

It is a linux project and the easiest way I found to create a MWE was to write the script using the excellent John Sundell's marathon script structure. Below is the code:

import Yaml // marathon: http://ift.tt/1QfqVAQ
import Files // marathon:http://ift.tt/2u6Nrus
import Foundation

var email: String = ""
var document: String = ""


for file in Folder.current.files {
    guard file.extension == "md" else {
        continue
    }

    let content = try file.readAsString()
    let pattern = "(?s)(?<=---).*(?=---)"
    if let range = content.range(of: pattern, options: .regularExpression) {
        let text = String(content[range])
        let value = try! Yaml.load(text)
        email = value["email"].string!
        let author = value["author"].string!
        let emailline = "email: " + email
        let authorline = "author: " + author
        document = content.replacingOccurrences(of: "\n\(emailline)", with: "")
        document = content.replacingOccurrences(of: "\n\(authorline)", with: "")
    }  

    // Creating new file with name from email and copying contents into it
    //let folder = try Folder()
    let file = try Folder.current.createFile(named: email)
    try file.write(string: document)
}

An example md file:

---
# Metadata
title: hum
author: jecatatu
email: email@gmail.com
---
This is more text outside the yaml block

email: zwe@gmail.com

A second file:

# Metadata
title: My essay
author: brauser
email: brauser@gmail.com
---
Extra text

A third file:

# Metadata
title: My third essay
author: bestuser
email: bestuser@gmail.com
---
Extra text

Question

To start off, I don't need code. But code is welcome. This question seems to be related more to the design than to the code itself. Note that one can run the above example (provided you have marathon installed) with:

marathon run lf-araujo/collab

  • How do I create a way of randomly attribute a file to the email present in one of the other files? My idea was to create a new file with the same contents, but the identificatio (email and author names). How do I scramble files with email from within these files using swift?



Aucun commentaire:

Enregistrer un commentaire