The text file contains the collection of short movie reviews. Each review is enclosed in parentheses and consists of a numerical rating followed by the review text. The numerical rating is on a scale of 0 to 4, where higher numbers indicate a more positive review.
Here are some additional observations:
Format: The reviews follow a consistent format with the rating at the beginning, making it easy to identify the sentiment of each review. Concise: The reviews are generally concise, focusing on key aspects of the movie. Variety of Movies: The reviews cover a wide range of movies, from dramas and comedies to documentaries and action films. Diverse Opinions: The ratings suggest a diversity of opinions on the movies.
The text file contains sentences that are hierarchically structured, likely using nested parentheses to represent some form of syntactic parsing. To convert this into a format suitable for sentiment analysis, we need to extract the sentences in a plain text format and load them into a DataFrame.
Follow the below code for the above process
import pandas as pd import re
labels = [] texts = []
with open("train.txt", "r") as file: for line in file: match = re.search(r'((\d+) ((.*)))', line) if match: labels.append(int(match.group(1))) texts.append(match.group(2))
df = pd.DataFrame({"label": labels, "text": texts}) df.to_csv("output.csv", index=False)
import pandas as pd import re
df = pd.read_csv("output.csv") pattern = r'[().,;:"!?0-9]' df['cleaned_text'] = df['text'].astype(str).apply(lambda x: re.sub(pattern, '', x)) df[['label', 'cleaned_text']].to_csv("cleaned_output.csv", index=False) df = df.drop('text' , axis=1) df.to_csv('sentence_train.csv' , index=False)