Example #1
0
        public static List<Recette> SelectBestRecettes(List<Recette> recettes, List<Product> products, int limit = 3)
        {
            List<Tuple<int, List<string>>> lesMotsDesProduits = products.Select(o => new Tuple<int, List<string>>(o.ID, o.Libelle.Split(' ').ToList())).ToList();
            List<Tuple<int, List<List<string>>>> lesMotsDesRecettes = recettes.Select(o => new Tuple<int, List<List<string>>>(o.ID, o.Ingrédients.Select(i => i.Split(' ').ToList()).ToList())).ToList();

            lesMotsDesProduits.ForEach(o => o.Item2.RemoveAll(i => uselessWords.Contains(i)));
            lesMotsDesRecettes.ForEach(o => o.Item2.ForEach(l => l.RemoveAll(s => uselessWords.Contains(s))));

            Dictionary<int, int> classementRecette = new Dictionary<int, int>();

            foreach (Tuple<int, List<List<string>>> recette in lesMotsDesRecettes)
            {
                foreach (List<string> ingredient in recette.Item2)
                {
                    if (!classementRecette.ContainsKey(recette.Item1))
                    {
                        classementRecette[recette.Item1] = 0;
                    }
                    classementRecette[recette.Item1] += lesMotsDesProduits.Sum(o => o.Item2.Where(i => ingredient.Contains(i)).Count());
                }
            }
            classementRecette = classementRecette.OrderBy(o => o.Value).ToDictionary(o => o.Key, i => i.Value);

            return classementRecette.Values.All(o => o == 0) ? null : classementRecette.Take(limit).Select(r => recettes.Where(o => o.ID == r.Key && r.Value > 0).First()).ToList();
        }
Example #2
0
        private Dictionary<char, int> TenMostFrequentChar(string value)
        {
            var charDictionary = new Dictionary<char, int>();

            foreach (var x in value)
            {
                if (charDictionary.ContainsKey(x))
                {
                    charDictionary[x] = charDictionary[x] + 1;
                }
                else
                {
                    charDictionary.Add(x, 1);
                }
            }

            //This will order the Dictionary by the values
            charDictionary = charDictionary.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            if (charDictionary.Count < 10)
            {
                return charDictionary;
            }
            else
            {
                return charDictionary.Take(10).ToDictionary(x => x.Key, x => x.Value);
            }
        }
Example #3
0
 public Bitmap CreateCloud(Config config, Dictionary<string, int> words)
 {
     var bitmap = new Bitmap(config.ConfigModel.Width, config.ConfigModel.Height);
     using (var graphics = Graphics.FromImage(bitmap))
     {
         float height = 0;
         graphics.Clear(Color.FromName(config.ConfigModel.Background));
         var sum = words.Take(config.ConfigModel.Count).Sum(x => x.Value);
         foreach (var word in words.Take(config.ConfigModel.Count))
         {
             var wordHeight = (bitmap.Height/sum)*word.Value;
             DrawWord(word, graphics, wordHeight, height, config);
             height += wordHeight;
         }
     }
     return bitmap;
 }
Example #4
0
 private void SplitByRack(Dictionary<int, int> smpPlasmaSlices)
 {
     int totalSliceCnt = smpPlasmaSlices.Count;
     while (smpPlasmaSlices.Any())
     {
         int remaining = smpPlasmaSlices.Count;
         int splitted = totalSliceCnt - remaining;
         var tmpPlasmaSlices = smpPlasmaSlices.Take(Math.Min(remaining, 16)).ToDictionary(x => x.Key, x => x.Value);
         allRackSmpPlasmaSlices.Add(tmpPlasmaSlices);
         var tmpPackageInfos = CreatePackInfo(tmpPlasmaSlices);
         allPackageExpectedInfos.Add(new Dictionary<Point, string>(tmpPackageInfos));
         allPackageScannedInfos.Add(new Dictionary<Point, string>());
         smpPlasmaSlices = smpPlasmaSlices.Except(tmpPlasmaSlices).ToDictionary(x => x.Key, x => x.Value);
     }
     ChangeRackIndex(0);
 }
        private string LoadSongs(string path, int category)
        {
            // read the index file from disk
            StreamReader sr = new StreamReader(path);
            string lyrics = sr.ReadToEnd();

            // parse the index file for songs and rank
            Dictionary<int, double> songs = new Dictionary<int, double>();
            foreach (string line in lyrics.Split('\n'))
            {
                try
                {
                    songs.Add(Convert.ToInt32(line.Split(' ')[0]), Convert.ToDouble(line.Split(' ')[1]));
                }
                catch (Exception ex)
                {
                    continue;
                }
            }

            // initialize web client
            WebClient client = new WebClient();

            // build the results output
            string output = "";
            foreach (KeyValuePair<int, double> song in songs.Take(5))
            {
                // download song information and album art from MusixMatch
                string response = client.DownloadString("http://api.musixmatch.com/ws/1.1/track.get?track_id=" + song.Key + "&apikey=" + apiKey);

                var track = Json.Decode(response).message.body.track;

                string albumCover = track.album_coverart_350x350 == "" ? track.album_coverart_100x100 : track.album_coverart_350x350;

                // append the raw HTML content
                output += "<div class='span2'><a href='view.aspx?id=" + track.track_id + "&category=" + category + "'><img class='img-polaroid' width='150' height='150' style='width: 150px; height: 150px;' src='" + albumCover + "'></a><h4><a href='view.aspx?id=" + track.track_id + "&category=" + category + "'>" + track.track_name + "</a></h4><p>by " + track.artist_name + "</p></div>\n";
            }

            // return the rendered results
            return output;
        }
Example #6
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var shapePageReference = new Rock.Web.PageReference(GetAttributeValue("SHAPEAssessmentPage"));
            var discPageReference = new Rock.Web.PageReference(GetAttributeValue("DISCAssessmentPage"));

            if (!string.IsNullOrWhiteSpace(PageParameter("FormId")))
                {
                    //Load the person based on the FormId
                    var personInUrl = PageParameter("FormId");
                    SelectedPerson = GetPersonFromForm(personInUrl);
                    PersonEncodedKey = SelectedPerson.UrlEncodedKey;
                }

                else if (!string.IsNullOrWhiteSpace(PageParameter("PersonId")))
                {
                    //Load the person based on the PersonId
                    SelectedPerson = GetPersonFromId(PageParameter("PersonId"));
                    PersonEncodedKey = SelectedPerson.UrlEncodedKey;
                 }

                else if (CurrentPerson != null)
                {
                    //Load the person based on the currently logged in person
                    SelectedPerson = CurrentPerson;
                    PersonEncodedKey = SelectedPerson.UrlEncodedKey;
                }

                else
                {
                    //Show Error Message
                    nbNoPerson.Visible = true;
                    Response.Redirect(shapePageReference.BuildUrl(), true);
                    return;
                }

            // Load the attributes

            AttributeValueService attributeValueService = new AttributeValueService(rockContext);
            DefinedValueService definedValueService = new DefinedValueService(rockContext);

            string spiritualGift1 = "";
            string spiritualGift2 = "";
            string spiritualGift3 = "";
            string spiritualGift4 = "";
            string heartCategories = "";
            string heartCauses = "";
            string heartPassion = "";
            string ability1 = "";
            string ability2 = "";
            string people = "";
            string places = "";
            string events = "";

            var spiritualGift1AttributeValue =
                attributeValueService
                    .Queryable()
                    .FirstOrDefault(a => a.Attribute.Key == "SpiritualGift1" && a.EntityId == SelectedPerson.Id);

            // Redirect if they haven't taken the Assessment
            if (spiritualGift1AttributeValue == null)
            {
                Response.Redirect(shapePageReference.BuildUrl(), true);
            }

            else
            {
                var spiritualGift2AttributeValue =
              attributeValueService
                  .Queryable()
                  .FirstOrDefault(a => a.Attribute.Key == "SpiritualGift2" && a.EntityId == SelectedPerson.Id);

                var spiritualGift3AttributeValue =
              attributeValueService
                  .Queryable()
                  .FirstOrDefault(a => a.Attribute.Key == "SpiritualGift3" && a.EntityId == SelectedPerson.Id);

                var spiritualGift4AttributeValue =
              attributeValueService
                  .Queryable()
                  .FirstOrDefault(a => a.Attribute.Key == "SpiritualGift4" && a.EntityId == SelectedPerson.Id);

                var ability1AttributeValue =
                    attributeValueService
                        .Queryable().FirstOrDefault(a => a.Attribute.Key == "Ability1" && a.EntityId == SelectedPerson.Id);

                var ability2AttributeValue =
                    attributeValueService
                        .Queryable().FirstOrDefault(a => a.Attribute.Key == "Ability2" && a.EntityId == SelectedPerson.Id);

                var peopleAttributeValue = attributeValueService
                    .Queryable()
                    .FirstOrDefault(a => a.Attribute.Key == "SHAPEPeople" && a.EntityId == SelectedPerson.Id);

                var placesAttributeValue = attributeValueService
                    .Queryable()
                    .FirstOrDefault(a => a.Attribute.Key == "SHAPEPlaces" && a.EntityId == SelectedPerson.Id);

                var eventsAttributeValue = attributeValueService
                    .Queryable()
                    .FirstOrDefault(a => a.Attribute.Key == "SHAPEEvents" && a.EntityId == SelectedPerson.Id);

                var heartCategoriesAttributeValue = attributeValueService
                    .Queryable()
                    .FirstOrDefault(a => a.Attribute.Key == "HeartCategories" && a.EntityId == SelectedPerson.Id);

                var heartCausesAttributeValue = attributeValueService
                    .Queryable()
                    .FirstOrDefault(a => a.Attribute.Key == "HeartCauses" && a.EntityId == SelectedPerson.Id);

                var heartPassionAttributeValue = attributeValueService
                    .Queryable()
                    .FirstOrDefault(a => a.Attribute.Key == "HeartPassion" && a.EntityId == SelectedPerson.Id);

                if (spiritualGift1AttributeValue.Value != null) spiritualGift1 = spiritualGift1AttributeValue.Value;
                if (spiritualGift2AttributeValue.Value != null) spiritualGift2 = spiritualGift2AttributeValue.Value;
                if (spiritualGift3AttributeValue.Value != null) spiritualGift3 = spiritualGift3AttributeValue.Value;
                if (spiritualGift4AttributeValue.Value != null) spiritualGift4 = spiritualGift4AttributeValue.Value;
                if (heartCategoriesAttributeValue.Value != null) heartCategories = heartCategoriesAttributeValue.Value;
                if (heartCausesAttributeValue.Value != null) heartCauses = heartCausesAttributeValue.Value;
                if (heartPassionAttributeValue.Value != null) heartPassion = heartPassionAttributeValue.Value;
                if (ability1AttributeValue.Value != null) ability1 = ability1AttributeValue.Value;
                if (ability2AttributeValue.Value != null) ability2 = ability2AttributeValue.Value;
                if (peopleAttributeValue.Value != null) people = peopleAttributeValue.Value;
                if (placesAttributeValue.Value != null) places = placesAttributeValue.Value;
                if (eventsAttributeValue.Value != null) events = eventsAttributeValue.Value;

                string spiritualGift1Guid;
                string spiritualGift2Guid;
                string spiritualGift3Guid;
                string spiritualGift4Guid;
                string ability1Guid;
                string ability2Guid;

                // Check to see if there are already values saved as an ID.  If so, convert them to GUID
                if (spiritualGift1.ToString().Length < 5)
                {
                    if (spiritualGift1 != null) SpiritualGift1 = Int32.Parse(spiritualGift1);
                    if (spiritualGift2 != null) SpiritualGift2 = Int32.Parse(spiritualGift2);
                    if (spiritualGift3 != null) SpiritualGift3 = Int32.Parse(spiritualGift3);
                    if (spiritualGift4 != null) SpiritualGift4 = Int32.Parse(spiritualGift4);
                    if (ability1 != null) Ability1 = Int32.Parse(ability1);
                    if (ability2 != null) Ability2 = Int32.Parse(ability2);

                    var intsOfGifts =
                        definedValueService.GetByIds(new List<int>
                        {
                            SpiritualGift1,
                            SpiritualGift2,
                            SpiritualGift3,
                            SpiritualGift4,
                            Ability1,
                            Ability2
                        });

                    spiritualGift1Guid = intsOfGifts.ToList()[SpiritualGift1].Guid.ToString();
                    spiritualGift2Guid = intsOfGifts.ToList()[SpiritualGift2].Guid.ToString();
                    spiritualGift3Guid = intsOfGifts.ToList()[SpiritualGift3].Guid.ToString();
                    spiritualGift4Guid = intsOfGifts.ToList()[SpiritualGift4].Guid.ToString();
                    ability1Guid = intsOfGifts.ToList()[Ability1].Guid.ToString();
                    ability2Guid = intsOfGifts.ToList()[Ability2].Guid.ToString();
                }
                else
                {
                    spiritualGift1Guid = spiritualGift1;
                    spiritualGift2Guid = spiritualGift2;
                    spiritualGift3Guid = spiritualGift3;
                    spiritualGift4Guid = spiritualGift4;
                    ability1Guid = ability1;
                    ability2Guid = ability2;
                }

                // Get all of the data about the assiciated gifts and ability categories
                var shapeGift1Object = definedValueService.GetListByGuids(new List<Guid> { new Guid(spiritualGift1Guid) }).FirstOrDefault();
                var shapeGift2Object = definedValueService.GetListByGuids(new List<Guid> { new Guid(spiritualGift2Guid) }).FirstOrDefault();
                var shapeGift3Object = definedValueService.GetListByGuids(new List<Guid> { new Guid(spiritualGift3Guid) }).FirstOrDefault();
                var shapeGift4Object = definedValueService.GetListByGuids(new List<Guid> { new Guid(spiritualGift4Guid) }).FirstOrDefault();
                var ability1Object = definedValueService.GetListByGuids(new List<Guid> { new Guid(ability1Guid) }).FirstOrDefault();
                var ability2Object = definedValueService.GetListByGuids(new List<Guid> { new Guid(ability2Guid) }).FirstOrDefault();

                shapeGift1Object.LoadAttributes();
                shapeGift2Object.LoadAttributes();
                shapeGift3Object.LoadAttributes();
                shapeGift4Object.LoadAttributes();
                ability1Object.LoadAttributes();
                ability2Object.LoadAttributes();

                // Get heart choices Values from Guids
                string heartCategoriesString = "";
                if (!heartCategories.IsNullOrWhiteSpace())
                {
                    string[] heartCategoryArray = heartCategories.Split(',');
                    foreach (string category in heartCategoryArray)
                    {
                        var definedValueObject =
                            definedValueService.Queryable().FirstOrDefault(a => a.Guid == new Guid(category));

                        if (category.Equals(heartCategoryArray.Last()))
                        {
                            heartCategoriesString += definedValueObject.Value;
                        }
                        else
                        {
                            heartCategoriesString += definedValueObject.Value + ", ";
                        }
                    }

                }

                // Get Volunteer Opportunities

                string gift1AssociatedVolunteerOpportunities =
                    shapeGift1Object.GetAttributeValue("AssociatedVolunteerOpportunities");
                string gift2AssociatedVolunteerOpportunities =
                    shapeGift2Object.GetAttributeValue("AssociatedVolunteerOpportunities");
                string gift3AssociatedVolunteerOpportunities =
                    shapeGift3Object.GetAttributeValue("AssociatedVolunteerOpportunities");
                string gift4AssociatedVolunteerOpportunities =
                    shapeGift4Object.GetAttributeValue("AssociatedVolunteerOpportunities");

                string allAssociatedVolunteerOpportunities = gift1AssociatedVolunteerOpportunities + "," +
                                                             gift2AssociatedVolunteerOpportunities + "," +
                                                             gift3AssociatedVolunteerOpportunities + "," +
                                                             gift4AssociatedVolunteerOpportunities;

                if (allAssociatedVolunteerOpportunities != ",,,")
                {
                    List<int> associatedVolunteerOpportunitiesList =
                        allAssociatedVolunteerOpportunities.Split(',').Select(t => int.Parse(t)).ToList();
                    Dictionary<int, int> VolunteerOpportunities = new Dictionary<int, int>();

                    var i = 0;
                    var q = from x in associatedVolunteerOpportunitiesList
                            group x by x
                        into g
                            let count = g.Count()
                            orderby count descending
                            select new { Value = g.Key, Count = count };
                    foreach (var x in q)
                    {
                        VolunteerOpportunities.Add(i, x.Value);
                        i++;
                    }

                    ConnectionOpportunityService connectionOpportunityService = new ConnectionOpportunityService(rockContext);
                    List<ConnectionOpportunity> connectionOpportunityList = new List<ConnectionOpportunity>();

                    foreach (KeyValuePair<int, int> entry in VolunteerOpportunities.Take(4))
                    {
                        var connection = connectionOpportunityService.GetByIds(new List<int> { entry.Value }).FirstOrDefault();

                        // Only display connection if it is marked Active
                        if (connection.IsActive == true)
                        {
                            connectionOpportunityList.Add(connection);
                        }

                    }

                    rpVolunteerOpportunities.DataSource = connectionOpportunityList;
                    rpVolunteerOpportunities.DataBind();
                }

                //Get DISC Info

                DiscService.AssessmentResults savedScores = DiscService.LoadSavedAssessmentResults(SelectedPerson);

                if (!string.IsNullOrWhiteSpace(savedScores.PersonalityType))
                {
                    ShowResults(savedScores);
                    DISCResults.Visible = true;
                    NoDISCResults.Visible = false;

                }
                else
                {
                    discPageReference.Parameters = new System.Collections.Generic.Dictionary<string, string>();
                    discPageReference.Parameters.Add("rckipid", SelectedPerson.UrlEncodedKey);
                    Response.Redirect(discPageReference.BuildUrl(), true);
                }

                // Build the UI

                lbPersonName.Text = SelectedPerson.FullName;

                lbGift1Title.Text = shapeGift1Object.Value;
                lbGift1BodyHTML.Text = shapeGift1Object.GetAttributeValue("HTMLDescription");

                lbGift2Title.Text = shapeGift2Object.Value;
                lbGift2BodyHTML.Text = shapeGift2Object.GetAttributeValue("HTMLDescription");

                lbGift3Title.Text = shapeGift3Object.Value;
                lbGift3BodyHTML.Text = shapeGift3Object.GetAttributeValue("HTMLDescription");

                lbGift4Title.Text = shapeGift4Object.Value;
                lbGift4BodyHTML.Text = shapeGift4Object.GetAttributeValue("HTMLDescription");

                lbAbility1Title.Text = ability1Object.Value;
                lbAbility1BodyHTML.Text = ability1Object.GetAttributeValue("HTMLDescription");

                lbAbility2Title.Text = ability2Object.Value;
                lbAbility2BodyHTML.Text = ability2Object.GetAttributeValue("HTMLDescription");

                lbPeople.Text = people;
                lbPlaces.Text = places;
                lbEvents.Text = events;

                lbHeartCategories.Text = heartCategoriesString;
                lbHeartCauses.Text = heartCauses;
                lbHeartPassion.Text = heartPassion;

                if (spiritualGift1AttributeValue.ModifiedDateTime != null)
                {
                    lbAssessmentDate.Text = spiritualGift1AttributeValue.ModifiedDateTime.Value.ToShortDateString();
                }
                else
                {
                    lbAssessmentDate.Text = spiritualGift1AttributeValue.CreatedDateTime.Value.ToShortDateString();
                }

                // Show create account panel if this person doesn't have an account
                if (SelectedPerson.Users.Count == 0)
                {
                    pnlAccount.Visible = true;
                }

            }
        }
 private void PrintRequests(string type, Dictionary<string, int> sortedDict, int sum)
 {
     m_log.InfoFormat("[INFO]:");
     m_log.InfoFormat("[INFO]: {0,25}", type);
     foreach (KeyValuePair<string, int> kvp in sortedDict.Take(12))
         m_log.InfoFormat("[INFO]: {0,25} {1,-6}", kvp.Key, kvp.Value);
     m_log.InfoFormat("[INFO]: {0,25}", "...");
     m_log.InfoFormat("[INFO]: {0,25} {1,-6}", "Total", sum);
 }
