public static class Settings
{
public static string BASE_MOVIE_URL = @"https://www.imdb.com/title/{0}";
public static string BASE_MOVIE_URL_REVIEWS = @"https://www.imdb.com/title/{0}/reviews?ref_=tt_ov_rt";
public static string API_SEARCH_URL = @"https://www.omdbapi.com/?t={0}&apikey=[APIKEY]";
}
#region HtmlHelpers
//Gets reviews nodes
private static IEnumerable<HtmlNode> GetReviewsNodes(HtmlDocument doc)
{
return doc.DocumentNode.SelectNodes("//div[contains(@class, 'lister-item-content')]");
}
//Gets rating value
private static string GetRating(HtmlNode node)
{
var mainNode = node.SelectSingleNode(".//span[contains(@class, 'rating-other-user-rating')]");
return mainNode != null ? mainNode.ChildNodes[3].InnerHtml : "N/A";
}
//Gets title
private static string GetTitle(HtmlNode node)
{
return node.SelectSingleNode(".//a[contains(@class, 'title')]").InnerHtml;
}
//Gets user and date
private static string[] GetUserNameAndData(HtmlNode node)
{
var mainNode = node.SelectSingleNode(".//div[contains(@class, 'display-name-date')]");
var userNode = mainNode.SelectSingleNode(".//span[contains(@class, 'display-name-link')]");
var user = userNode.SelectSingleNode("a").InnerHtml;
var date = mainNode.SelectSingleNode(".//span[contains(@class, 'review-date')]").InnerHtml;
return new[] {user, date};
}
//Gets title
private static string GetReview(HtmlNode node)
{
return node.SelectSingleNode(".//div[contains(@class, 'text show-more__control')]").InnerHtml;
}
#endregion
public class Rating
{
public string Source { get; set; }
public string Value { get; set; }
}
public class MovieSearchResultModel
{
public string Title { get; set; }
public string Year { get; set; }
public string Rated { get; set; }
public string Released { get; set; }
public string Runtime { get; set; }
public string Genre { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
public string Actors { get; set; }
public string Plot { get; set; }
public string Language { get; set; }
public string Country { get; set; }
public string Awards { get; set; }
public string Poster { get; set; }
public List<Rating> Ratings { get; set; }
public string Metascore { get; set; }
public string imdbRating { get; set; }
public string imdbVotes { get; set; }
public string imdbID { get; set; }
public string Type { get; set; }
public string DVD { get; set; }
public string BoxOffice { get; set; }
public string Production { get; set; }
public string Website { get; set; }
public bool Response { get; set; }
public string PageUrl { get; set; }
public string ReviewsPageUrl { get; set; }
}
public class ReviewsModel
{
public string Title { get; set; }
public string Review { get; set; }
public string Rating { get; set; }
public string User { get; set; }
public string Date { get; set; }
}
private static readonly bool _debugMode = true;
private SentimentAnalyst _sentimentAnalyst;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var modelDataFile = Path.Combine(GetParentDirectory(),
_debugMode
? $@"Movie Reviews\\Movie Reviews\\bin\\{"Debug"}\\Data"
: $@"Movie Reviews\\Movie Reviews\\bin\\{"Release"}\\Data",
"model.zip");
SetColors();
_sentimentAnalyst = new SentimentAnalyst(null, modelDataFile);
_sentimentAnalyst.LoadTrainedModel();
}
#region Helpers
//Load user reviews into the list
private void LoadReviews(IEnumerable<ReviewsModel> reviews)
{
var top = 0;
reviewList.Controls.Clear();
if(reviews==null) return;
foreach (var review in reviews)
{
var data = new Data {Review = review.Review};
var status = _sentimentAnalyst.Predicate(data);
var reviewItem = new ReviewItem
{
Width = 485,
Top = top,
lblTitle = {Text = review.Title},
lblDate = {Text = review.Date},
lblReview = {Text = review.Review},
lblUser = {Text = review.User},
lblRank = {Text = $@"{review.Rating}/10"},
lblStatus = { Text = status.PredictionValue==true?"Positive":"Negative" }
};
reviewItem.lblStatus.BackColor = status.PredictionValue ? Color.Green : Color.DarkRed;
reviewList.Controls.Add(reviewItem);
top += 180;
}
}
//Gets solution path
private static string GetParentDirectory()
{
var directoryInfo = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
if (directoryInfo?.Parent?.Parent != null)
return directoryInfo.Parent.Parent
.FullName;
return string.Empty;
}
//Set colors for the UI elements
private void SetColors()
{
lblInfo1.ForeColor=Color.FromArgb(170,170,170);
lblInfo2.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo3.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo4.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo5.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo6.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo7.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo8.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo9.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo10.ForeColor = Color.FromArgb(170, 170, 170);
lblTitle.ForeColor = Color.FromArgb(170, 170, 170);
lblYear.ForeColor = Color.FromArgb(170, 170, 170);
lblReleased.ForeColor = Color.FromArgb(170, 170, 170);
lblRated.ForeColor = Color.FromArgb(170, 170, 170);
lblRuntime.ForeColor = Color.FromArgb(170, 170, 170);
lblGenre.ForeColor = Color.FromArgb(170, 170, 170);
lblLanguage.ForeColor = Color.FromArgb(170, 170, 170);
lblCountry.ForeColor = Color.FromArgb(170, 170, 170);
imdbRating.ForeColor = Color.FromArgb(170, 170, 170);
lblInfo11.ForeColor = Color.FromArgb(170, 170, 170);
lblPlot.ForeColor = Color.FromArgb(170, 170, 170);
}
#endregion Helpers
private void btnAnalyze_Click(object sender, EventArgs e)
{
if (txtMoviewName.Text == string.Empty)
{
MessageBox.Show(this, "Please type a movie name", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtMoviewName.Focus();
return;
}
var movieInfo= WebHelper.GetMovieGeneralInfo(txtMoviewName.Text);
if (movieInfo.Response)
{
lblTitle.Text = movieInfo.Title;
lblYear.Text = movieInfo.Year;
lblReleased.Text = movieInfo.Released;
lblRated.Text = $@"{movieInfo.Rated}/10";
lblRuntime.Text = movieInfo.Runtime;
lblGenre.Text = movieInfo.Genre;
lblPlot.Text = movieInfo.Plot;
lblLanguage.Text = movieInfo.Language;
lblCountry.Text = movieInfo.Country;
imdbRating.Text = movieInfo.imdbRating;
lblInfo11.Text = $@"{movieInfo.imdbRating}/10";
if (movieInfo.Poster != null)
if(movieInfo.Poster!= "N/A")
moviePicture.Load(movieInfo.Poster);
//Get movie reviews
LoadReviews(WebHelper.GetMovieReviews(movieInfo.ReviewsPageUrl));
}
else
{
lblTitle.Text = string.Empty;
lblYear.Text = string.Empty;
lblReleased.Text = string.Empty;
lblRated.Text = string.Empty;
lblRuntime.Text = string.Empty;
lblGenre.Text = string.Empty;
lblPlot.Text = string.Empty;
lblLanguage.Text = string.Empty;
lblCountry.Text = string.Empty;
imdbRating.Text = string.Empty;
moviePicture.Image = null;
reviewList.Controls.Clear();
MessageBox.Show(this, "Cannot find the movie, please check the name and try again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtMoviewName.Focus();
}
}
var status = _sentimentAnalyst.Predicate(data);
Classification
K-Nearest Neighbor is the one of the well-known and easy machine algorithm ...
K-Nearest Neighbor is the one of the well-known and easy machine algorithm which is very suitable for a lot of real world problems such product recommendation, social media friend recommendation based on interest or social network of person.
Classification
Naive Bayes Algorithm is the algorithm which makes machines to be able to m...
Naive Bayes Algorithm is the algorithm which makes machines to be able to make predictions about the events which they don't have any knowledge about only by looking priors knowledge.
Classification
The super strong wind of the Machine Learning is turning our heads incredib...
The super strong wind of the Machine Learning is turning our heads incredibly, we see an example of the machine learning usage almost every area of the technology. Face detection, voice recognition, text recognition, etc. there are plenty of them, and each of them has a different type of approach to machine learning.
Classification
In the previous article, we finished the first part of our example project,...
In the previous article, we finished the first part of our example project, now we have SentimentAnalyst class which we can use for training data and making a prediction by passing real data to it. Today we are going to work on Part 2 which is going to be a trainer project, the project which is going to handle the training process.