示例#1
0
        //Save word
        public async Task <JsonResult> saveWord(string Voca)
        {
            string userId = User.Identity.GetUserId();


            DictCache dictCache = _dictCache.findWordCache(Voca);

            if (dictCache == null)
            {
                return(Json(new
                {
                    result = "False",
                }));
            }
            DetailOurWord detailWord = _detailOutWordService.findDetailOurWord(userId, dictCache.Id);

            //tick từ mới thành công
            if (detailWord == null)
            {
                ApplicationUser user = _service.GetUserById(userId);
                detailWord              = new DetailOurWord();
                detailWord.Id_User      = userId;
                detailWord.Id_Messenger = user.Id_Messenger;
                detailWord.Id_OurWord   = dictCache.Id;
                detailWord.Learned      = 1;
                detailWord.Id           = 0;
                detailWord.Schedule     = DateTime.Now.AddDays(-1);
                _detailOutWordService.Add(detailWord);
                _detailOutWordService.Save();
                await notifyMessenger(true);

                return(Json(new
                {
                    result = "True"
                }));
            }
            else
            {
                //tick cái nữa thì xóa tick
                _detailOutWordService.Delete(detailWord);
                _detailOutWordService.Save();
                return(Json(new
                {
                    result = "False"
                }));
            }
        }
示例#2
0
        public async Task <string> getDictToExtension(string contain)
        {
            bool   isVN   = false;
            string userid = User.Identity.GetUserId();

            contain = contain.ToString().ToLower().Trim();
            DictCache dictTemp = new DictCache();

            //Tim trong cache nếu ko có thì request crawler
            try
            {
                dictTemp = _dictCache.findWordCache(contain);
            }
            catch (Exception e)
            {
            }
            //post
            //
            bool        isSaveCached = true;
            DictCache   cacheDict;
            OxfordDict  dict             = new OxfordDict();
            GoogleTrans googleTransJson  = new GoogleTrans();
            GoogleTrans detailVietnamese = new GoogleTrans();
            //Explain main of letter
            MessJson messExplaintion = new MessJson();
            //Pron
            MessJson messPron = new MessJson();
            //Vietnamese
            MessJson messVietnamese = new MessJson();
            //
            ChatfuelJson   hello = new ChatfuelJson();
            AttachmentJson sound = new AttachmentJson();

            sound.attachment.type = "audio";
            Attachment2 attach = new Attachment2();

            if (contain.LastOrDefault() == '.')
            {
                messPron.text = "";
                hello.messages.Add(messPron);
                return(JsonConvert.SerializeObject(hello));
            }
            Payload2 payload = new Payload2();

            //
            if (dictTemp != null)
            {
                messPron.text                = dictTemp.Pron;
                messExplaintion.text         = dictTemp.MeanEn;
                messVietnamese.text          = dictTemp.MeanVi;
                sound.attachment.payload.url = dictTemp.SoundUrl;
            }
            else
            {
                int size = contain.Split(' ').Length;
                if (size > 1)
                {
                    isSaveCached    = false;
                    googleTransJson = await _clientDictionaryService.startGoogleTrans(contain);

                    hello.strVietnamese = googleTransJson.src;
                    if (googleTransJson.src == "vi")
                    {
                        isVN = true;
                    }
                    messExplaintion.text = googleTransJson.sentences[0].trans;
                }
                else
                {
                    try
                    {
                        dict = await _clientDictionaryService.startCrawlerOxford(contain);

                        messPron.text        = dict.m_Pron;
                        messExplaintion.text = dict.m_Explanation.First().m_UseCase;
                    }
                    catch
                    {
                        isSaveCached         = false;
                        messExplaintion.text = contain;
                    }
                    detailVietnamese = await _clientDictionaryService.startGoogleDetailTrans(contain);

                    hello.strVietnamese = detailVietnamese.src;
                    if (detailVietnamese.src == "vi")
                    {
                        isVN = true;
                    }
                    try
                    {
                        messVietnamese.text = detailVietnamese.dict.First().pos + ": ";
                        foreach (var item in detailVietnamese.dict.First().terms)
                        {
                            messVietnamese.text += item + ", ";
                        }
                    }
                    catch
                    {
                        isSaveCached        = false;
                        messVietnamese.text = detailVietnamese.sentences.FirstOrDefault().trans;
                    }
                }
                sound.attachment.payload.url = dict.m_SoundUrl;
                //Add database
                cacheDict          = new DictCache();
                cacheDict.VocaID   = contain;
                cacheDict.Pron     = messPron.text;
                cacheDict.MeanEn   = messExplaintion.text;
                cacheDict.MeanVi   = messVietnamese.text;
                cacheDict.SoundUrl = dict.m_SoundUrl;
                _dictCache.Add(cacheDict);
                if (isSaveCached)
                {
                    _dictCache.Save();
                }
                dictTemp = cacheDict;
            }
            DetailOurWord detailWord = _detailOutWordService.findDetailOurWord(userid, dictTemp.Id);

            if (detailWord == null)
            {
                hello.Selected = false;
            }
            else
            {
                hello.Selected = true;
            }
            hello.messages.Add(messPron);
            hello.messages.Add(messVietnamese);
            hello.messages.Add(messExplaintion);
            hello.messages.Add(sound);
            if (isVN)
            {
                return(null);
            }
            return(JsonConvert.SerializeObject(hello));
        }
