以下内容使我了解以下输出:
words freq
0 hello 5
1 yes 10
I would like the above output to be same for ngrams(4). The results is only showing freq with "1". Can someone help me tune the codes for ngrams and as per the above output. The requirement is Ngrams with freqencies and output in excel(xlsx).
示例如下:
(('benito', 'kanchan'), 1),
(('kanchan', 'tata'), 1),
(('tata', 'arora'), 1),
So far the code:
df = pd.read_excel(r"Filename")
#Converting to lovercase
df['Body'] = df['Body'].apply(lambda x: " ".join(x.lower() for x in x.split()))
df['Body'].head()
#Count of Words
df['word_count'] = df['Body'].apply(lambda x: len(str(x).split(" ")))
df[['Body','word_count']].head()
#Removing Punctuation
df['Body'] = df['Body'].str.replace('[^\w\s]','')
df['Body'].head()
#Removing Stop Words
from nltk.corpus import stopwords
stop = stopwords.words('english')
df['Body'] = df['Body'].apply(lambda x: " ".join(x for x in x.split() if x not in stop))
df['Body'].head()
#df['Body'] = df['Body'].astype('|S')
# Word Count
tf1 = (df['Body']).apply(lambda x: pd.value_counts(x.split(" "))).sum(axis = 0).reset_index()
print (tf1)
tf1.columns = ['words','tf']
tf1
Ngrams 从馆藏进口柜台 从textblob导入TextBlob 一个= TextBlob(tf1 ['words'] [0])。ngrams(4) a = [','。join(map(str,l))for a in l] 打印(一) 计数器=(计数器(a)) counter.most_common(150) counter.columns = ['ngram','tf'] 计数器