Example #8
0
        /// <summary>
        /// Vrátí součet rozdílů očekávaných a nalezených procentuálních výskytů znaků
        /// </summary>
        /// <param name="text"></param>
        /// <param name="occurrence"></param>
        /// <returns></returns>
        private static double SumSimilarity(string text, Dictionary<string, double> occurrence)
        {
            Dictionary<string, double> standardLettersOccurrence =
                occurrence.Take(6).ToDictionary(x => x.Key, x => x.Value);

            Dictionary<string, double> inTextLettersOcc = TextAnalysis.GetOccurrence(text, occurrence.Keys.Take(1).ToArray()[0].Length);

            double sum = 0;

            foreach (KeyValuePair<string, double> occ in standardLettersOccurrence)
            {
                if (inTextLettersOcc.ContainsKey(occ.Key))
                {
                    sum += Math.Abs(occ.Value - inTextLettersOcc[occ.Key]);
                }
                else
                {
                    sum += 10;
                }
            }

            return sum;
        }
        public static List<FeedResultItem> GetFeeds(FeedApiFilter filter)
        {
            var filterOffset = filter.Offset;
            var filterLimit = filter.Max > 0 && filter.Max < 1000 ? filter.Max : 1000;

            var feeds = new Dictionary<string, List<FeedResultItem>>();

            var tryCount = 0;
            List<FeedResultItem> feedsIteration;
            do
            {
                feedsIteration = GetFeedsInternal(filter);
                foreach (var feed in feedsIteration)
                {
                    if (feeds.ContainsKey(feed.GroupId))
                    {
                        feeds[feed.GroupId].Add(feed);
                    }
                    else
                    {
                        feeds[feed.GroupId] = new List<FeedResultItem> { feed };
                    }
                }
                filter.Offset += feedsIteration.Count;
            } while (feeds.Count < filterLimit
                     && feedsIteration.Count == filterLimit
                     && tryCount++ < 5);

            filter.Offset = filterOffset;
            return feeds.Take(filterLimit).SelectMany(group => group.Value).ToList();
        }