示例#3
0
        public async Task <string> callChatBot(string contain, string id, string userid)
        {
            contain = contain.ToString().ToLower().Trim();
            //Tim trong cache nếu ko có thì request crawler
            var dictTemp = _dictCache.findWordCache(contain);
            //post
            //
            bool        isSaveCached     = true;
            DictCache   cacheDict        = new DictCache();
            OxfordDict  dict             = new OxfordDict();
            GoogleTrans googleTransJson  = new GoogleTrans();
            GoogleTrans detailVietnamese = new GoogleTrans();
            //Explain main of letter
            MessJson messExplaintion = new MessJson();
            //Pron
            MessJson messPron = new MessJson();
            //Vietnamese
            MessJson messVietnamese = new MessJson();
            //
            ChatfuelJson   hello = new ChatfuelJson();
            AttachmentJson sound = new AttachmentJson();

            sound.attachment.type = "audio";
            Attachment2 attach = new Attachment2();

            if (contain.LastOrDefault() == '.')
            {
                messPron.text = "";
                hello.messages.Add(messPron);
                return(JsonConvert.SerializeObject(hello));
            }
            Payload2 payload = new Payload2();

            //
            if (dictTemp != null)
            {
                messPron.text                = dictTemp.Pron;
                messExplaintion.text         = dictTemp.MeanEn;
                messVietnamese.text          = dictTemp.MeanVi;
                sound.attachment.payload.url = dictTemp.SoundUrl;
            }
            else
            {
                int size = contain.Split(' ').Length;
                if (size > 1)
                {
                    googleTransJson = await _clientDictionaryService.startGoogleTrans(contain);

                    messExplaintion.text = googleTransJson.sentences[0].trans;
                }
                else
                {
                    try
                    {
                        dict = await _clientDictionaryService.startCrawlerOxford(contain);

                        messPron.text        = dict.m_Pron;
                        messExplaintion.text = dict.m_Explanation.First().m_UseCase;
                    }
                    catch
                    {
                        isSaveCached         = false;
                        messExplaintion.text = contain;
                    }
                    detailVietnamese = await _clientDictionaryService.startGoogleDetailTrans(contain);

                    try
                    {
                        messVietnamese.text = detailVietnamese.dict.First().pos + ": ";
                        foreach (var item in detailVietnamese.dict.First().terms)
                        {
                            messVietnamese.text += item + ", ";
                        }
                    }
                    catch
                    {
                        isSaveCached        = false;
                        messVietnamese.text = detailVietnamese.sentences.FirstOrDefault().trans;
                    }
                }
                sound.attachment.payload.url = dict.m_SoundUrl;
                //Add database
                cacheDict          = new DictCache();
                cacheDict.VocaID   = contain;
                cacheDict.Pron     = messPron.text;
                cacheDict.MeanEn   = messExplaintion.text;
                cacheDict.MeanVi   = messVietnamese.text;
                cacheDict.SoundUrl = dict.m_SoundUrl;
                _dictCache.Add(cacheDict);
                if (isSaveCached)
                {
                    _dictCache.Save();
                }
                dictTemp = cacheDict;
            }
            DetailOurWord detailWord = _detailOutWordService.findDetailOurWord(userid, dictTemp.Id);

            if (detailWord == null)
            {
                hello.Selected = false;
            }
            else
            {
                hello.Selected = true;
            }
            hello.messages.Add(messPron);
            hello.messages.Add(messVietnamese);
            hello.messages.Add(messExplaintion);
            hello.messages.Add(sound);
            try
            {
                hello.strVoca = ToTitleCase(contain);
                ApplicationUser currentUser = _service.listUserID().Where(x => x.Id_Messenger == id).FirstOrDefault();
                NotificationHub.sendNoti(currentUser.Email, JsonConvert.SerializeObject(hello));
            }
            catch
            {
            }
            return(JsonConvert.SerializeObject(hello));
        }
示例#4
0
        public async Task <JsonResult> Tick(OurWordViewModel data)
        {
            if (data != null)
            {
                DictCache dictCache = _dictCache.findWordCache(data.VocaID);

                if (dictCache == null)
                {
                    dictCache   = new DictCache();
                    data.MeanVi = HttpUtility.HtmlDecode(data.MeanVi).ToString();
                    FieldHelper.CopyNotNullValue(dictCache, data);
                    try
                    {
                        _dictCache.Add(dictCache);
                        _dictCache.Save();
                    }
                    catch (Exception e)
                    {
                        return(Json(new
                        {
                            result = "failed",
                        }));
                    }
                }
                var userId = User.Identity.GetUserId();

                DetailOurWord detailWord = _detailOutWordService.findDetailOurWord(userId, dictCache.Id);

                if (detailWord == null)
                {
                    ApplicationUser user = _service.GetUserById(userId);
                    detailWord              = new DetailOurWord();
                    detailWord.Id_User      = userId;
                    detailWord.Id_Messenger = user.Id_Messenger;
                    detailWord.Id_OurWord   = dictCache.Id;
                    detailWord.Learned      = 1;
                    detailWord.Id           = 0;
                    detailWord.Schedule     = DateTime.Now.AddDays(-1);
                    _detailOutWordService.Add(detailWord);
                }
                else
                {
                    _detailOutWordService.Delete(detailWord);
                }
                try
                {
                    _detailOutWordService.Save();
                    await notifyMessenger();

                    return(Json(new
                    {
                        result = "success"
                    }));
                }
                catch (Exception e)
                {
                    return(Json(new
                    {
                        result = "failed",
                    }));
                }
            }
            return(Json(new
            {
                result = "failed"
            }));
        }