2019-06-05 14:45:45 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2020-02-10 02:10:18 +00:00
|
|
|
using Marechai.Database.Models;
|
2019-06-05 14:45:45 +01:00
|
|
|
|
2020-02-10 02:20:48 +00:00
|
|
|
namespace Marechai.Helpers
|
2019-06-05 14:45:45 +01:00
|
|
|
{
|
|
|
|
|
public static class Iso639
|
|
|
|
|
{
|
|
|
|
|
// TODO: Changes file
|
2019-06-05 14:50:09 +01:00
|
|
|
// Data files can be found in https://iso639-3.sil.org/code_tables/download_tables
|
2020-02-10 02:47:39 +00:00
|
|
|
internal static void Import(MarechaiContext context)
|
2019-06-05 14:45:45 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!Directory.Exists("iso639"))
|
|
|
|
|
return;
|
2019-06-05 14:45:45 +01:00
|
|
|
|
|
|
|
|
IEnumerable<string> files = Directory.EnumerateFiles("iso639", "iso-639-3_*.tab");
|
|
|
|
|
long added = 0;
|
|
|
|
|
long modified = 0;
|
|
|
|
|
|
|
|
|
|
foreach(string file in files)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Importing ISO-639 codes from {0}", file);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
|
|
|
using(var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))
|
2019-06-05 14:45:45 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
using(var sr = new StreamReader(fs, Encoding.UTF8))
|
2019-06-05 14:45:45 +01:00
|
|
|
{
|
|
|
|
|
string line;
|
|
|
|
|
string[] pieces;
|
|
|
|
|
|
|
|
|
|
line = sr.ReadLine();
|
|
|
|
|
|
|
|
|
|
if(line is null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Invalid data, not proceeding");
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-06-05 14:45:45 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pieces = line.Split('\t');
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(pieces.Length != 8 ||
|
|
|
|
|
pieces[0] != "Id" ||
|
|
|
|
|
pieces[1] != "Part2B" ||
|
|
|
|
|
pieces[2] != "Part2T" ||
|
|
|
|
|
pieces[3] != "Part1" ||
|
|
|
|
|
pieces[4] != "Scope" ||
|
|
|
|
|
pieces[5] != "Language_Type" ||
|
|
|
|
|
pieces[6] != "Ref_Name" ||
|
|
|
|
|
pieces[7] != "Comment")
|
2019-06-05 14:45:45 +01:00
|
|
|
{
|
|
|
|
|
Console.WriteLine("Invalid data, not proceeding");
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-06-05 14:45:45 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line = sr.ReadLine();
|
|
|
|
|
|
|
|
|
|
while(line != null)
|
|
|
|
|
{
|
|
|
|
|
pieces = line.Split('\t');
|
|
|
|
|
|
|
|
|
|
if(pieces.Length != 8)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Invalid data, continuing with next line");
|
|
|
|
|
line = sr.ReadLine();
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-06-05 14:45:45 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int p = 1; p < 8; p++)
|
|
|
|
|
if(pieces[p] == "")
|
|
|
|
|
pieces[p] = null;
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
Database.Models.Iso639 lang = context.Iso639.FirstOrDefault(i => i.Id == pieces[0]);
|
2019-06-05 14:45:45 +01:00
|
|
|
|
|
|
|
|
if(lang is null)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
context.Iso639.Add(new Database.Models.Iso639
|
2019-06-05 14:45:45 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
Id = pieces[0], Part2B = pieces[1], Part2T = pieces[2],
|
|
|
|
|
Part1 = pieces[3],
|
|
|
|
|
Scope = pieces[4], Type = pieces[5], ReferenceName = pieces[6],
|
|
|
|
|
Comment = pieces[7]
|
2019-06-05 14:45:45 +01:00
|
|
|
});
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-06-05 14:45:45 +01:00
|
|
|
line = sr.ReadLine();
|
|
|
|
|
added++;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-06-05 14:45:45 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(pieces[1] != lang.Part2B ||
|
|
|
|
|
pieces[2] != lang.Part2T ||
|
2019-06-05 14:45:45 +01:00
|
|
|
pieces[3] != lang.Part1 ||
|
2020-02-10 22:44:18 +00:00
|
|
|
pieces[4] != lang.Scope ||
|
|
|
|
|
pieces[5] != lang.Type ||
|
2019-06-05 14:45:45 +01:00
|
|
|
pieces[6] != lang.ReferenceName ||
|
|
|
|
|
pieces[7] != lang.Comment)
|
|
|
|
|
{
|
|
|
|
|
lang.Part2B = pieces[1];
|
|
|
|
|
lang.Part2T = pieces[2];
|
|
|
|
|
lang.Part1 = pieces[3];
|
|
|
|
|
lang.Scope = pieces[4];
|
|
|
|
|
lang.Type = pieces[5];
|
|
|
|
|
lang.ReferenceName = pieces[6];
|
|
|
|
|
lang.Comment = pieces[7];
|
|
|
|
|
context.Iso639.Update(lang);
|
|
|
|
|
modified++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line = sr.ReadLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
Console.WriteLine("{0} language codes added", added);
|
2019-06-05 14:45:45 +01:00
|
|
|
Console.WriteLine("{0} language codes modified", modified);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|