Esempio n. 1
0
 public Show(object tag)
 {
     InitializeComponent();
     UnderImage.Background = new ImageBrush()
     {
         ImageSource = new Image()
         {
             Source = new BitmapImage(
                 new Uri("pack://application:,,,/Чтение;component/Resources/over_place.png"))
         }.Source
     };
     BackButton.Content = new Image()
     {
         Source = new BitmapImage(new Uri("pack://application:,,,/Чтение;component/Resources/Arrows/back.png"))
     };
     TempPlace = new ImageBrush()
     {
         ImageSource = new Image()
         {
             Source = new BitmapImage(
                 new Uri("pack://application:,,,/Чтение;component/Resources/place.jpg"))
         }.Source
     };
     ImagePlace.Background = TempPlace;
     CurrentTable          = new WordsTable();
     ForWords.Child        = CurrentTable;
     InputLanguageManager.SetInputLanguage(WordInput, CultureInfo.CreateSpecificCulture("ru"));
     Loaded += (sender, e) =>
               Update((int)tag);
 }
 public BayesianFilter(WordsTable table)
 {
     this.table         = table;
     this.probabilities = new Dictionary <string, Word>();
     foreach (var word in table.Words.Values)
     {
         probabilities.Add(word.Name, word);
     }
 }
Esempio n. 3
0
        static void Main(string[] args)
        {
            ConfigureEnvironmentVariables();

            var settings = new FolderSettings();

            Configuration.Bind(settings);
            IFilesReader spamReader    = new FolderFilesReader(settings.SpamFolder);
            IFilesReader notSpamReader = new FolderFilesReader(settings.NotSpamFolder);
            Folder       spam          = new Folder()
            {
                Files = spamReader.GetFiles()
            };
            Folder notSpam = new Folder()
            {
                Files = notSpamReader.GetFiles()
            };
            WordsTable words = new WordsTable();

            words.PopulateSpam(spam);
            words.PopulateNotSpam(notSpam);
            BayesianFilter filter = new BayesianFilter(words);

            filter.RecalculateProb();
            var biggest = filter.findBiggest();
            var spamas  = new List <string>();

            //spamas.Add("ka");
            spamas.Add("a");
            //spamas.Add("off");
            var isSpam = filter.CheckIfSpam(spamas);

            Console.WriteLine("Tekstas: ");
            foreach (var word in spamas)
            {
                Console.Write(word + " ");
            }
            Console.WriteLine("\nTikimybe: " + isSpam);
            Console.WriteLine("Ar spamas:" + (isSpam > 0.5));
            var i = 2;
        }