Example #10
0
        //serach bar action for ajax send
        public ActionResult SearchActionMethod(string word)
        {
            Dictionary<string[], int> words = new Dictionary<string[], int>();
            foreach (var item in client.GetProducts())
            {
                if (item.Name.ToUpper().Contains(word.ToUpper()) && word != String.Empty)
                {
                    words.Add(new string[] { "pro", item.Name }, item.Id);

                }
            }
            foreach (var item in client.GetShipping_Companys())
            {
                if (item.Company_Name.ToUpper().Contains(word.ToUpper()) && word != String.Empty)
                {
                    words.Add(new string[] { "com", item.Company_Name }, item.Id);
                }
            }
            foreach (var item in client.SubCategories())
            {
                if (item.Name.ToUpper().Contains(word.ToUpper()) && word != String.Empty)
                {
                    words.Add(new string[] { "sub", item.Name }, item.Id);
                }
            }
            //foreach (var item in client.GetProducts())
            //{
            //    if (item.Name.ToUpper().Contains(word.ToUpper()) && word != String.Empty)
            //    {
            //        words.Add("",item.Name);
            //    }
            //}
            foreach (var item in client.GetCategories())
            {
                if (item.Name.ToUpper().Contains(word.ToUpper()) && word != String.Empty)
                {
                    words.Add(new string[] { "cat", item.Name }, item.Id);
                }

            }
            //client.GetCategories();

            return Json(words.Take(7), JsonRequestBehavior.AllowGet);
        }
        public void Iterate()
        {
            Dictionary<Particle, Rank> ranks = new Dictionary<Particle, Rank>();

            // Determine how strongly each Particle is being pulled in the four cardinal directions
            for (int x = 0; x < Size; ++x) {
                for (int y = 0; y < Size; ++y) {
                    Particle p = _lattice[x, y];
                    if (p == null)
                        continue;

                    Rank r = getParticleRank(p);
                    ranks.Add(p, r);
                }
            }

            // Compute the energy of the system in this Iteration
            _energy = ranks.Sum(r => Math.Abs(r.Value.Pulls[r.Value.MaxDirection]));

            // Sort the particles by the max magnitude of all their pull directions
            ranks = ranks.OrderBy(r => r.Value.Pulls[r.Value.MaxDirection])
                         .ToDictionary(r => r.Key, r => r.Value);

            // Process the Particles of highest rank (and any in their way) until all have been processed
            do {
                KeyValuePair<Particle, Rank> pair = ranks.Take(1).Single();
                processRank(pair.Key, pair.Value, ref ranks);
            } while (ranks.Count > 0);
        }
