DetectorFactory.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Model.Serialization;
  5. using NLangDetect.Core.Utils;
  6. namespace NLangDetect.Core
  7. {
  8. public class DetectorFactory
  9. {
  10. public Dictionary<string, ProbVector> WordLangProbMap;
  11. public List<string> Langlist;
  12. private static readonly DetectorFactory _instance = new DetectorFactory();
  13. #region Constructor(s)
  14. private DetectorFactory()
  15. {
  16. WordLangProbMap = new Dictionary<string, ProbVector>();
  17. Langlist = new List<string>();
  18. }
  19. #endregion
  20. #region Public methods
  21. public static void LoadProfiles(IJsonSerializer json)
  22. {
  23. var assembly = typeof(DetectorFactory).Assembly;
  24. var names = assembly.GetManifestResourceNames()
  25. .Where(i => i.IndexOf("NLangDetect.Profiles", StringComparison.Ordinal) != -1)
  26. .ToList();
  27. var index = 0;
  28. foreach (var name in names)
  29. {
  30. using (var stream = assembly.GetManifestResourceStream(name))
  31. {
  32. var langProfile = (LangProfile)json.DeserializeFromStream(stream, typeof(LangProfile));
  33. AddProfile(langProfile, index);
  34. }
  35. index++;
  36. }
  37. }
  38. public static Detector Create()
  39. {
  40. return CreateDetector();
  41. }
  42. public static Detector Create(double alpha)
  43. {
  44. var detector = CreateDetector();
  45. detector.SetAlpha(alpha);
  46. return detector;
  47. }
  48. public static void SetSeed(int? seed)
  49. {
  50. _instance.Seed = seed;
  51. }
  52. #endregion
  53. #region Internal methods
  54. internal static void AddProfile(LangProfile profile, int index)
  55. {
  56. var lang = profile.name;
  57. if (_instance.Langlist.Contains(lang))
  58. {
  59. throw new NLangDetectException("duplicate the same language profile", ErrorCode.DuplicateLangError);
  60. }
  61. _instance.Langlist.Add(lang);
  62. foreach (string word in profile.freq.Keys)
  63. {
  64. if (!_instance.WordLangProbMap.ContainsKey(word))
  65. {
  66. _instance.WordLangProbMap.Add(word, new ProbVector());
  67. }
  68. double prob = (double)profile.freq[word] / profile.n_words[word.Length - 1];
  69. _instance.WordLangProbMap[word][index] = prob;
  70. }
  71. }
  72. internal static void Clear()
  73. {
  74. _instance.Langlist.Clear();
  75. _instance.WordLangProbMap.Clear();
  76. }
  77. #endregion
  78. #region Private helper methods
  79. private static Detector CreateDetector()
  80. {
  81. if (_instance.Langlist.Count == 0)
  82. {
  83. throw new NLangDetectException("need to load profiles", ErrorCode.NeedLoadProfileError);
  84. }
  85. return new Detector(_instance);
  86. }
  87. #endregion
  88. #region Properties
  89. public int? Seed { get; private set; }
  90. #endregion
  91. }
  92. }