Esempio n. 4
0
        private void SearchBox_TextChanged(object sender, EventArgs e)
        {
            var str = searchBox.Text;

            if (!isListing)
            {
                str            = str.Replace("\n", String.Empty);
                str            = str.Replace("\r", String.Empty);
                str            = str.Replace("\t", String.Empty);
                searchBox.Text = str;
                BringToFront();
                Show();
                textBoxTimer.Stop();
                textBoxTimer.Start();
            }
            else
            {
                str = str.Replace("\n", String.Empty);
                str = str.Replace("\r", String.Empty);
                str = str.Replace("\t", String.Empty);

                var          jsonOperator = new JsonOperator(FilePaths.PermanentFiles.UserDictionary);
                List <Entry> entries      = jsonOperator.LoadFile();
                var          closeWords   = new SortedDictionary <int, Entry>();
                foreach (var entry in entries)
                {
                    var word     = entry.Word;
                    var distance = WordsTable.LevenshteinDistance(word, str);
                    if (distance < 3 && word.Contains(str))
                    {
                        closeWords.Add(distance, entry);
                    }
                }

                // TODO: burayı biraz daha geliştir. aranan kelimeyi bulsada listelememe ihtimali var
                wordsTable.LoadSearchedWord(closeWords);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <param name="e"></param>
        /// <param name="payload"></param>
        private void ProcessRequest(string line, Exception e, object payload)
        {
            // Make sure socket hasn't closed or thrown an exception
            if (ReferenceEquals(line, null) && ReferenceEquals(e, null) || !ReferenceEquals(e, null))
            {
                return;
            }

            // The StringSocket to talk to this client.
            StringSocket ss = (StringSocket)payload;

            // Lowercase the line so we can process requests case-insensitively
            line = line.ToLower();

            // Remove whitespace from the end of the string that browsers seem to tack on and I can't seem to do a match for
            line = line.Trim();

            // Send valid HTTP connection message
            ss.BeginSend("HTTP/1.1 200 OK\r\n", (ee, pp) => { }, null);
            ss.BeginSend("Connection: close\r\n", (ee, pp) => { }, null);
            ss.BeginSend("Content-Type: text/html; charset=UTF-8\r\n", (ee, pp) => { }, null);
            ss.BeginSend("\r\n", (ee, pp) => { }, null);

            // Upon receiving "GET /players HTTP/1.1" the server will send back an HTML web page containing a table of information 
            // reporting all games played. The table should have one row for each player in the database and four columns. 
            // Each row should consist of the player's name, the number of games won by the player, the number of games lost by the player, 
            // and the number of games tied by the player. 
            if (line == "get /players http/1.1" || line == "get /players/ http/1.1")
            {
                PlayersTable pt = new PlayersTable();

                // Connect to the DB
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    try
                    {
                        // Open a connection
                        conn.Open();

                        // Create a command
                        MySqlCommand command = conn.CreateCommand();
                        command.CommandText = "SELECT * from Players";

                        // Execute the command and cycle through the DataReader object
                        using (MySqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                pt.name.Add(reader["Name"].ToString());
                                pt.won.Add((int)reader["Won"]);
                                pt.tied.Add((int)reader["Tied"]);
                                pt.lost.Add((int)reader["Lost"]);
                            }
                        }
                    }
                    catch (Exception f)
                    {
                        Console.WriteLine(f.Message);
                    }
                }
                // Start sending data back to web page as HTML table
                ss.BeginSend("All Games Played: " +
                            "<table width=\"100%\" border=\"2\">" +
                            "<tr>" +
                            "<td>Player Name</td>" +
                            "<td>Games Won</td>" +
                            "<td>Games Tied</td>" +
                            "<td>Games Lost</td>" +
                            "</tr>" +

                            "<tr>", (ee, pp) => { }, null);

                for (int i = 0; i < pt.name.Count; i++)
                {
                    ss.BeginSend("<tr>" +
                        string.Format("<td><a href=\"/games?player={0}\">{0}</a></td>", pt.name[i]) +
                        string.Format("<td>{0}</td>", pt.won[i]) +
                        string.Format("<td>{0}</td>", pt.tied[i]) +
                        string.Format("<td>{0}</td>", pt.lost[i]) +
                        "</tr>", (ee, pp) => { }, null);
                }
                // Link to all Players and all Games
                ss.BeginSend("</table>" +
                    "<p>" +
                    "<a href=\"/players\">List of all Players</a>" +
                    "</p>" +
                    "<p>" +
                    "<a href=\"/games\">List of all Games</a>" +
                    "</p>", (ee, pp) => { }, null); 
            }

            // Upon receiving "GET /games?player=Joe HTTP/1.1" the server will send back an HTML web page containing 
            // a table of information reporting all games by the player "Joe". There should be one row for each 
            // game played by the player named in the line of text (e.g., "Joe" in the example above) and six columns. 
            // Each row should consist of a number that uniquely identifies the game (see the next paragraph for how that number will be used), 
            // the date and time when the game was played, the name of the opponent, the score for the named player, and the score for the opponent.
            else if (Regex.IsMatch(line, @"^get /games\?player=[a-z0-9_]{1,20} http/1\.1")) // Player names are capped at 20 max (see Player class)
            {
                GamesTable gt = new GamesTable();
                bool player1 = false;
              
                // Extract the player's name from the line
                string playerName = line.Substring(18, line.Length - 27);

                // Connect to the DB
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    try
                    {
                        // Open a connection
                        conn.Open();

                        // Create a command
                        MySqlCommand command = conn.CreateCommand();

                        // Check to see if player exists in Player1 field or Player2 field
                        command.CommandText = "SELECT Player1 from Games WHERE Player1 = '" + playerName + "'";
                        using (MySqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                player1 = true;
                            }
                        }

                        // If player exists in Player1 field, get opponents data
                        if (player1)
                        {
                            command.CommandText = "SELECT GameID, DateTime, Player2, Player1Score, Player2Score from Games WHERE Player1 = '" + playerName + "'";
                            using (MySqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    gt.gameID.Add((int)reader["GameID"]);
                                    gt.dateTime.Add(reader["DateTime"].ToString());
                                    gt.player2.Add(reader["Player2"].ToString());
                                    gt.player1Score.Add((int)reader["Player1Score"]);
                                    gt.player2Score.Add((int)reader["Player2Score"]);
                                }
                            }
                        }
                        // Player must be in Player2 field
                        else
                        {
                            command.CommandText = "SELECT GameID, DateTime, Player1, Player1Score, Player2Score from Games WHERE Player2 = '" + playerName + "'";
                            using (MySqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    gt.gameID.Add((int)reader["GameID"]);
                                    gt.dateTime.Add(reader["DateTime"].ToString());
                                    gt.player1.Add(reader["Player1"].ToString());
                                    gt.player1Score.Add((int)reader["Player1Score"]);
                                    gt.player2Score.Add((int)reader["Player2Score"]);
                                }
                            }
                        }
                    }
                    catch (Exception f)
                    {
                        Console.WriteLine(f.Message);
                    }
                }

                ss.BeginSend(string.Format("<p>Player {0} Stats:</p>", playerName) +
                        "<table width=\"100%\" border=\"2\">" +
                            "<tr>"+
                            "<td>Game ID</td>" +
                            "<td>Date & Time</td>" +
                            "<td>Opponent Name</td>" +
                            "<td>Your Score</td>" +
                            "<td>Opponent Score</td>" +
                            "</tr>" , (ee, pp) => { }, null);

                // If the player is in Player1 field, post data on Player2 as opponent
                if (player1)
                {
                    for (int i = 0; i < gt.gameID.Count; i++)
                    {
                        ss.BeginSend("<tr>" +
                            string.Format("<td><a href=\"/game?id={0}\">{0}</a></td>", gt.gameID[i]) +
                            string.Format("<td>{0}</td>", gt.dateTime[i]) +
                            string.Format("<td><a href=\"/games?player={0}\">{0}</a></td>", gt.player2[i]) +
                            string.Format("<td>{0}</td>", gt.player1Score[i]) +
                            string.Format("<td>{0}</td>", gt.player2Score[i]) +
                            "</tr>", (ee, pp) => { }, null);
                    }
                }
                // Player is in Player2 field, post data on Player1 as opponent
                else
                {
                    for (int i = 0; i < gt.gameID.Count; i++)
                    {
                        ss.BeginSend("<tr>" +
                            string.Format("<td><a href=\"/game?id={0}\">{0}</a></td>", gt.gameID[i]) +
                            string.Format("<td>{0}</td>", gt.dateTime[i]) +
                            string.Format("<td><a href=\"/games?player={0}\">{0}</a></td>", gt.player1[i]) +
                            string.Format("<td>{0}</td>", gt.player2Score[i]) +
                            string.Format("<td>{0}</td>", gt.player1Score[i]) +
                            "</tr>", (ee, pp) => { }, null);
                    }
                }
                // Link to all Players and all Games
                ss.BeginSend("</table>" +
                    "<p>" +
                    "<a href=\"/players\">List of all Players</a>" +
                    "</p>" +
                    "<p>" +
                    "<a href=\"/games\">List of all Games</a>" +
                    "</p>", (ee, pp) => { }, null);  

            }
            // Upon receiving "GET /game?id=35 HTTP/1.1" the server should send back an HTML page containing information 
            // about the specified game (e.g., 35 in this example). The page should contain the names and scores of 
            // the two players involved, the date and time when the game was played, a 4x4 table containing the Boggle board that was used, 
            // the time limit that was used for the game, and the five-part word summary.
            else if (Regex.IsMatch(line, @"get /game\?id=[1-9][0-9]{0,11} http/1\.1")) // Put a cap on game numbers (11 integers long)
            {                
                GamesTable gt = new GamesTable();
                WordsTable wt = new WordsTable();

                // Extract the gameID from the line
                string gameIDAsString = line.Substring(13, line.Length-22);
                int gameID;
                int.TryParse(gameIDAsString, out gameID); // This cannot fail because of the Regex above (0-11 ints long)

                // Connect to the DB
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    try
                    {
                        // Open a connection
                        conn.Open();

                        // Create a command
                        MySqlCommand command = conn.CreateCommand();
                        command.CommandText = "SELECT Player1, Player2, Player1Score, Player2Score, DateTime, Board, TimeLimit, Word, Status " +
                                              "FROM Games, Words WHERE Games.GameID = " + gameID + " AND Words.Game = " + gameID;

                        gt.gameID.Add(gameID);

                        // Execute the command and cycle through the DataReader object
                        using (MySqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                gt.player1.Add(reader["Player1"].ToString());
                                gt.player2.Add(reader["Player2"].ToString());
                                gt.player1Score.Add((int)reader["Player1Score"]);
                                gt.player2Score.Add((int)reader["Player2Score"]);
                                gt.dateTime.Add(reader["DateTime"].ToString());
                                gt.board = reader["Board"].ToString();
                                gt.timeLimit = (int)reader["TimeLimit"];
                                wt.word.Add(reader["Word"].ToString());
                                wt.status.Add(reader["Status"].ToString());
                            }
                        }
                    }
                    catch (Exception f)
                    {
                        Console.WriteLine(f.Message);
                    }
                }

                // Send game stats
                ss.BeginSend(string.Format("<p>Game {0} Stats:</p>", gameID) +
                        "<table width=\"100%\" border=\"2\">" +
                            "<tr>" +
                            "<td>Player 1 Name</td>" +
                            "<td>Player 2 Name</td>" +
                            "<td>Player 1 Score</td>" +
                            "<td>Player 2 Score</td>" +
                            "<td>Date & Time</td>" +
                            "<td>Time Limit</td>" +
                            "</tr>", (ee, pp) => { }, null);

                ss.BeginSend("<tr>" +
                        string.Format("<td><a href=\"/games?player={0}\">{0}</a></td>", gt.player1[0]) +
                        string.Format("<td><a href=\"/games?player={0}\">{0}</a></td>", gt.player2[0]) + 
                        string.Format("<td>{0}</td>", gt.player1Score[0]) +
                        string.Format("<td>{0}</td>", gt.player2Score[0]) +
                        string.Format("<td>{0}</td>", gt.dateTime[0]) +
                        string.Format("<td>{0}</td>", gt.timeLimit) +
                        "</tr>" +
                        "</table>", (ee, pp) => { }, null);


                // Send board configuration as a 4x4 table
                ss.BeginSend("<p>Board Configuration:</p>" +
                        "<table width=\"10%\" border=\"2\">", (ee, pp) => { }, null);
                // first row
                string board1 = gt.board.Substring(0, 4);
                ss.BeginSend("<tr>", (ee, pp) => { }, null);
                for(int i = 0; i < 4; i++) 
                {
                    ss.BeginSend(string.Format("<td>{0}</td>", board1[i]), (ee, pp) => { }, null);
                }
                ss.BeginSend("</tr>", (ee, pp) => { }, null);
                
                // second row
                string board2 = gt.board.Substring(4, 4);
                ss.BeginSend("<tr>", (ee, pp) => { }, null);
                for (int i = 0; i < 4; i++)
                {
                    ss.BeginSend(string.Format("<td>{0}</td>", board2[i]), (ee, pp) => { }, null);
                }
                ss.BeginSend("</tr>", (ee, pp) => { }, null);

                // third row
                string board3 = gt.board.Substring(8, 4);
                ss.BeginSend("<tr>", (ee, pp) => { }, null);
                for (int i = 0; i < 4; i++)
                {
                    ss.BeginSend(string.Format("<td>{0}</td>", board3[i]), (ee, pp) => { }, null);
                }
                ss.BeginSend("</tr>", (ee, pp) => { }, null);

                // fourth row
                string board4 = gt.board.Substring(12, 4);
                ss.BeginSend("<tr>", (ee, pp) => { }, null);
                for (int i = 0; i < 4; i++)
                {
                    ss.BeginSend(string.Format("<td>{0}</td>", board4[i]), (ee, pp) => { }, null);
                }
                ss.BeginSend("</tr>", (ee, pp) => { }, null); 
                ss.BeginSend("</table>", (ee, pp) => { }, null);

                // Send word summary
                ss.BeginSend("<p>Word Summary</p>" +
                        "<table width=\"15%\" border=\"2\">", (ee, pp) => { }, null);

                for (int i = 0; i < wt.word.Count; i++)
                {
                    ss.BeginSend("<tr>"+
                            string.Format("<td>{0}</td>", wt.word[i]) +
                            string.Format("<td>{0}</td>", wt.status[i])
                            , (ee, pp) => { }, null);
                }
                // Link to all Players and all Games
                ss.BeginSend("</table>" +
                    "<p>" +
                    "<a href=\"/players\">List of all Players</a>" +
                    "</p>"+
                    "<p>" +
                    "<a href=\"/games\">List of all Games</a>" +
                    "</p>", (ee, pp) => { }, null);                  
            }
            else if (line == "get /games http/1.1" || line == "get /games/ http/1.1") 
            {
                GamesTable gt = new GamesTable();

                // Connect to the DB
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    try
                    {
                        // Open a connection
                        conn.Open();

                        // Create a command
                        MySqlCommand command = conn.CreateCommand();
                        command.CommandText = "SELECT GameID, Player1, Player2, DateTime FROM Games";

                        // Execute the command and cycle through the DataReader object
                        using (MySqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                gt.gameID.Add((int)reader["GameID"]);
                                gt.player1.Add(reader["Player1"].ToString());
                                gt.player2.Add(reader["Player2"].ToString());
                                gt.dateTime.Add(reader["DateTime"].ToString());
                            }
                        }
                    }
                    catch (Exception f)
                    {
                        Console.WriteLine(f.Message);
                    }
                }

                // Send game stats
                ss.BeginSend("<p>List of all Games in Database:</p>" +
                        "<table width=\"50%\" border=\"2\">" +
                            "<tr>" +
                            "<td>Game ID</td>" +
                            "<td>Player 1 Name</td>" +
                            "<td>Player 2 Name</td>" +
                            "<td>Date & Time</td>" +
                            "</tr>", (ee, pp) => { }, null);

                for (int i = 0; i < gt.player1.Count; i++)
                {
                    ss.BeginSend("<tr>" +
                        string.Format("<td><a href=\"/game?id={0}\">{0}</a></td>", gt.gameID[i]) +
                        string.Format("<td><a href=\"/games?player={0}\">{0}</a></td>", gt.player1[i]) +
                        string.Format("<td><a href=\"/games?player={0}\">{0}</a></td>", gt.player2[i]) +
                        string.Format("<td>{0}</td>", gt.dateTime[i]) +
                        "</tr>", (ee, pp) => { }, null);
                }
                // Link to all Players and all Games
                ss.BeginSend("</table>" +
                    "<p>" +
                    "<a href=\"/players\">List of all Players</a>" +
                    "</p>", (ee, pp) => { }, null); 
            }
            // If the first line of text sent by the browser to the server is anything else, the server should send back an HTML page 
            // containing an error message. The error message should be meaningful and contain a summary of valid options.
            else
            {
                // The line below this one is a generic placeholder for what needs to be sent
                ss.BeginSend(string.Format("<p>ERROR, you sent: [{0}]</p><p>The only valid commands are /players, /games?player=[player name], and /game?id=[integer]</p>", line), (ee, pp) => { }, null);
            }

            // Close the StringSocket
            ss.Close();
        }