Example #12
0
        public void UpsertDocuments(Dictionary<string, string> docsToUpsert)
        {
            while (docsToUpsert.Any()) {
                var upsertBatch = docsToUpsert.Take (32).ToList ();
                upsertBatch.ForEach (b => docsToUpsert.Remove (b.Key));
                var upsertCommand = new StringBuilder ();
                foreach (var kvp in upsertBatch) {
                    var segments = kvp.Key.Split ('_');
                    string environment = segments [0];
                    string type = segments [1];

                    upsertCommand.AppendFormat ("exec UpsertDocument @id='{0}', @index='{1}', @type='{2}',  @doc='{3}'", kvp.Key, environment, type, kvp.Value.Replace ("'", "''"));
                }

                string sqlText = upsertCommand.ToString ();
                using (var _loadConnection = new SqlConnection(Common.TargetConnectionString)) {
                    _loadConnection.Open ();
                    using (var upsertCmd = new SqlCommand(sqlText, _loadConnection)) {
                        try {
                            upsertCmd.ExecuteNonQuery ();
                        } catch (SqlException sex) {
                            Console.WriteLine ("SqlException: {0}", sex.Message);
                            foreach (var d in upsertBatch) { // requeue these docs for upsert
                                docsToUpsert.Add (d.Key, d.Value);
                            }
                        }
                    }
                }
            }
        }
