import nltk from nltk import WordNetLemmatizer from nltk.stem import PorterStemmer from nltk.corpus import stopwords from textblob import TextBlob from nltk.tokenize import RegexpTokenizer import re import string nltk.download('stopwords') nltk.download('punkt') nltk.download('omw-1.4') from collections import OrderedDict from wordcloud import WordCloud from matplotlib import pyplot as plt def join_countries(a_list): joined_string = "|".join(a_list) return joined_string def join_columns(x): return ' '.join(x.dropna()) def remove_short_words(text, length): if text is None: text = '' else: regex = '\W*\\b\w{1,' + str(length) +'}\\b' shortword = re.compile(regex) text = shortword.sub('', text) return text def remove_duplicates_from_cell(text): if text is None: text = '' else: text = text.split() text = ' '.join(OrderedDict.fromkeys(text).keys()) return text def make_lower_case(text): if text is None: text = '' else: text = text.lower() return text def remove_curly_brackets(text): if text is None: text = '' else: text = text.replace("{","").replace("}","") return text def remove_double_quotes(text): if text is None: text = '' else: text = text.replace('"','') return text def remove_and_semicolon(text): if text is None: text = '' else: text = re.sub(r'\&[^]]*\;', '', text) return text def remove_slash_n(text): if text is None: text = '' else: text = text.replace('\n','') return text def remove_urls(text): if text is None: text = '' else: text = re.sub(r'http\S+', '', text) return text def remove_html_tags(text): if text is None: text = '' else: clean = re.compile('<.*?>') text = re.sub(clean, '', text) return text def remove_forward_slash(text): if text is None: text = '' else: text = text.replace('/','') return text def remove_and_sign(text): if text is None: text = '' else: text = text.replace('&','') return text def remove_dash_sign(text): if text is None: text = '' else: text = text.replace('-','') return text # Function for replacing | sign with comma def replace_straight(text): if text is None: text = '' else: text = text.replace("|",",") return text # Function for replacing | sign with space def replace_straight_with_space(text): if text is None: text = '' else: text = text.replace("|"," ") return text # Function for replace comma with | sign def replace_comma_straight(text): if text is None: text = '' else: text = text.replace(",","|") return text # Function for revoming non ascii symbols def remove_non_ascii(s): if s is None: s = '' else: s = "".join(i for i in s if ord(i)<128) return s def remove_stop_words(text): if text is None: text = '' else: text = text.split() stops = set(stopwords.words("english")) text = [w for w in text if not w in stops] text = " ".join(text) return text def remove_punctuation(text): if text is None: text = '' else: tokenizer = RegexpTokenizer(r'\w+') text = tokenizer.tokenize(text) text = " ".join(text) return text # Function for removing the html tags def remove_html(text): if text is None: text = '' else: html_pattern = re.compile('<.*?>') text = html_pattern.sub(r'', text) return text def draw_word_cloud(paragraph): # Create WordCloud word_cloud = WordCloud(width = 1000, height = 800, background_color ='Black', #stopwords = stopword_list, min_font_size = 14).generate(paragraph) # Set wordcloud figure size plt.figure(figsize = (10, 6)); # Show image plt.imshow(word_cloud); # Remove Axis plt.axis("off"); plt.show(); def tokenize(x): tokenizer = RegexpTokenizer(r'\w+') return tokenizer.tokenize(x) def stemmer(x): stemmer = PorterStemmer() return ' '.join([stemmer.stem(word) for word in x]) def lemmatize(x): lemmatizer = WordNetLemmatizer() return ' '.join([lemmatizer.lemmatize(word) for word in x])