Skip to content

Instantly share code, notes, and snippets.

@GeorgeHahn
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save GeorgeHahn/d4d2ac3628120dea5f1c to your computer and use it in GitHub Desktop.

Select an option

Save GeorgeHahn/d4d2ac3628120dea5f1c to your computer and use it in GitHub Desktop.
HAM Radio Test Question Anki Importer
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace FCC2Anki
{
class Program
{
static void Main(string[] args)
{
var file = "2014-2018 Tech Pool.txt";
var contents = File.ReadAllText(file);
contents = contents.Replace("�", "'");
var contentslines = new List<string>(contents.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
string subelement = null;
char section = 'A';
for (var i = 0; i < contentslines.Count; i++)
{
contentslines[i] = contentslines[i].Trim();
if (contentslines[i].IndexOf("SUBELEMENT") >= 0)
{
subelement = contentslines[i].Substring(contentslines[i].IndexOf("SUBELEMENT") + 11, 2);
section = 'A';
contentslines.RemoveAt(i);
i--;
}
else if ((subelement != null) && contentslines[i].IndexOf(subelement + section + " ") == 0)
{
contentslines.RemoveAt(i);
section++;
}
}
contents = string.Join(Environment.NewLine, contentslines);
var questions = contents.Split(new []{ "~~" }, StringSplitOptions.RemoveEmptyEntries);
var output = "<?xml version=\"1.0\" standalone=\"yes\"?>" + Environment.NewLine +
"<?xml-stylesheet type=\"text/xsl\" href=\"http://supermemo.com/archive/beta/tmp/xml/supermemo.xsl\"?>" + Environment.NewLine +
"<SuperMemoCollection>" + Environment.NewLine +
"<Count>" + questions.Length + "</Count>" + Environment.NewLine;
var count = 0;
foreach (var question in questions)
{
var lines = question.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var id = lines[0].Substring(0, lines[0].IndexOf(" "));
var correctletter = lines[0].Substring(lines[0].IndexOf('(') + 1, 1);
var reference = lines[0].Substring(lines[0].IndexOf(')') + 1);
var questiontext = lines[1];
var answers = new string[lines.Length - 2];
Array.Copy(lines, 2, answers, 0, lines.Length - 2);
var correctanswer = answers["ABCD".IndexOf(correctletter)];
output += "<SuperMemoElement>" + Environment.NewLine +
"<ID>" + count + "</ID>" + Environment.NewLine +
"<Content>" + Environment.NewLine +
"<Question>" + Environment.NewLine +
id + " " + reference + Environment.NewLine +
questiontext + Environment.NewLine +
string.Join(Environment.NewLine, answers) +
"</Question>" + Environment.NewLine +
"<Answer>" + Environment.NewLine +
correctanswer + Environment.NewLine +
"</Answer>" + Environment.NewLine +
"</Content>" + Environment.NewLine +
"</SuperMemoElement>" + Environment.NewLine;
count++;
}
output += "</SuperMemoCollection>" + Environment.NewLine;
File.WriteAllText(Path.GetFileName(file) + ".xml", output);
}
}
}
@GeorgeHahn
Copy link
Copy Markdown
Author

Dirty, but sufficient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment