<jupyter_start><jupyter_text>**Bag of Words (BOW) and Tf-idf unstemmed**<jupyter_code>def transform_BOW(training, testing, column_name):
    vect = CountVectorizer(max_features=100, ngram_range=(1,3), stop_words=ENGLISH_STOP_WORDS)
    vectFit = vect.fit(training[column_name])
    BOW_training = vectFit.transform(training[column_name])
    BOW_training_df = pd.DataFrame(BOW_training.toarray(), columns=vect.get_feature_names())
    BOW_testing = vectFit.transform(testing[column_name])
    BOW_testing_Df = pd.DataFrame(BOW_testing.toarray(), columns=vect.get_feature_names())
    return vectFit, BOW_training_df, BOW_testing_Df
def transform_tfidf(training, testing, column_name):
    Tfidf = TfidfVectorizer(ngram_range=(1,3), max_features=100, stop_words=ENGLISH_STOP_WORDS)
    Tfidf_fit = Tfidf.fit(training[column_name])
    Tfidf_training = Tfidf_fit.transform(training[column_name])
    Tfidf_training_df = pd.DataFrame(Tfidf_training.toarray(), columns=Tfidf.get_feature_names())
    Tfidf_testing = Tfidf_fit.transform(testing[column_name])
    Tfidf_testing_df = pd.DataFrame(Tfidf_testing.toarray(), columns=Tfidf.get_feature_names())
    return Tfidf_fit, Tfidf_training_df, Tfidf_testing_df
from sklearn import svm
from sklearn.metrics import classification_report
from sklearn.calibration import CalibratedClassifierCV

# def build_model(X_train, y_train, X_test, y_test, name_of_test):
#     # log_reg = LogisticRegression(C=30, max_iter=200).fit(X_train, y_train)
#     linear_svm = svm.SVC(kernel='linear',C=1.0,probability=True)
#     linear_svm.fit(X_train, y_train)
#     # linearn_svm = LinearSVC(C=1)

#     y_pred = linear_svm.predict(X_test)
#     print('Training accuracy of '+name_of_test+': ', linear_svm.score(X_train, y_train))
#     print('Testing accuracy of '+name_of_test+': ', linear_svm.score(X_test, y_test))
#     print(classification_report(y_test, y_pred)) 
#     return linear_svm

def build_model(X_train, y_train, X_test, y_test, name_of_test):
    # log_reg = LogisticRegression(C=30, max_iter=200).fit(X_train, y_train)
    lin_svm = LinearSVC(C=1)
    linear_svm = CalibratedClassifierCV(lin_svm)
    linear_svm.fit(X_train, y_train)
    # linearn_svm = LinearSVC(C=1)
    y_pred = linear_svm.predict(X_test)
    print('Training accuracy of '+name_of_test+': ', linear_svm.score(X_train, y_train))
    print('Testing accuracy of '+name_of_test+': ', linear_svm.score(X_test, y_test))
    print(classification_report(y_test, y_pred)) 
    return linear_svm<jupyter_output><empty_output><jupyter_text>**loading training and testing dataset**<jupyter_code>df_train = load_data('train')
df_test = load_data('test')

df_train<jupyter_output>Using custom data configuration default
Reusing dataset rotten_tomatoes_movie_review (/root/.cache/huggingface/datasets/rotten_tomatoes_movie_review/default/1.0.0/40d411e45a6ce3484deed7cc15b82a53dad9a72aafd9f86f8f227134bec5ca46)<jupyter_text>Bag of Words(BOW)<jupyter_code># Create unstemmed BOW features for training set
unstemmed_BOW_vect_fit, df_train_bow_unstem, df_test_bow_unstem = transform_BOW(df_train, df_test, 'Review')<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated in 1.0 and will be removed in 1.2. Please use get_feature_names_out instead.
  warnings.warn(msg, category=FutureWarning)<jupyter_text>TF-idf<jupyter_code>unstemmed_tfidf_vect_fit, df_train_tfidf_unstem, df_test_tfidf_unstem = transform_tfidf(df_train, df_test, 'Review')<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated in 1.0 and will be removed in 1.2. Please use get_feature_names_out instead.
  warnings.warn(msg, category=FutureWarning)
