Example #1
0
 protected override void AfterWrite(RecordWriter writer)
 {
     if (Data == null)
     {
         Data = new KeywordData();
     }
 }
 private void OnExtractKeywords(KeywordData keywordData, string data)
 {
     if (keywordData != null)
     {
         Log.Debug("ExampleAlchemyLanguage", "status: {0}", keywordData.status);
         Log.Debug("ExampleAlchemyLanguage", "url: {0}", keywordData.url);
         Log.Debug("ExampleAlchemyLanguage", "language: {0}", keywordData.language);
         Log.Debug("ExampleAlchemyLanguage", "text: {0}", keywordData.text);
         if (keywordData == null || keywordData.keywords.Length == 0)
         {
             Log.Debug("ExampleAlchemyLanguage", "No keywords found!");
         }
         else
         {
             foreach (Keyword keyword in keywordData.keywords)
             {
                 Log.Debug("ExampleAlchemyLanguage", "text: {0}, relevance: {1}", keyword.text, keyword.relevance);
             }
         }
     }
     else
     {
         Log.Debug("ExampleAlchemyLanguage", "Failed to find Keywords!");
     }
 }
Example #3
0
 private List <string> GetSuggestions(KeywordData keyword, UserKeywordTag ukt)
 {
     return(new List <string>()
     {
         "a", "b", "c", "d"
     });
 }
        public static XElement GetChildern(this KeywordData keyword, ListBaseColumns list = ListBaseColumns.Default)
        {
            ChildKeywordsFilterData keywordsDataFilter = new ChildKeywordsFilterData
            {
                BaseColumns = list,
            };

            return(Wrapper.Instance.GetListXml(keyword.Id, keywordsDataFilter));
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Keyword"/> class.
        /// </summary>
        /// <param name="client"><see cref="T:TcmCoreService.Client" /></param>
        /// <param name="keywordData"><see cref="T:Tridion.ContentManager.CoreService.Client.KeywordData" /></param>
        protected Keyword(Client client, KeywordData keywordData) : base(client, keywordData)
        {
            if (keywordData == null)
            {
                throw new ArgumentNullException("keywordData");
            }

            mKeywordData = keywordData;
        }
        public static XElement GetClassifiedItems(this KeywordData keyword, ItemType[] itemTypes, ListBaseColumns list = ListBaseColumns.Default)
        {
            ClassifiedItemsFilterData filter = new ClassifiedItemsFilterData
            {
                ItemTypes   = itemTypes,
                BaseColumns = list
            };

            return(Wrapper.Instance.GetListXml(keyword.Id, filter));
        }
Example #7
0
        /// <summary>
        /// Reload the <see cref="Keyword" /> with the specified <see cref="T:Tridion.ContentManager.CoreService.Client.KeywordData" />
        /// </summary>
        /// <param name="keywordData"><see cref="T:Tridion.ContentManager.CoreService.Client.KeywordData" /></param>
        protected void Reload(KeywordData keywordData)
        {
            if (keywordData == null)
            {
                throw new ArgumentNullException("keywordData");
            }

            mKeywordData = keywordData;
            base.Reload(keywordData);

            mParentKeywords  = null;
            mRelatedKeywords = null;
        }
Example #8
0
        public static KeywordResult From(KeywordData item, string currentUserId)
        {
            var result = new KeywordResult
            {
                Description = TextEntry.From(item.Description, Resources.LabelDescription),
                Key = TextEntry.From(item.Key, Resources.LabelKey)
            };

            if (item.IsAbstract == true)
            {
                result.IsAbstract = TextEntry.From(Resources.Yes, Resources.LabelIsAbstract);
            }

            AddCommonProperties(item, result);
            AddPropertiesForRepositoryLocalObject(item, result, currentUserId);
            return result;
        }
        public static KeywordResult From(KeywordData item, string currentUserId)
        {
            var result = new KeywordResult
            {
                Description = TextEntry.From(item.Description, Resources.LabelDescription),
                Key         = TextEntry.From(item.Key, Resources.LabelKey)
            };

            if (item.IsAbstract == true)
            {
                result.IsAbstract = TextEntry.From(Resources.Yes, Resources.LabelIsAbstract);
            }


            AddCommonProperties(item, result);
            AddPropertiesForRepositoryLocalObject(item, result, currentUserId);
            return(result);
        }
        /// <summary>
        /// Create Keyword using KeywordData class
        /// </summary>
        /// <param name="coreService"></param>
        /// <param name="value"></param>
        /// <param name="key"></param>
        /// <param name="CategoryID"></param>
        /// <returns></returns>
        public static string GenerateKeyword(ICoreServiceFrameworkContext coreService, string value, string key, string CategoryID)
        {
            try
            {
                KeywordData keyword = (KeywordData)coreService.Client.GetDefaultData(ItemType.Keyword, CategoryID, new ReadOptions());
                keyword.Id    = "tcm:0-0-0";
                keyword.Title = value;
                keyword.Key   = key;

                keyword = (KeywordData)coreService.Client.Create(keyword, new ReadOptions());

                return(keyword.Id.ToString());
            }
            catch (Exception ex)
            {
                return("");
            }
        }
Example #11
0
            public Keyword(KeywordData keywordData, string weakDeliminator, string additionalDeliminator)
            {
                string        defaultDeliminators = DefaultDeliminators;
                StringBuilder builder             = new StringBuilder();

                for (int i = 0; i < defaultDeliminators.Length; ++i)
                {
                    char c = defaultDeliminators[i];
                    if (weakDeliminator.IndexOf(c) == -1 && additionalDeliminator.IndexOf(c) == -1)
                    {
                        builder.Append(c);
                    }
                }
                builder.Append(additionalDeliminator);
                deliminators = builder.ToString();
                nodes        = keywordData.nodes;
                nodesCount   = keywordData.nodesCount;
            }
        public void CreateKeywords(List <string> keywords)
        {
            List <string> currentKeywords = GetExistingKeywords(_keywordDirty);

            foreach (var keyword in keywords)
            {
                if (currentKeywords.Contains(keyword))
                {
                    continue;
                }
                // Must create a new one
                _keywordDirty = true;
                KeywordData newKeyword = (KeywordData)_client.GetDefaultData(ItemType.Keyword,
                                                                             ResolveUrl(Constants.ContentCategoryUrl));
                newKeyword.Title = keyword;
                _client.Save(newKeyword, null);
            }
        }
Example #13
0
        /// <summary>
        /// Create Keywirds in a given category
        /// </summary>
        /// <param name="model">CategoryModel</param>
        /// <param name="CategoryId">tcm:xx-yy-zz</param>
        /// <returns></returns>
        public static string CreateKeywordsInCategory(CategoryModel model, string CategoryId)
        {
            var result = "true";

            cs_client = CoreServiceProvider.CreateCoreService();

            try
            {
                // open the category that is already created in Tridion
                CategoryData category = (CategoryData)cs_client.Read(CategoryId, null);

                var xmlCategoryKeywords = cs_client.GetListXml(CategoryId, new KeywordsFilterData());
                var keywordAny          = xmlCategoryKeywords.Elements()
                                          .Where(element => element.Attribute("Key").Value == model.Key)
                                          .Select(element => element.Attribute("ID").Value)
                                          .Select(id => (KeywordData)cs_client.Read(id, null)).FirstOrDefault();

                if (keywordAny == null)
                {
                    // create a new keyword
                    KeywordData keyword = (KeywordData)cs_client.GetDefaultData(Tridion.ContentManager.CoreService.Client.ItemType.Keyword, category.Id, new ReadOptions());
                    // set the id to 0 to notify Tridion that it is new
                    keyword.Id          = "tcm:0-0-0";
                    keyword.Title       = model.Title;
                    keyword.Key         = model.Key;
                    keyword.Description = model.Description;
                    keyword.IsAbstract  = false;

                    // create the keyword
                    cs_client.Create(keyword, null);
                    cs_client.Close();
                }
            }
            catch (Exception ex)
            {
                result = "Error: " + ex.Message;
            }
            finally
            {
                cs_client.Close();
            }

            return(result);
        }
Example #14
0
 public KeywordData(string[] words, bool casesensitive, KeywordData next)
 {
     this.casesensitive = casesensitive;
     this.next          = next;
     string[] sortedWords = new string[words.Length];
     if (casesensitive)
     {
         Array.Copy(words, sortedWords, words.Length);
     }
     else
     {
         for (int i = 0; i < words.Length; ++i)
         {
             sortedWords[i] = words[i].ToLowerInvariant();
         }
     }
     Array.Sort(sortedWords, System.StringComparer.Ordinal);
     NodesAdd(new KeywordNode((char)1));
     ParseNodes(sortedWords, 0, 0, sortedWords.Length);
 }
Example #15
0
        public static void AddKeyword(IAlchemyCoreServiceClient client, string tcmCategory, string keywordName)
        {
            List <ItemInfo> keywords = GetKeywordsByCategory(client, tcmCategory);

            if (keywords.All(x => x.Title != keywordName))
            {
                KeywordData keyword = new KeywordData
                {
                    Title        = keywordName,
                    Key          = keywordName,
                    Description  = keywordName,
                    LocationInfo = new LocationInfo {
                        OrganizationalItem = new LinkToOrganizationalItemData {
                            IdRef = tcmCategory
                        }
                    },
                    Id = "tcm:0-0-0"
                };

                keyword = (KeywordData)client.Create(keyword, new ReadOptions());
            }
        }
Example #16
0
        public static bool Reserve(string word)
        {
            if (word.Length < 3)
            {
                return(true);
            }
            HashSet <string> h = null;

            if (CacheService.Contains("RESERVE_KEYWORD"))
            {
                h = CacheService.Get(RESERVE_KEYWORD) as HashSet <string>;
            }
            else
            {
                h = KeywordData.GetReserve(); CacheService.Add(RESERVE_KEYWORD, h);
            }

            if (h != null)
            {
                return(h.Contains(word));
            }
            return(false);
        }
Example #17
0
        public string GetSplitKeywords(int id)
        {
            List <KeywordData> keywords = new List <KeywordData>();

            try
            {
                using SqlConnection conn = new SqlConnection(connString.SQLConnection);

                var sqlQuery =
                    "SELECT AnalysisId, Keyword, Count FROM SplitKeywords  " +
                    "WHERE AnalysisId = @AnalysisId " +
                    "ORDER BY Count DESC";
                conn.Open();

                using SqlCommand cmd = new SqlCommand(sqlQuery, conn);
                cmd.Parameters.Add("@AnalysisId", SqlDbType.Int).Value = id;

                var reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    var keyword = new KeywordData();
                    keyword.analysisId = (int)reader["AnalysisId"];
                    keyword.keyword    = reader["Keyword"].ToString();
                    keyword.count      = (int)reader["Count"];
                    keywords.Add(keyword);
                }

                reader.Close();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }

            return(JsonConvert.SerializeObject(keywords));
        }
 private void OnExtractKeywordsText(KeywordData keywordData, string data)
 {
     Log.Debug("ExampleAlchemyLanguage", "Alchemy Language - Extract keywords response text: {0}", data);
     Test(keywordData != null);
     _getKeywordExtractionTextTested = true;
 }
Example #19
0
 public void AddKeyword(Keyword keyword)
 {
     keywordList.Add(keyword);
     toolTipInfos.Add(KeywordData.getData(keyword).info);
 }
Example #20
0
 // convert keyword to tool tip
 public static ToolTipInfo getToolTipInfoFromKeyword(KeywordData k) => new KeywordInfo(k);
Example #21
0
 private void ParseNodes(string[] words, int position, int i0, int i1)
 {
     int index = nodesCount;
     {
         char prevC   = '\0';
         bool hasEnds = false;
         for (int i = i0; i < i1; ++i)
         {
             string word = words[i];
             if (position >= word.Length)
             {
                 if (position == word.Length)
                 {
                     hasEnds = true;
                 }
                 continue;
             }
             char c = word[position];
             if (c != prevC)
             {
                 prevC = c;
                 NodesAdd(new KeywordNode(c));
                 if (!casesensitive)
                 {
                     char upperC = char.ToUpperInvariant(c);
                     if (upperC != c)
                     {
                         NodesAdd(new KeywordNode(upperC));
                     }
                 }
             }
         }
         NodesAdd(new KeywordNode(hasEnds ? (char)1 : '\0'));
     }
     {
         bool first = true;
         char prevC = '\0';
         int  prevI = i0;
         for (int i = i0; i < i1; ++i)
         {
             string word = words[i];
             if (position >= word.Length)
             {
                 continue;
             }
             char c = word[position];
             if (first)
             {
                 prevC = c;
                 first = false;
             }
             else if (c != prevC)
             {
                 int next = nodesCount;
                 if (i - prevI == 1 && words[prevI].Length == position + 1)
                 {
                     next = 0;
                 }
                 nodes[index].next = next;
                 ++index;
                 if (!casesensitive)
                 {
                     char upperC = char.ToUpperInvariant(prevC);
                     if (upperC != prevC)
                     {
                         nodes[index].next = next;
                         ++index;
                     }
                 }
                 if (next != 0)
                 {
                     ParseNodes(words, position + 1, prevI, i);
                 }
                 prevC = c;
                 prevI = i;
             }
         }
         if (!first)
         {
             int next = nodesCount;
             if (i1 - prevI == 1 && words[prevI].Length == position + 1)
             {
                 next = 0;
             }
             nodes[index].next = next;
             if (!casesensitive)
             {
                 char currentC = nodes[index].c;
                 char upperC   = char.ToUpperInvariant(currentC);
                 if (upperC != currentC)
                 {
                     ++index;
                     nodes[index].next = next;
                 }
             }
             if (next != 0)
             {
                 ParseNodes(words, position + 1, prevI, i1);
             }
         }
     }
 }
