Beispiel #1
0
        public void StartSearchCopy(string source, string target, string outputLoc)
        {
            CSVReader sourceReader = new CSVReader();
            CSVReader targetReader = new CSVReader();

            sourceReader.ParseCSV(source, "}");
            targetReader.ParseCSV(target, "");

            List <List <string> > sourceList = new List <List <string> >();
            List <List <string> > targetList = new List <List <string> >();

            sourceList = sourceReader.GetArrayStorage();
            targetList = targetReader.GetArrayStorage();

            //Tidy up Lists
            List <OrderEntry> orderList             = new List <OrderEntry>();
            List <FinalEntry> finalList             = new List <FinalEntry>();
            List <OrderEntry> orderFailToMatchjList = new List <OrderEntry>();

            int indexID = 0;

            foreach (List <string> entry in sourceList)
            {
                if (indexID == 0)
                {
                    indexID = 1;
                    continue;
                }
                indexID++;

                OrderEntry oEntry = new OrderEntry();
                oEntry.indexID = indexID;
                oEntry.orderID = Int32.Parse(entry[0]);
                oEntry.custID  = Int32.Parse(entry[1]);
                oEntry.shipID  = Int32.Parse(entry[3]);
                oEntry.notes   = entry[4];
                oEntry.SearchNotes();

                orderList.Add(oEntry);
            }

            indexID = 0;
            foreach (List <string> entry in targetList)
            {
                if (indexID == 0)
                {
                    indexID = 1;
                    continue;
                }
                indexID++;
                //TODO:fix these
                FinalEntry oEntry = new FinalEntry();
                oEntry.indexID      = indexID;
                oEntry.APIID        = Int64.Parse(entry[0]);
                oEntry.customerName = entry[4];
                oEntry.siteName     = entry[5];

                finalList.Add(oEntry);
            }

            //Start finding matches
            int[] matchScore             = new int[finalList.Count];
            int   successfulMatchCounter = 0;
            int   processedAmount        = 0;

            foreach (OrderEntry oE in orderList)
            {
                foreach (string name in oE.sitenames)
                {
                    if (!name.Contains("CORIOLIS"))//theres a random scope inside a Service At: field
                    {
                        if (Properties.Settings.Default.UseLevensteins)
                        {
                            //assign scores per name in comments
                            int matchCounter = 0;
                            foreach (FinalEntry fE in finalList)
                            {
                                matchScore[matchCounter] = ComputeDistance(name, fE.siteName);
                                matchCounter++;
                            }
                            //Find lowest scores and assign them as probable matches
                            int bestScore = matchScore.Min();
                            int counter   = 0;
                            foreach (int value in matchScore)
                            {
                                if (value == bestScore)
                                {
                                    finalList[counter].proposedSiteIDs.Add(oE.shipID);
                                    finalList[counter].proposedeNameMatch.Add(name);
                                    finalList[counter].proposedWorkOrder.Add(oE.orderID);
                                }
                                counter++;
                            }
                        }
                        else
                        {
                            int counter = 0;
                            foreach (FinalEntry fE in finalList)
                            {
                                bool match = CanContain(name, fE.siteName);
                                if (match)
                                {
                                    finalList[counter].proposedSiteIDs.Add(oE.shipID);
                                    finalList[counter].proposedeNameMatch.Add(name);
                                    finalList[counter].proposedWorkOrder.Add(oE.orderID);

                                    orderList[oE.indexID].matched = true;
                                    successfulMatchCounter++;
                                }

                                counter++;
                            }
                        }
                    }
                }
                processedAmount++;
            }
            //Write Results
            string filePath  = outputLoc + "/Results.csv";
            string delimiter = ",";

            using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
            {
                if (fs.CanWrite)
                {
                    string csvInput = "";
                    csvInput = "Index_ID" + delimiter + "API_ID" + delimiter + "Customer" + delimiter + "Site_Names" + delimiter + "Prop_Ship_ID1" +
                               delimiter + "Prop_Name1" + delimiter + "Prop_Work_Order1" + delimiter + "Match Rate: " + successfulMatchCounter + "/" + processedAmount;
                    fs.Write(Encoding.ASCII.GetBytes(csvInput), 0, ASCIIEncoding.ASCII.GetByteCount(csvInput));
                    byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
                    fs.Write(newline, 0, newline.Length);
                    foreach (FinalEntry fE in finalList)
                    {
                        csvInput = fE.indexID + delimiter + fE.APIID + delimiter + fE.customerName + delimiter + fE.siteName;
                        fs.Write(Encoding.ASCII.GetBytes(csvInput), 0, ASCIIEncoding.ASCII.GetByteCount(csvInput));

                        int count = 0;
                        foreach (int siteID in fE.proposedSiteIDs)
                        {
                            csvInput = delimiter + fE.proposedSiteIDs[count] + delimiter + fE.proposedeNameMatch[count] + delimiter + fE.proposedWorkOrder[count];
                            fs.Write(Encoding.ASCII.GetBytes(csvInput), 0, ASCIIEncoding.ASCII.GetByteCount(csvInput));
                            count++;
                        }

                        fs.Write(newline, 0, newline.Length);
                    }
                }
                fs.Flush();
                fs.Close();
            }
            filePath = outputLoc + "/FailedMatches.csv";


            using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
            {
                if (fs.CanWrite)
                {
                    string csvInput = "";
                    csvInput = "Index_ID" + delimiter + "Order_ID" + delimiter + "Ship_ID" + delimiter + "Site_Names" + delimiter + "Notes";
                    fs.Write(Encoding.ASCII.GetBytes(csvInput), 0, ASCIIEncoding.ASCII.GetByteCount(csvInput));
                    byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
                    fs.Write(newline, 0, newline.Length);

                    foreach (OrderEntry oE in orderList)
                    {
                        if (oE.matched == false)
                        {
                            csvInput = oE.indexID + delimiter + oE.orderID + delimiter + oE.shipID + delimiter + oE.sitenames[0] + delimiter + oE.notes;
                            fs.Write(Encoding.ASCII.GetBytes(csvInput), 0, ASCIIEncoding.ASCII.GetByteCount(csvInput));
                            fs.Write(newline, 0, newline.Length);
                        }
                    }
                }
                fs.Flush();
                fs.Close();
            }
        }