Ejemplo n.º 1
0
        public static int Main(string[] args)
        {
            Console.WriteLine("CrossWord ver. {0} ", "1.0");

            string inputFile, outputFile, puzzle, dictionaryFile;

            if (!ParseInput(args, out inputFile, out outputFile, out puzzle, out dictionaryFile))
            {
                return(1);
            }
            ICrossBoard board;

            try
            {
                board = CrossBoardCreator.CreateFromFile(inputFile);
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Cannot load crossword layout from file {0}.", inputFile), e);
                return(2);
            }
            Dictionary dictionary;

            try
            {
                dictionary = new Dictionary(dictionaryFile, board.MaxWordLength);
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Cannot load dictionary from file {0}.", dictionaryFile), e);
                return(3);
            }
            ICrossBoard resultBoard;

            try
            {
                resultBoard = puzzle != null
                    ? GenerateFirstCrossWord(board, dictionary, puzzle)
                    : GenerateFirstCrossWord(board, dictionary);
            }
            catch (Exception e)
            {
                Console.WriteLine("Generating crossword has failed.", e);
                return(4);
            }
            if (resultBoard == null)
            {
                Console.WriteLine(string.Format("No solution has been found."));
                return(5);
            }
            try
            {
                SaveResultToFile(outputFile, resultBoard, dictionary);
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Saving result crossword to file {0} has failed.", outputFile), e);
                return(6);
            }
            return(0);
        }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            Console.WriteLine("CrossWord ver. {0} ", "1.0");

            string inputFile, outputFile, puzzle, dictionaryFile;

            if (!ParseInput(args, out inputFile, out outputFile, out puzzle, out dictionaryFile))
            {
                return(1);
            }
            ICrossBoard board;

            try
            {
                if (inputFile.StartsWith("http"))
                {
                    board = CrossBoardCreator.CreateFromUrl(inputFile);
                }
                else
                {
                    board = CrossBoardCreator.CreateFromFile(inputFile);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Cannot load crossword layout from file {0}.", inputFile), e);
                return(2);
            }

            ICrossDictionary dictionary;

            try
            {
                if (dictionaryFile.Equals("database"))
                {
                    dictionary = new DatabaseDictionary("server=localhost;port=3306;database=dictionary;user=user;password=password;charset=utf8;", board.MaxWordLength);
                }
                else
                {
                    dictionary = new Dictionary(dictionaryFile, board.MaxWordLength);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Cannot load dictionary from file {0}.", dictionaryFile), e);
                return(3);
            }

            if (outputFile.Equals("signalr"))
            {
                // generate and send to signalr hub
                // var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(20));
                var  tokenSource = new CancellationTokenSource();
                Task workerTask  = Task.Run(
                    async() =>
                {
                    CancellationToken token = tokenSource.Token;
                    try
                    {
                        await Generator.GenerateCrosswordsAsync(board, dictionary, puzzle, token);
                    }
                    catch (OperationCanceledException)
                    {
                        Console.WriteLine("Cancelled @ {0}", DateTime.Now);
                    }
                });

                // wait until the task is done
                Task.WaitAll(workerTask);

                // or wait until the user presses a key
                // Console.WriteLine("Press Enter to Exit ...");
                // Console.ReadLine();
                // tokenSource.Cancel();
            }
            else if (outputFile.Equals("database"))
            {
                var dbContextFactory = new DesignTimeDbContextFactory();
                using (var db = dbContextFactory.CreateDbContext("server=localhost;database=dictionary;user=user;password=password;charset=utf8;", Log.Logger)) // null instead of Log.Logger enables debugging
                {
                    // setup database
                    // You would either call EnsureCreated() or Migrate().
                    // EnsureCreated() is an alternative that completely skips the migrations pipeline and just creates a database that matches you current model.
                    // It's good for unit testing or very early prototyping, when you are happy just to delete and re-create the database when the model changes.
                    // db.Database.EnsureDeleted();
                    // db.Database.EnsureCreated();

                    // Note! Therefore don't use EnsureDeleted() and EnsureCreated() but Migrate();
                    db.Database.Migrate();

                    // set admin user
                    var user = new User()
                    {
                        FirstName = "",
                        LastName  = "Norwegian Synonyms json",
                        UserName  = "******"
                    };

                    // check if user already exists
                    var existingUser = db.DictionaryUsers.Where(u => u.FirstName == user.FirstName).FirstOrDefault();
                    if (existingUser != null)
                    {
                        user = existingUser;
                    }
                    else
                    {
                        db.DictionaryUsers.Add(user);
                        db.SaveChanges();
                    }

                    // disable tracking to speed things up
                    // note that this doesn't load the virtual properties, but loads the object ids after a save
                    db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

                    // this works when using the same user for all words.
                    db.ChangeTracker.AutoDetectChangesEnabled = false;

                    bool isDebugging = false;
#if DEBUG
                    isDebugging = true;
#endif

                    var source = "norwegian-synonyms.json";
                    if (Path.GetExtension(dictionaryFile).ToLower().Equals(".json"))
                    {
                        // read json files
                        using (StreamReader r = new StreamReader(dictionaryFile))
                        {
                            var json = r.ReadToEnd();
                            var jobj = JObject.Parse(json);

                            var totalCount = jobj.Properties().Count();
                            int count      = 0;
                            foreach (var item in jobj.Properties())
                            {
                                count++;

                                var wordText     = item.Name;
                                var relatedArray = item.Values().Select(a => a.Value <string>());

                                WordDatabaseService.AddToDatabase(db, source, user, wordText, relatedArray);

                                if (isDebugging)
                                {
                                    // in debug mode the Console.Write \r isn't shown in the output console
                                    Console.WriteLine("[{0}] / [{1}]", count, totalCount);
                                }
                                else
                                {
                                    Console.Write("\r[{0}] / [{1}]", count, totalCount);
                                }
                            }
                            Console.WriteLine("Done!");
                        }
                    }
                }
            }
            else
            {
                ICrossBoard resultBoard;
                try
                {
                    resultBoard = puzzle != null
                        ? GenerateFirstCrossWord(board, dictionary, puzzle)
                        : GenerateFirstCrossWord(board, dictionary);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Generating crossword has failed.", e);
                    return(4);
                }
                if (resultBoard == null)
                {
                    Console.WriteLine(string.Format("No solution has been found."));
                    return(5);
                }
                try
                {
                    SaveResultToFile(outputFile, resultBoard, dictionary);
                }
                catch (Exception e)
                {
                    Console.WriteLine(string.Format("Saving result crossword to file {0} has failed.", outputFile), e);
                    return(6);
                }
            }
            return(0);
        }