aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/TextEncoding/NLangDetect/GenProfile.cs
blob: 26157483b6176747fd67db8a2e2a65da1be95a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.IO;
using System.IO.Compression;
using System.Xml;
using NLangDetect.Core.Utils;

namespace NLangDetect.Core
{
    // TODO IMM HI: xml reader not tested
    public static class GenProfile
    {
        #region Public methods

        public static LangProfile load(string lang, string file)
        {
            var profile = new LangProfile(lang);
            var tagextractor = new TagExtractor("abstract", 100);
            Stream inputStream = null;

            try
            {
                inputStream = File.OpenRead(file);

                string extension = Path.GetExtension(file) ?? "";

                if (extension.ToUpper() == ".GZ")
                {
                    inputStream = new GZipStream(inputStream, CompressionMode.Decompress);
                }

                using (var xmlReader = XmlReader.Create(inputStream))
                {
                    while (xmlReader.Read())
                    {
                        switch (xmlReader.NodeType)
                        {
                            case XmlNodeType.Element:
                                tagextractor.SetTag(xmlReader.Name);
                                break;

                            case XmlNodeType.Text:
                                tagextractor.Add(xmlReader.Value);
                                break;

                            case XmlNodeType.EndElement:
                                tagextractor.CloseTag(profile);
                                break;
                        }
                    }
                }
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.Close();
                }
            }

            Console.WriteLine(lang + ": " + tagextractor.Count);

            return profile;
        }

        #endregion
    }
}