<jupyter_start><jupyter_text>Data CleaningNotebook for testing the cleaning process for our dataset. Setup<jupyter_code>import pandas as pd
import numpy as np
import amzn_reviews_cleaner_funcs as amzn
from pyspark.sql import SparkSession

%autoreload 2<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code># create spark session
spark = SparkSession(sc)
# get dataframe
# specify s3 as sourc with s3a://
df = spark.read.json("s3a://amazon-review-data/reviews_Musical_Instruments_5.json.gz")<jupyter_output><empty_output><jupyter_text>Test helper module  Add tfidf vectors<jupyter_code>df_tfidf, vocab = amzn.add_tfidf(df)

df_tfidf.select("idf_vector").show(3)<jupyter_output>+--------------------+
|          idf_vector|
+--------------------+
|(21502,[0,2,8,10,...|
|(21502,[0,2,4,10,...|
|(21502,[0,7,13,17...|
+--------------------+
only showing top 3 rows<jupyter_text>Test extract top n features<jupyter_code>df_features = amzn.add_top_features(df_tfidf, vocab)
df_features.select("top_features").show(3)<jupyter_output>+--------------------+
|        top_features|
+--------------------+
|[supposed, record...|
|[nose, candy, car...|
|[pops, allowing, ...|
+--------------------+
only showing top 3 rows<jupyter_text>Test clean_reviewText()<jupyter_code>df_clean = amzn.clean_reviewText(df)
df_clean.select("cleanText").show(3)<jupyter_output>+--------------------+
|           cleanText|
+--------------------+
|Not much to write...|
|The product does ...|
|The primary job o...|
+--------------------+
only showing top 3 rows<jupyter_text>Test removal of empty tokens<jupyter_code># clean
df_clean = amzn.clean_reviewText(df)

# tokenize
df_raw_tokens = amzn.tokenize(df_clean)
df_raw_tokens.select("raw_tokens").show(3)
df_tfidf.printSchema()<jupyter_output>root
 |-- asin: string (nullable = true)
 |-- helpful: array (nullable = true)
 |    |-- element: long (containsNull = true)
 |-- overall: double (nullable = true)
 |-- reviewText: string (nullable = true)
 |-- reviewTime: string (nullable = true)
 |-- reviewerID: string (nullable = true)
 |-- reviewerName: string (nullable = true)
 |-- summary: string (nullable = true)
 |-- unixReviewTime: long (nullable = true)
 |-- cleanText: string (nullable = true)
 |-- raw_tokens: array (nullable = true)
 |    |-- element: string (containsNull = true)
 |-- tokens: array (nullable = true)
 |    |-- element: string (containsNull = true)
 |-- tf_vector: vector (nullable = true)
 |-- idf_vector: vector (nullable = true)<jupyter_text>Analyze row<jupyter_code>df_features.show(1)
test_row = df_features.first()
test_row["top_features"]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20765441455096628, 'fraction_numerical': 0.01667298219022357, 'mean_word_length': 3.6315789473684212, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '433408', 'license': 'MIT', 'n_tokens_mistral': 938, 'n_tokens_neox': 917, 'n_words': 185, 'path': 'src/archive/test-data-cleaning.ipynb', 'repo_name': 'rob-dalton/amazonfeatures-feature-extraction', 'size': '2639'}