I am saving a list of tokenized data with NLTK into a csv file with just one column. Later, i have to retrieve the tokens and create a dictionary of keywords using method:
dictionary = gensim.corpora.Dictionary(column).
The problem is that when i save the tokenized file into a csv, the tokens are saved in single quotations and when i try to retrieve them and give the dataframe column to a gensim method to create a dictionary, it gives an error that the dictionary needs an array of tokens and not a string. The following steps are performed:
Steps:
The csv file with one column is:
Description
0 Key moments included the DOJ description of the FBI affidavit used for the
search, warnings about chilling witnesses and inaction from Trump's attorney.
1 Russian vehicles seen inside turbine hall at Ukraine nuclear plant.
2 Finnish PM says videos of her partying shouldn't have been made public.
.......
and so on
I read the csv file and then tokenize the data using the following method:
df = pd.read_csv('news_csv.csv', encoding='latin-1')
def tokenize(column):
tokens = nltk.word_tokenize(column)
return [w for w in tokens]
Now, i tokenize the dataframe and save it again into a csv.
processedData = df['Description'].astype(str).map(tokenize)
processedData.to_csv('nesws_tokenized.csv', header=True, index = False)
Now, i read the column and pass it to the gensim method to create a dictionary of keywords:
df1 = pd.read_csv('news_tokenized.csv', encoding='latin-1')
dictionary = gensim.corpora.Dictionary(df1)
When i run it, it gives the following error:
TypeError: doc2bow expects an array of unicode tokens on input, not a single string
Why i am getting this error? I believe the data is saved in the column as comma separated tokens and also tokens are single quoted. Is single quotation a problem as if without saving the tokenized data, if i directly pass the processedData to gensim method, it creates the dictionary but not when i save it into a csv and retrieve it again.
Important Note: I have to save it into a csv file because the dataset is very large and the Colab session crashed due to the full use of resources and removal of keywords and lemmatization process takes almost all the ram so i can not proceed further so that is why i have to save the data into csv files and then start a new session to complete the task.