/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated in 1.0 and will be removed in 1.2. Please use get_feature_names_out instead.
  warnings.warn(msg, category=FutureWarning)<jupyter_text>Training linear svm model on BOW(unstemmed) features and tf-idf(unstemmed) features<jupyter_code>bow_unstemmed = build_model(df_train_bow_unstem, df_train['Sentiment'], df_test_bow_unstem, df_test['Sentiment'], 'BOW Unstemmed')
tfidf_unstemmed = build_model(df_train_tfidf_unstem, df_train['Sentiment'], df_test_tfidf_unstem, df_test['Sentiment'], 'TFIDF Unstemmed')<jupyter_output>Training accuracy of TFIDF Unstemmed:  0.6213364595545134
Testing accuracy of TFIDF Unstemmed:  0.6078799249530957
              precision    recall  f1-score   support

           0       0.60      0.67      0.63       533
           1       0.62      0.54      0.58       533

    accuracy                           0.61      1066
   macro avg       0.61      0.61      0.61      1066
weighted avg       0.61      0.61      0.61      1066<jupyter_text>Installing textattack toolbox<jupyter_code>!pip install textattack
!pip install tensorflow-text<jupyter_output>Requirement already satisfied: textattack in /usr/local/lib/python3.7/dist-packages (0.3.4)
Requirement already satisfied: more-itertools in /usr/local/lib/python3.7/dist-packages (from textattack) (8.8.0)
Requirement already satisfied: lemminflect in /usr/local/lib/python3.7/dist-packages (from textattack) (0.2.2)
Requirement already satisfied: torch!=1.8,>=1.7.0 in /usr/local/lib/python3.7/dist-packages (from textattack) (1.10.0+cu111)
Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from textattack) (1.4.1)
Requirement already satisfied: num2words in /usr/local/lib/python3.7/dist-packages (from textattack) (0.5.10)
Requirement already satisfied: language-tool-python in /usr/local/lib/python3.7/dist-packages (from textattack) (2.6.2)
Requirement already satisfied: filelock in /usr/local/lib/python3.7/dist-packages (from textattack) (3.4.0)
Requirement already satisfied: bert-score>=0.3.5 in /usr/local/lib/python3.7/dist-packages (from textattack)[...]<jupyter_text>Performing Textfooler attack<jupyter_code>import textattack
from textattack.models.wrappers import SklearnModelWrapper
from textattack.datasets import HuggingFaceDataset
from textattack.attack_recipes import TextFoolerJin2019
from textattack import Attacker<jupyter_output><empty_output><jupyter_text>**On Unstemmed BOW features**<jupyter_code>model_wrapper = SklearnModelWrapper(bow_unstemmed, unstemmed_BOW_vect_fit)
dataset = HuggingFaceDataset("rotten_tomatoes", None, "train")
attack = TextFoolerJin2019.build(model_wrapper)

attacker = Attacker(attack, dataset)
attacker.attack_dataset()<jupyter_output>Attack(
  (search_method): GreedyWordSwapWIR(
    (wir_method):  delete
  )
  (goal_function):  UntargetedClassification
  (transformation):  WordSwapEmbedding(
    (max_candidates):  50
    (embedding):  WordEmbedding
  )
  (constraints): 
    (0): WordEmbeddingDistance(
        (embedding):  WordEmbedding
        (min_cos_sim):  0.5
        (cased):  False
        (include_unknown_words):  True
        (compare_against_original):  True
      )
    (1): PartOfSpeech(
        (tagger_type):  nltk
        (tagset):  universal
        (allow_verb_noun_swap):  True
        (compare_against_original):  True
      )
    (2): UniversalSentenceEncoder(
        (metric):  angular
        (threshold):  0.840845057
        (window_size):  15
        (skip_text_shorter_than_window):  True
        (compare_against_original):  False
      )
    (3): RepeatModification
    (4): StopwordModification
    (5): InputColumnModification(
        (matching_column_labels):  ['premise', 'hypothesis']
       [...]<jupyter_text>**On Unstemmed tf-idf features**<jupyter_code>from textattack.models.wrappers import SklearnModelWrapper

model_wrapper = SklearnModelWrapper(tfidf_unstemmed, unstemmed_tfidf_vect_fit)
dataset = HuggingFaceDataset("rotten_tomatoes", None, "train")
attack = TextFoolerJin2019.build(model_wrapper)

attacker = Attacker(attack, dataset)
attacker.attack_dataset()<jupyter_output><empty_output>