Example #13
0
        public ulong S_1(Dictionary<ulong, int> guesses)
        {
            ulong start = 1;
            ulong stop = 10;
            while(stop<guesses.First().Key)
            {
                start *= 10;
                stop *= 10;
            }

            var poss = new List<ulong>();

            foreach (var g in guesses.Take(1))
            {
                ulong guess = g.Key;
                int correct = g.Value;

                for (ulong i = start; i < guess; i++)
                {
                    ulong diff = guess - i;
                    int count = 0;

                    while (diff > 0)
                    {
                        if (diff%10 == 0)
                        {
                            count++;
                            if (count > correct)
                            {
                                //continue to for loop
                                diff = 0;
                            }
                        }

                        diff /= 10;
                    }

                    if (count == correct)
                        poss.Add(i);

                }
                for (ulong i = guess + 1; i < stop; i++)
                {
                    ulong diff = i - guess;
                    int count = 0;

                    while (diff > 0)
                    {
                        if (diff%10 == 0)
                        {
                            count++;
                            if (count > correct)
                            {
                                //continue to for loop
                                diff = 0;
                            }
                        }

                        diff /= 10;
                    }

                    if (count == correct)
                        poss.Add(i);
                }
            }

            foreach (var g in guesses.Skip(1))
            {
                ulong guess = g.Key;
                int correct = g.Value;

                foreach (var p in poss)
                {
                    ulong diff = p > guess ? p - guess : guess - p;
                    int count = 0;

                    while (diff > 0)
                    {
                        if (diff%10 == 0)
                        {
                            count++;
                            if (count > correct)
                            {
                                //continue to for loop
                                diff = 0;
                            }
                        }

                        diff /= 10;
                    }

                    if (count != correct)
                        poss.Remove(p);
                }
            }

            Debug.Assert(poss.Count==1);

            return poss.Single();
        }
Example #14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        public SearchResponse SearchDocumentSpace(SearchRequest req)
        {
            SearchResponse resp = new SearchResponse();
            try
            {
                if (req == null || String.IsNullOrEmpty(req.DocumentSpace) || String.IsNullOrEmpty(req.SearchWords))
                    return resp;

                resp.DocumentSpace = req.DocumentSpace;

                Dictionary<int, Document> dmnts = new Dictionary<int, Document>();

                Action repack = () =>
                {
                    //Repacking dmnts into resp
                    if (req.IncludeDocuments)
                    {
                        foreach (var el in dmnts)
                        {
                            resp.Documents.Add(el.Value);
                        }
                    }
                    else
                    {
                        foreach (var el in dmnts)
                        {
                            resp.DocumentsInternalIds.Add(el.Key);
                        }
                    }
                };

                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();

                using (var tran = DBreezeEngine.GetTransaction())
                {
                    var mt = tran.SelectTable<int>(DocumentsStorageTablesPrefix + "m", 1, 0);
                    var docSpaceId = mt.Select<string, long>(req.DocumentSpace).Value;

                    if (docSpaceId == 0)
                        return resp;    //Not found document space

                    var Words = this.PrepareSearchKeyWords(req.SearchWords);

                    string docTable = DocumentsStorageTablesPrefix + "d" + docSpaceId.ToString();
                    var vt = tran.SelectTable<int>(docTable, 3, 0); //Version table Key
                    var dt = tran.SelectTable<int>(docTable, 1, 0); //Document table Key
                    dt.ValuesLazyLoadingIsOn = !req.IncludeDocuments;

                    DBreeze.DataTypes.Row<int, byte[]> docRow = null;
                    Document doc = null;
                    //byte[] btDoc = null;
                    int qOutput = 0;

                    //-----------------------------------------------------------------   ONE/MULTIPLE WORDS SEARCH then one word is supplied, using AND/OR LOGIC

                    #region "Multiple Words"

                    int j = -1;
                    List<byte[]> foundArrays = new List<byte[]>();
                    List<byte[]> oneWordFoundArrays = new List<byte[]>();
                    //WAH2 wh = null;
                    var tbOneWordWAH = tran.SelectTable<int>(DocumentsStorageTablesPrefix + "s" + docSpaceId.ToString(), 2, 0);
                    tbOneWordWAH.ValuesLazyLoadingIsOn = false;

                    resp.UniqueWordsInDataSpace = (int)tbOneWordWAH.Count();

                    bool anyWordFound = false;
                    int totalFoundWords = 0;

                    Dictionary<string, WordInDoc> words = new Dictionary<string, WordInDoc>();
                    int foundOrigin = 1;

                    Dictionary<string, WordInDoc> perWord = new Dictionary<string, WordInDoc>();
                    Dictionary<string, WordInDoc> firstHighOccuranceWord = new Dictionary<string, WordInDoc>();

                    //Currently we ignore these words and do nothing with them
                    List<string> highOccuranceWordParts = new List<string>();

                    foreach (var word in Words.Take(10)) //Maximum 10 words for search
                    {
                        anyWordFound = false;
                        totalFoundWords = 0;
                        perWord = new Dictionary<string, WordInDoc>();

                        foreach (var row1 in tbOneWordWAH.SelectForwardStartsWith<string, byte[]>(word))
                        {
                            anyWordFound = true;
                            totalFoundWords++;

                            if (Words.Count() == 1 && totalFoundWords > req.Quantity)
                            {
                                //In case if only one search word, then we don't need to make any comparation
                                break;
                            }
                            else if (totalFoundWords >= req.MaximalExcludingOccuranceOfTheSearchPattern)  //Found lots of words with such mask inside
                            {
                                //Too much found docs have this word-part inside, better to enhance search
                                if (firstHighOccuranceWord.Count() == 0)
                                {
                                    //Only first HighOccurance word part come to the list. It can be used later in case if all search words are of HighOccurance (then we will visualize only this one)
                                    firstHighOccuranceWord = perWord.ToDictionary(r => r.Key, r => r.Value);
                                }
                                //Clearing repack element
                                perWord.Clear();
                                //Adding word into List of High-Occurance word-part
                                highOccuranceWordParts.Add(word);
                                break;
                            }

                            perWord.Add(row1.Key, new WordInDoc()
                            {
                                 BlockId = row1.Value.Substring(0,4).To_Int32_BigEndian(),
                                 NumberInBlock = row1.Value.Substring(4, 4).To_Int32_BigEndian(),
                                 foundOrigin = foundOrigin
                            });
                        }

                        //Repacking occurances
                        foreach (var pw in perWord)
                            words.Add(pw.Key, pw.Value);

                        foundOrigin++;

                        if (
                            req.SearchLogicType == SearchRequest.eSearchLogicType.AND
                            &&
                            !anyWordFound
                            )
                        {
                            //Non of words found corresponding to AND logic
                            sw.Stop();
                            resp.SearchDurationMs = sw.ElapsedMilliseconds;
                            return resp;
                        }
                    }

                    if (words.Count() == 0)
                    {
                        //In case of multiple search words and each of them of HighOccurance.
                        //We will form result only from the first HighOccurance list

                        //Repacking occurances
                        foreach (var pw in firstHighOccuranceWord.Take(req.Quantity))
                            words.Add(pw.Key, pw.Value);

                        //In this case highOccuranceWordParts must be cleared, because the returning result is very approximate
                        highOccuranceWordParts.Clear();
                    }

                    //Here we must start get data from blocks
                    //Nested table with blocks
                    var tbBlocks = tran.SelectTable<int>(DocumentsStorageTablesPrefix + "s" + docSpaceId.ToString(), 10, 0);
                    tbBlocks.ValuesLazyLoadingIsOn = false;

                    Dictionary<int,byte[]> block=null;
                    byte[] btBlock=null;
                    int currentBlockId = 0;

                    //DBreeze.Diagnostic.SpeedStatistic.StartCounter("LoadBlocks");

                    foreach (var wrd in words.OrderBy(r=>r.Value.BlockId))
                    {
                        if (currentBlockId != wrd.Value.BlockId)
                        {
                            currentBlockId = wrd.Value.BlockId;

                                //DBreeze.Diagnostic.SpeedStatistic.StartCounter("SelectBlocks");
                            btBlock = tbBlocks.Select<int, byte[]>(wrd.Value.BlockId).Value;
                                //DBreeze.Diagnostic.SpeedStatistic.StopCounter("SelectBlocks");
                            btBlock = btBlock.Substring(4, btBlock.Substring(0, 4).To_Int32_BigEndian());
                                //DBreeze.Diagnostic.SpeedStatistic.StartCounter("DecomDeserBlocks");
                            btBlock = btBlock.DecompressGZip();
                            block = btBlock.DeserializeProtobuf<Dictionary<int, byte[]>>();
                                //DBreeze.Diagnostic.SpeedStatistic.StopCounter("DecomDeserBlocks");
                        }

                        wrd.Value.wah = new WAH2(block[wrd.Value.NumberInBlock]);
                    }
                    //DBreeze.Diagnostic.SpeedStatistic.PrintOut("LoadBlocks", true);
                    //DBreeze.Diagnostic.SpeedStatistic.PrintOut("SelectBlocks", true);
                    //DBreeze.Diagnostic.SpeedStatistic.PrintOut("DecomDeserBlocks", true);

                    foundOrigin = 0;

                    foreach (var wrd in words.OrderBy(r => r.Value.foundOrigin))
                    {
                        //Console.WriteLine(wrd.Value.foundOrigin);

                        if (foundOrigin != wrd.Value.foundOrigin)
                        {
                            if (oneWordFoundArrays.Count() > 0)
                            {
                                j++;
                                foundArrays.Add(WAH2.MergeAllUncompressedIntoOne(oneWordFoundArrays));
                                oneWordFoundArrays = new List<byte[]>();
                            }

                            foundOrigin = wrd.Value.foundOrigin;
                        }
                        else
                        {

                        }

                        oneWordFoundArrays.Add(wrd.Value.wah.GetUncompressedByteArray());
                    }

                    //The last
                    if (oneWordFoundArrays.Count() > 0)
                    {
                        j++;
                        foundArrays.Add(WAH2.MergeAllUncompressedIntoOne(oneWordFoundArrays));
                        oneWordFoundArrays = new List<byte[]>();
                    }

                    //////////  final results

                    if (j >= 0)
                    {
                        var q = WAH2.TextSearch_OR_logic(foundArrays, req.Quantity);

                        if (req.SearchLogicType == SearchRequest.eSearchLogicType.AND)
                            q = WAH2.TextSearch_AND_logic(foundArrays).Take(req.Quantity);

                        foreach (var el in q)
                        {
                            //Getting document
                            docRow = dt.Select<int, byte[]>((int)el);
                            if (docRow.Exists)
                            {
                                if (!dmnts.ContainsKey((int)el))
                                {
                                    if (highOccuranceWordParts.Count() > 0)
                                    {
                                        //We got some noisy word-parts of high occurance together with strongly found words.
                                        //We must be sure that these word parts are also inside of returned docs
                                        doc = this.RetrieveDocument(req.IncludeDocumentsContent, true, dt, docRow);
                                        if (doc != null)
                                        {
                                            //Checking doc.Searchables must have all word parts from the occurance in case of AND
                                            if (req.SearchLogicType == SearchRequest.eSearchLogicType.AND)
                                            {
                                                if (String.IsNullOrEmpty(doc.Searchables))
                                                    continue;
                                                if (!highOccuranceWordParts.All(doc.Searchables.ToLower().Contains))
                                                    continue;
                                            }

                                            if (req.IncludeDocuments)
                                            {
                                                if (!req.IncludeDocumentsSearchanbles)
                                                    doc.Searchables = String.Empty;

                                                dmnts.Add((int)el, doc);
                                            }
                                            else
                                            {
                                                dmnts.Add((int)el, null);
                                            }

                                        }
                                        else
                                            continue;
                                    }
                                    else
                                    {
                                        if (req.IncludeDocuments)
                                        {

                                            doc = this.RetrieveDocument(req.IncludeDocumentsContent, req.IncludeDocumentsSearchanbles, dt, docRow);
                                            if (doc == null) //If doc is deleted, while search was in progress and we received its id in the list
                                                continue;

                                            dmnts.Add((int)el, doc);
                                        }
                                        else
                                        {
                                            dmnts.Add((int)el, null);
                                        }

                                    }

                                    qOutput++;
                                }
                            }

                            if (qOutput > req.Quantity)
                                break;

                        }

                    }
                    #endregion

                }//eo using

                //Repacking dmnts into resp
                repack();
                sw.Stop();

                resp.SearchDurationMs = sw.ElapsedMilliseconds;
            }
            catch (Exception ex)
            {
                throw ThrowException("SearchDocumentSpace", ex.ToString());
            }

            return resp;
        }
        private string FormatLastInsertRecord(Dictionary<string, string> row, Dictionary<string, string> fieldFormats, string prefixFormat, string fieldSeparator)
        {
            var sb = new StringBuilder();
            sb.AppendFormat(prefixFormat, "SELECT");

            foreach (var field in row.Take(row.Count - 1))
            {
                var fieldName = field.Key;
                sb.AppendFormat(fieldFormats[fieldName] + fieldSeparator, field.Value);
            }

            var lastField = row.Last();
            var lastFieldName = lastField.Key;
            sb.AppendFormat(fieldFormats[lastFieldName] + new string(' ', fieldSeparator.Length), lastField.Value);

            return sb.ToString();
        }
        private void WriteInsertColumns(Dictionary<string, string> exampleRow, string fieldSeparator, Dictionary<string, int> maxFieldWidths, out Dictionary<string, string> fieldFormats)
        {
            fieldFormats = new Dictionary<string, string>();

            foreach (var field in exampleRow.Take(exampleRow.Count - 1))
            {
                var fieldName = field.Key;
                var fieldFormat = GetFieldFormat(fieldName, maxFieldWidths);

                fieldFormats.Add(fieldName, fieldFormat);

                _tt.Write(fieldFormat + fieldSeparator, fieldName);
            }

            var lastField = exampleRow.Last();
            var lastFieldName = lastField.Key;
            var lastFieldFormat = GetFieldFormat(lastFieldName, maxFieldWidths);

            fieldFormats.Add(lastFieldName, lastFieldFormat);

            _tt.Write(lastFieldFormat + new string(' ', fieldSeparator.Length), lastFieldName);
        }