Example #22
0
 protected override void AfterWrite(RecordWriter writer)
 {
     if (Data == null)
         Data = new KeywordData();
 }
Example #23
0
 public KeywordInfo(KeywordData k)
 {
     keyword = k;
 }
Example #24
0
 private static bool KeywordIsValid(KeywordData k)
 {
     return(k != null && !String.IsNullOrEmpty(k.KeywordName) && !String.IsNullOrEmpty(k.KeywordValue));
 }
Example #25
0
 public void RemoveKeyword(Keyword keyword)
 {
     keywordList.Remove(keyword);
     toolTipInfos.Remove(KeywordData.getData(keyword).info);
 }
 public static XElement GetClassifiedItems(this KeywordData keyword, ItemType itemType, ListBaseColumns list = ListBaseColumns.Default)
 {
     ItemType[] itemList = { itemType };
     return(GetClassifiedItems(keyword, itemList, list));
 }
    static async Task Main(string[] args)
    {
        #region 取得token

        using (HttpClient client = new HttpClient())                                                                                                                                      //建立 HttpClient
            using (HttpResponseMessage response = await client.GetAsync("https://api2.ifeel.com.tw/pro/auth?member_account=iiidsi_sa&client_secretkey=daa75a16f538c13be9e97cf91acdd9c8")) //使用 async 方法從網路 url 上取得回應
                using (HttpContent content = response.Content)                                                                                                                            //將網路取得回應的內容設定給 httpcontent
                {
                    string result = await content.ReadAsStringAsync();                                                                                                                    // 將 httpcontent 轉為 string

                    if (result != "Unauthorized Access")                                                                                                                                  //成功取得token
                    {
                        Console.WriteLine("Get token successfully" + "\n");
                        dynamic dy = JsonConvert.DeserializeObject <dynamic>(result); //將result轉換為dynamic物件,方便資料讀取
                        tokenType = dy.token_type;
                        token     = dy.access_token;
                        Console.WriteLine("token type: " + tokenType);
                        Console.WriteLine("token: " + token + "\n");
                    }
                    else //取得token失敗
                    {
                        Console.WriteLine("Get token unsuccessfully" + "\n");
                    }
                }
        #endregion

        #region 驗證token有效性

        using (HttpClient client = new HttpClient())
        {
            tokenForTest = tokenType + " " + token;                                                                    //串聯token type與token碼,以利之後header驗證使用
            client.DefaultRequestHeaders.Add("Authorization", tokenForTest);                                           // 指定 authorization header
            using (HttpResponseMessage response = client.PostAsync("https://api2.ifeel.com.tw/pro/auth", null).Result) //使用 async 方法從網路 url 上取得回應
                using (HttpContent content = response.Content)                                                         //將網路取得回應的內容設定給 httpcontent
                {
                    string result = await content.ReadAsStringAsync();                                                 // 將 httpcontent 轉為 string

                    if (result != "Unauthorized Access")                                                               //驗證成功
                    {
                        Console.WriteLine("Token accessible,Information:" + "\n");
                        dynamic dy = JsonConvert.DeserializeObject <dynamic>(result); //將result轉換為dynamic物件,方便資料讀取
                        Console.WriteLine(result);
                        success = true;
                    }
                    else //驗證失敗
                    {
                        Console.WriteLine("Token not accessible" + "\n");
                        success = false;
                    }
                }
        }
        #endregion

        #region 抓取新聞內容

        //token驗證成功,可抓取新聞資料
        if (success == true)
        {
            //抓取2018-04-20的資料
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", tokenForTest);                                  // 指定 authorization header
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //指定content type header
                var paraBefore = new Dictionary <string, string>()                                                //定義傳入參數
                {
                    {
                        "date", "2018-04-20"
                    },
                    {
                        "keyword", "麻疹"
                    },
                };
                var paraAfter = new StringContent(JsonConvert.SerializeObject(paraBefore), Encoding.UTF8, "application/json");               //轉換傳入參數的編碼與資料格式
                using (HttpResponseMessage response = client.PostAsync("https://api2.ifeel.com.tw/pro/document/news/all", paraAfter).Result) //使用 async 方法從網路 url 上取得回應
                    using (HttpContent content = response.Content)                                                                           //將網路取得回應的內容設定給 httpcontent
                    {
                        string result = await content.ReadAsStringAsync();                                                                   // 將 httpcontent 轉為 string

                        dynamic dy = JsonConvert.DeserializeObject <dynamic>(result);                                                        //將result轉換為dynamic物件,方便資料讀取

                        if (dy["_status"] == "Success")                                                                                      //抓取成功
                        {
                            Console.WriteLine("Get " + paraBefore["date"] + " news data successfully!" + "\n");

                            //依序將抓到的新聞內容存進News物件,並存進news list裡
                            for (int i = 0; i < dy["data"].Count; i++)
                            {
                                news.Add(new News(dy["data"][i]["content"].ToString()));
                                //Console.WriteLine(dy["data"][i]["content"].ToString() + "\n");
                            }
                        }
                        else //抓取失敗
                        {
                            Console.WriteLine("Get " + paraBefore["date"] + " news data unsuccessfully!" + "\n");
                        }
                    }

                //接著抓取2018-04-21的資料
                paraBefore = new Dictionary <string, string>()   //定義傳入參數
                {
                    {
                        "date", "2018-04-21"
                    },
                    {
                        "keyword", "麻疹"
                    },
                };
                paraAfter = new StringContent(JsonConvert.SerializeObject(paraBefore), Encoding.UTF8, "application/json");                   //轉換傳入參數的編碼與資料格式
                using (HttpResponseMessage response = client.PostAsync("https://api2.ifeel.com.tw/pro/document/news/all", paraAfter).Result) //使用 async 方法從網路 url 上取得回應
                    using (HttpContent content = response.Content)                                                                           //將網路取得回應的內容設定給 httpcontent
                    {
                        string result = await content.ReadAsStringAsync();                                                                   // 將 httpcontent 轉為 string

                        dynamic dy = JsonConvert.DeserializeObject <dynamic>(result);                                                        //將result轉換為dynamic物件,方便資料讀取

                        if (dy["_status"] == "Success")                                                                                      //抓取成功
                        {
                            Console.WriteLine("Get " + paraBefore["date"] + " news data successfully!" + "\n");

                            //依序將抓到的新聞內容存進News物件,並存進news list裡
                            for (int i = 0; i < dy["data"].Count; i++)
                            {
                                news.Add(new News(dy["data"][i]["content"].ToString()));
                                //Console.WriteLine(dy["data"][i]["content"].ToString() + "\n");
                            }
                        }
                        else //抓取失敗
                        {
                            Console.WriteLine("Get " + paraBefore["date"] + " news data unsuccessfully!" + "\n");
                        }
                    }
            }
            #endregion

            #region 文章斷詞與關鍵詞分析

            //合併個別文章
            for (int i = 0; i < news.Count; i++)
            {
                newsAppend += news[i].getNews();
            }

            var seg      = new JiebaSegmenter(); //建立Jieba分詞物件
            var segments = seg.Cut(newsAppend);  //進行斷詞

            segCount = 0;
            //計算分詞個數
            foreach (var segment in segments)
            {
                segCount++;
            }

            words = new string[segCount];
            int c = 0;
            //將分詞集合裡的詞依序存進words array
            foreach (var segment in segments)
            {
                words[c] = segment;
                c++;
            }

            //從本地端讀進stopwords.txt file,並存進stopwords陣列
            stopwords = File.ReadAllLines(System.IO.Directory.GetCurrentDirectory() + "\\Resources\\stopwords.txt");

            for (int i = 0; i < words.Length; i++)
            {
                for (int j = 0; j < stopwords.Length; j++)
                {
                    //若分詞屬於stopword,則從words list刪除該分詞
                    if (words[i] == stopwords[j])
                    {
                        words[i] = "Deleted!";
                        break;
                    }
                }

                if (words[i] != "Deleted!" && words[i] != " ")
                {
                    wordsWithoutSw.Add(words[i]);
                }
            }

            int count;
            //計算TF值
            for (int i = 0; i < wordsWithoutSw.Count; i++)
            {
                count = 1;
                if (wordTF.ContainsKey(wordsWithoutSw[i]) == false)   //wordTF dictionary裡無紀錄值才計算,以避免重複算的問題
                {
                    for (int j = i + 1; j < wordsWithoutSw.Count; j++)
                    {
                        if (wordsWithoutSw[i].Equals(wordsWithoutSw[j]))
                        {
                            count++;
                        }
                    }
                    wordTF.Add(wordsWithoutSw[i], ((decimal)count / wordsWithoutSw.Count));  //將計算後的次數加入到wordTF dictionary
                }
            }

            //取出前20大關鍵詞
            var top20 = wordTF.OrderByDescending(pair => pair.Value).Take(20)
                        .ToDictionary(pair => pair.Key, pair => pair.Value);

            #endregion

            #region Json格式轉換與檔案輸出

            Console.WriteLine("Top 20 most used keyword:");

            //依序讀取20個關鍵字,將關鍵字資料存成KeywordData物件,並存進keydata list
            foreach (KeyValuePair <string, decimal> top in top20)
            {
                Console.WriteLine("keyword = {0}, TF = {1}", top.Key, top.Value);
                KeywordData kd = new KeywordData();
                kd.text = top.Key;
                kd.size = (int)(top.Value * 4000);
                keydata.Add(kd);
            }

            var dataForOutput = JsonConvert.SerializeObject(keydata);                                                      //將list序列化為JSON字串
            dataForOutput = "data = '" + dataForOutput + "';";                                                             //轉換成HTML可讀取的資料格式
            //Console.WriteLine(dataForOutput);
            System.IO.File.WriteAllText(@"C:\Users\User\Desktop\資策會測驗\III program test\website\data.json", dataForOutput); //將json檔輸出至本地端
            #endregion
        }
    }
 private void OnExtractKeywordsUrl(KeywordData keywordData, string data)
 {
     Log.Debug("ExampleAlchemyLanguage", "Alchemy Language - Extract keywords response url: {0}", data);
     _getKeywordExtractionURLTested = true;
 }
 private void OnExtractKeywordsText(KeywordData keywordData, Dictionary <string, object> customData)
 {
     Log.Debug("ExampleAlchemyLanguage.OnExtractKeywordsText()", "Alchemy Language - Extract keywords response text: {0}", customData["json"].ToString());
     _getKeywordExtractionTextTested = true;
 }