public static Collection <Term> MatchingTerms(string text, ClientContext ctx, Guid TermSetId, Guid AnchorId)
        {
            LogHelper.Log("Inside Matching Term " + TermSetId);
            LogHelper.Log("Inside Matching Term text len : " + text.Length);
            List <Guid>       FoundIds      = new List <Guid>();
            Collection <Term> ReturnedTerms = new Collection <Term>();

            text = RemoveDiacritics(text).ToUpperInvariant();

            TaxonomyTerms TT = new TaxonomyTerms(ctx, TermSetId.ToString(), AnchorId);
            List <Guid>   MatchingTermList = new List <Guid>();

            foreach (KeyValuePair <TermKey, string> kv in TT.TermsDictionary)
            {
                if (kv.Key.term.CustomProperties.ContainsKey(Constants.RegexBasedTagging))
                {
                    if (Regex.IsMatch(text, kv.Key.term.CustomProperties[Constants.RegexBasedTagging]) &&
                        !FoundIds.Contains(kv.Key.TermId))
                    {
                        FoundIds.Add(kv.Key.TermId);
                        ReturnedTerms.Add(kv.Key.term);
                    }
                }
                else if (Regex.IsMatch(text, string.Format("\\b{0}\\b", RemoveDiacritics(kv.Value).ToUpperInvariant()))
                         &&
                         !FoundIds.Contains(kv.Key.TermId))
                {
                    FoundIds.Add(kv.Key.TermId);
                    ReturnedTerms.Add(kv.Key.term);
                }
            }
            return(ReturnedTerms);
        }
        public static Collection<Term> MatchingTerms(string text,ClientContext ctx,Guid TermSetId,Guid AnchorId)
        {
            LogHelper.Log("Inside Matching Term " + TermSetId);
            LogHelper.Log("Inside Matching Term text len : " + text.Length);
            List<Guid> FoundIds = new List<Guid>();
            Collection<Term> ReturnedTerms = new Collection<Term>();
            text = RemoveDiacritics(text).ToUpperInvariant();

            TaxonomyTerms TT = new TaxonomyTerms(ctx, TermSetId.ToString(), AnchorId);
            List<Guid> MatchingTermList = new List<Guid>();
            foreach (KeyValuePair<TermKey, string> kv in TT.TermsDictionary)
            {
                if (kv.Key.term.CustomProperties.ContainsKey(Constants.RegexBasedTagging))
                {
                    if (Regex.IsMatch(text, kv.Key.term.CustomProperties[Constants.RegexBasedTagging]) &&
                    !FoundIds.Contains(kv.Key.TermId))
                    {
                        FoundIds.Add(kv.Key.TermId);
                        ReturnedTerms.Add(kv.Key.term);
                    }
                }
                else if (Regex.IsMatch(text, string.Format("\\b{0}\\b", RemoveDiacritics(kv.Value).ToUpperInvariant()))
                    &&                
                    !FoundIds.Contains(kv.Key.TermId))
                {
                    FoundIds.Add(kv.Key.TermId);
                    ReturnedTerms.Add(kv.Key.term);
                }
            }
            return ReturnedTerms;
        }
        /// <summary>
        /// Helper Method to set a Taxonomy Field on a list item
        /// </summary>
        /// <param name="ctx">The Authenticated ClientContext</param>
        /// <param name="listItem">The listitem to modify</param>
        /// <param name="model">Domain Object of key/value pairs of the taxonomy field & value</param>
        public static void SetTaxonomyFields(ClientContext ctx, ListItem listItem, string FileContent, string ListId, string url)
        {
            FieldCollection _fields = listItem.ParentList.Fields;

            ctx.Load(ctx.Web.AllProperties);
            ctx.Load(_fields);
            ctx.ExecuteQuery();

            AppWebHelper         hlp      = new AppWebHelper(url, false);
            List <GlobalSetting> settings = GetGlobalConfig(hlp);

            LogHelper.Log(settings.Count.ToString());
            var enabled = settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault();

            bool KeywordCreationEnabled = Convert.ToBoolean(
                settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault().value);
            int KeywordRecognitionTreshold = Convert.ToInt32(
                settings.Where(s => s.key == Constants.KeywordRecognitionTreshold).SingleOrDefault().value);
            int KeywordCreationTreshold = Convert.ToInt32(
                settings.Where(s => s.key == Constants.KeywordCreationTreshold).SingleOrDefault().value);

            List <string> ConfiguredFields = hlp.ListTaxFields(ListId);

            foreach (var _f in _fields)
            {
                if (ConfiguredFields.Contains(_f.Id.ToString()))
                {
                    TaxonomyField _field = ctx.CastTo <TaxonomyField>(_fields.GetById(_f.Id));
                    if (_f.InternalName != Constants.TaxFieldInternalName)
                    {
                        ctx.Load(_field);
                        ctx.ExecuteQuery();
                        Collection <Term> MatchingTerms = null;
                        MatchingTerms = AutoTaggingHelper.MatchingTerms(FileContent, ctx, _field.TermSetId, _field.AnchorId);

                        if (MatchingTerms.Count > 0)
                        {
                            LogHelper.Log("Updating taxfield " + _field.Title);
                            if (_field.AllowMultipleValues)
                            {
                                _field.SetFieldValueByCollection(listItem, MatchingTerms, 1033);
                            }
                            else
                            {
                                _field.SetFieldValueByTerm(listItem, MatchingTerms.First(), 1033);
                            }

                            listItem.Update();
                            ctx.ExecuteQuery();
                        }
                    }
                    else
                    {
                        TaxonomyTerms tt           = new TaxonomyTerms(ctx);
                        string        TextLanguage =
                            AutoTaggingHelper.LanguageIdentifier.Identify(FileContent).FirstOrDefault().Item1.Iso639_3;
                        StringBuilder            EntKeyWordsValue = new StringBuilder();
                        Dictionary <string, int> tokens           =
                            Tokenize(FileContent,
                                     KeywordRecognitionTreshold,
                                     TextLanguage);
                        StringBuilder TokenString = new StringBuilder();
                        foreach (KeyValuePair <string, int> token in tokens)
                        {
                            Guid KeywordId = TaxonomyTerms.GetKeyword(token.Key);
                            TokenString.AppendFormat("{0}|", token.Key);
                            if (KeywordId != Guid.Empty)
                            {
                                EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, KeywordId);
                            }
                            else
                            {
                                if (KeywordCreationEnabled && token.Value >= KeywordCreationTreshold &&
                                    !AutoTaggingHelper.IsEmptyWord(token.Key.ToLowerInvariant(), TextLanguage, hlp))
                                {
                                    Guid g = AddKeyWord(token.Key, ctx);
                                    if (g != Guid.Empty)
                                    {
                                        EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, g);
                                    }
                                }
                            }
                        }
                        LogHelper.Log(TokenString.ToString());
                        if (EntKeyWordsValue.ToString().Length > 0)
                        {
                            LogHelper.Log("keyword value " + EntKeyWordsValue.ToString(), LogSeverity.Error);

                            TaxonomyFieldValueCollection col = new TaxonomyFieldValueCollection(ctx, string.Empty, _field);
                            col.PopulateFromLabelGuidPairs(EntKeyWordsValue.ToString());
                            _field.SetFieldValueByValueCollection(listItem, col);
                            listItem.Update();
                            ctx.ExecuteQuery();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Helper Method to set a Taxonomy Field on a list item
        /// </summary>
        /// <param name="ctx">The Authenticated ClientContext</param>
        /// <param name="listItem">The listitem to modify</param>
        /// <param name="model">Domain Object of key/value pairs of the taxonomy field & value</param>
        public static void SetTaxonomyFields(ClientContext ctx, ListItem listItem,string FileContent,string ListId,string url)
        {                           
            FieldCollection _fields = listItem.ParentList.Fields;
            ctx.Load(ctx.Web.AllProperties);
            ctx.Load(_fields);
            ctx.ExecuteQuery();

            AppWebHelper hlp = new AppWebHelper(url,false);
            List<GlobalSetting> settings = GetGlobalConfig(hlp);
            LogHelper.Log(settings.Count.ToString());
            var enabled = settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault();
            
            bool KeywordCreationEnabled = Convert.ToBoolean(
                settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault().value);
            int KeywordRecognitionTreshold = Convert.ToInt32(
                                settings.Where(s => s.key == Constants.KeywordRecognitionTreshold).SingleOrDefault().value);
            int KeywordCreationTreshold = Convert.ToInt32(
                                settings.Where(s => s.key == Constants.KeywordCreationTreshold).SingleOrDefault().value);

            List<string> ConfiguredFields = hlp.ListTaxFields(ListId);
            foreach(var _f in _fields)
            {                
                if(ConfiguredFields.Contains(_f.Id.ToString()))
                {
                    TaxonomyField _field = ctx.CastTo<TaxonomyField>(_fields.GetById(_f.Id));
                    if(_f.InternalName != Constants.TaxFieldInternalName)
                    {
                        
                        ctx.Load(_field);
                        ctx.ExecuteQuery();
                        Collection<Term> MatchingTerms = null;
                        MatchingTerms = AutoTaggingHelper.MatchingTerms(FileContent, ctx, _field.TermSetId, _field.AnchorId);

                        if (MatchingTerms.Count > 0)
                        {
                            LogHelper.Log("Updating taxfield " + _field.Title);
                            if (_field.AllowMultipleValues)
                            {
                                _field.SetFieldValueByCollection(listItem, MatchingTerms, 1033);
                            }
                            else
                            {
                                _field.SetFieldValueByTerm(listItem, MatchingTerms.First(), 1033);
                            }

                            listItem.Update();
                            ctx.ExecuteQuery();
                        }    
                    }
                    else
                    {
                        TaxonomyTerms tt = new TaxonomyTerms(ctx);
                        string TextLanguage=
                            AutoTaggingHelper.LanguageIdentifier.Identify(FileContent).FirstOrDefault().Item1.Iso639_3;
                        StringBuilder EntKeyWordsValue = new StringBuilder();
                        Dictionary<string, int> tokens = 
                            Tokenize(FileContent,
                            KeywordRecognitionTreshold,
                            TextLanguage);
                        StringBuilder TokenString = new StringBuilder();
                        foreach (KeyValuePair<string, int> token in tokens)
                        {
                            Guid KeywordId = TaxonomyTerms.GetKeyword(token.Key);
                            TokenString.AppendFormat("{0}|", token.Key);
                            if (KeywordId != Guid.Empty)
                            {
                                EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, KeywordId);
                            }
                            else
                            {
                                
                                if (KeywordCreationEnabled && token.Value >= KeywordCreationTreshold &&
                                    !AutoTaggingHelper.IsEmptyWord(token.Key.ToLowerInvariant(), TextLanguage,hlp))
                                {
                                    
                                    Guid g = AddKeyWord(token.Key, ctx);
                                    if (g != Guid.Empty)
                                    {
                                        EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, g);
                                    }

                                }
                            }
                        }
                        LogHelper.Log(TokenString.ToString());
                        if (EntKeyWordsValue.ToString().Length > 0)
                        {
                            LogHelper.Log("keyword value " + EntKeyWordsValue.ToString(), LogSeverity.Error);

                            TaxonomyFieldValueCollection col = new TaxonomyFieldValueCollection(ctx, string.Empty, _field);
                            col.PopulateFromLabelGuidPairs(EntKeyWordsValue.ToString());
                            _field.SetFieldValueByValueCollection(listItem, col);
                            listItem.Update();
                            ctx.ExecuteQuery();
                        }                       
                    }
                        
                }
            }

        }