コード例 #1
0
        //[DataMember(IsRequired = false,EmitDefaultValue = false)]
        //private readonly Dictionary<string, Dictionary<string, string[]>> glossMap;

        public ForeignWord(string word) : base(word)
        {
            if (word == null)
            {
                throw new ArgumentNullException("word", "Cannot construct words with null");
            }
            word = ProcessPuncuation(word);
            //!word.ContainsCheck("*")  foreign word might have no spaces
            if (word.ContainsCheck(" "))
            {
                throw new ArgumentNullException("word", "Normalized foreign words and phrases can have *, but no spaces [" + word + "]");
            }
            if (word.Split(new char[] { '*' }).Length > 5)
            {
                string  possibleTp = word.Replace("*", " ").Trim(new char[] { '"' });
                bool    hasLetters = word.ToLower().Any(x => Token.Alphabet.Contains(x));
                decimal percent    = NormalizeForeignText.PercentTokiPona(possibleTp);
                if (percent > 0.80m && hasLetters)
                {
                    throw new InvalidOperationException("This is a really long foreign word/phrase and it is " + percent + " toki pona : " + possibleTp);
                }
            }
            if (!(word.EndCheck("\"") && word.StartCheck("\"")))
            {
                throw new InvalidLetterSetException("Must be bracketed by double quotes [" + word + "]");
            }
            this.word = word;
        }
コード例 #2
0
        //[HttpParamAction]
        private ActionResult SaveText(SimpleParserViewModel parse)
        {
            parse.SnippetSavingError = null;

            string toSave = parse.SourceText;


            // Cast the Thread.CurrentPrincipal
            ClaimsPrincipal icp = Thread.CurrentPrincipal as ClaimsPrincipal;

            if (icp == null)
            {
                parse.SnippetSavingError = "<b>NOT SAVED. You must create an account and/or login</b>";
                parse.SnippetUrl         = parse.SnippetSavingError;
                parse.SnippetUrlParam    = "";
                return(View("Index", parse));
            }

            // Access IClaimsIdentity which contains claims
            ClaimsIdentity claimsIdentity = (ClaimsIdentity)icp.Identity;

            // Access claims
            string userId = null;

            foreach (Claim claim in claimsIdentity.Claims)
            {
                if (claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")
                {
                    userId = claim.Value;
                }
                //Debug.WriteLine(claim.Value);
            }
            if (userId == null)
            {
                parse.SnippetSavingError = "<b>NOT SAVED. You must create an account and/or login</b>";
                parse.SnippetUrl         = parse.SnippetSavingError;
                parse.SnippetUrlParam    = "";
                return(View("Index", parse));
            }

            decimal percent = NormalizeForeignText.PercentTokiPona(toSave);

            if (percent < 0.80m)
            {
                parse.SnippetSavingError = "<b>NOT SAVED. Insufficient toki pona here, only " + String.Format("Value: {0:P2}.", percent) + "</b>";
                parse.SnippetUrl         = parse.SnippetSavingError;
                parse.SnippetUrlParam    = "";
                return(View("Index", parse));
            }



            //Save to DB
            using (CorpusTextsContext context = new CorpusTextsContext())
            {
                int count = context.CorpusTexts.Count();

                var query = context.CorpusTexts.Where(x => x.SnippetText == toSave).FirstOrDefault();
                if (query == null)
                {
                    CorpusText text = new CorpusText();
                    text.Id               = NativeMethods.CreateGuid();
                    text.AspNetUserId     = userId;// icp.Identity.Name;
                    text.SnippetText      = toSave;
                    text.CreatedOn        = DateTime.Now;
                    text.UpdatedOn        = text.CreatedOn;
                    text.ShortUrl         = Base36Encode(Convert.ToUInt64(count));
                    parse.SnippetUrl      = MakeUrl(text.ShortUrl);
                    parse.SnippetUrlParam = text.ShortUrl;
                    context.CorpusTexts.Add(text);
                    context.SaveChanges();
                }
                else
                {
                    parse.SnippetUrl      = MakeUrl(query.ShortUrl);
                    parse.SnippetUrlParam = query.ShortUrl;
                }
            }

            return(Display(parse.SnippetUrlParam));
        }