0
$\begingroup$

Let’s imagine we want to create a simple Sentiment Analysis model using Machine Learning not Deep Learning algorithms, so we need to have a set of handcrafted features for this classification problem.

Let's say we have 2 features (F1, F2) for each sentence and a target class consisting of 0 and 1 as positive and negative. So we have a bunch of sentences in a dataset like this:

enter image description here

Well, classification algorithms like SVM, LR, DT, and …. can be trained from the training set and learn the sentiment of each sentence from their features. They can also test themselves using the test set. Now the model is ready to predict the sentiment of any sentence out of the dataset. Till now, there is no problem and everything is clear to me.

My problem is when we want to give a new sentence to the model form outside the dataset. Obviously, when we give a sentence to the model, we don’t give any feature to the model. So here is my question, how the model can predict the sentiment of the new sentence when it doesn’t know how to calculate each of the features?

Should we define what is F1, and F2 in the model first? Should we define a function and for any new input sentence, call the function to calculate the proper value for each feature and then teach the model that the function outputs are equal to F1 and F2 in the dataset?

To make a long question shorter, let me give a clear instance. My new sentence is "She was overjoyed when saw my cat". I want to give this sentence to the model and obviously expect to see a predicted positive sentiment. This sentence is not in my dataset so there is no feature for it and the model has no idea about F1 and F2. On the other hand, my model learned to determine the sentiment of each sentence using F1 and F2. So back to my question, when I don't give new sentence features (F1, F2) to my model and don't specify any procedure to calculate the features for new inputs, how the model can predict the sentiment of my new sentences?


Updated:

As an example of a classification problem, I mentioned the article " A Pattern-Based Approach for Sarcasm Detection on Twitter," Click here! where the authors proposed four sets of features that cover the different types of sarcasm. They used those to classify tweets as sarcastic and non-sarcastic. These features are sentiment-related features, Punctuation-related features, Syntactic and semantic features, and Pattern features. For instance, in sentiment-related features, they checked whether there is a contrast between the different components. By contrast, they mean the coexistence of a negative component and a positive one within the same tweet. Once the features are extracted, they ran the classification using classifiers like (SVM). So till now, everything is clear, with those features they trained their model and then test it with the test set. The question is when they wanna check the classification for a new sentence, say x for instance, obviously, we should calculate all four sets of features for the new input x. Assume that G(x) is a function that gets a new sentence x and returns four sets of features for it (F1,F2,F3, F4), where (F1 = Sentiment-relatedfeatures, F2 = Punctuation-related features, F3 = Syntactic and semantic features, F4 = Pattern features) How should I show the model to use these features (F1,...,F4) to predict the class for this new sentence (x)?

We have such a code: Ypred = model.predict(x, F1,F2,F3,F4); how the model should know to use F1,...F4 features in the same way as its train or test phase?

$\endgroup$

1 Answer 1

2
$\begingroup$

when I don't give new sentence features (F1, F2) to my model and don't specify any procedure to calculate the features for new inputs, how the model can predict the sentiment of my new sentences?

It just cannot.

Any supervised ML model can be seen as a function of some set of features $x$, i.e. $\hat{y} = f(x)$. Since you describe the actual input (the sentence) with two features $F_1$ and $F_2$, and you don't provide them to the model you can't predict on new sentences.

Conversely, if you try to learn another model, say $\hat F = g(s)$, that maps a sentence $s$ to the features, you need to figure out how to encode the sentence to a fixed-length representation. But if you can already do it then there is no point at learning the model $g$ at all, since you can directly learn $f$ instead also dropping the two features.

I think your problem setup is a bit ill posed, you should encode the sentences either with an n-gram approach or with a vocabulary lookup to a dense embedding (instead of one-hot encodings of words.), and then train directly the model to predict the sentiment given the class variable, so skipping the two features.

Update: Assuming you can can compute a set of features $F$ for each sentence, like done in the paper you linked, you need to train a multi-class classifier (e.g. a decision tree, random forest, SVM, or even neural-network) on six classes from $1$ (highly non-sarcastic) to $6$ (highly sarcastic): otherwise you can define just two classes, sarcastic vs non-sarcastic. The way you train the classifier is by pairing features $F$ and targets $y$ (i.e. the desired class label.) So you build a training dataset $\mathcal{D}_{train}=\{F_i, y_i\}_i^N$ for each training sentence $s_i$ ($N$ in total), and a validation dataset (that you can obtain from $\mathcal{D}_{train}$ by taking a random $20\%$ for example). Your train the model, like model.fit(F, y), and then evaluate the performance on a test set $\mathcal{D}_{test} = \{F_j\}_j^M$ for each new sentence $s_j$. Note that for the new sentences you don't have a class label (you want to predict that) but you have only to compute their features $F$. To predict the new classes you do something like: y_pred = model.predict(F_test), where F_test are the features of the test sentences, i.e. $\mathcal{D}_{test}$. Finally, for building/training and evaluating a classifier have a look at scikit-learn (DTs, RF, SVM) or Keras (for NNs).

Basically, to both train and test the model you only need to provide the computed features $F$ as input, and not also the sentence: such kind of models cannot learn directly from words, and so from sentences.

$\endgroup$
6
  • $\begingroup$ thank you for your response. So you believe It’s better to use n-gram or vocabulary lookup, that’s a good idea. But what if I want to use those features only? Let’s say I add a function to my program to calculate F1 and F2 for any new input. How should I tell the model that the output of the function is equal to F1 and F2. ? $\endgroup$
    – Z Bokaee
    May 12, 2023 at 14:18
  • $\begingroup$ The problem is that I read lots of articles on classification problem like sentiment analysis and sarcasm detection and in most of them authors think about different features and try to teach their models using those features. However, I have no idea how I should predict the classification labels for new sentences. I would appreciate it if you can help me. $\endgroup$
    – Z Bokaee
    May 12, 2023 at 14:28
  • $\begingroup$ If you're able to compute F1 and F2 then you just need to setup a classifier where each sentiment is a class, there is no need to train a model that predicts them. Can you update your question with the papers you mentioned? Knowing them may help, others too $\endgroup$ May 12, 2023 at 17:59
  • $\begingroup$ I appreciate your time. I've updated my question with relevant information. I would be thankful for any help. $\endgroup$
    – Z Bokaee
    May 14, 2023 at 7:15
  • 1
    $\begingroup$ Honestly, no one could answer my question in such a smooth and clear way. I get the point to a great extend. Thanks a million! $\endgroup$
    – Z Bokaee
    May 17, 2023 at 17:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .