从python中的句子中提取单词

时间:2018-05-31 22:34:45

标签: python nltk

我有一个text / csv文件格式的数据集。它有2列,如this =

ID - TEXT
1 - this probability is 10-15% 
2 - approximately 20% probablity 
3 - 15% probability 

我正在尝试使用NLTK从存在关键字'Probability'的数据中提取数字。

这就是我的代码。

import pandas as pd
import nltk
from nltk import sent_tokenize, word_tokenize

data_file = pd.read_excel(r'data_excel.xlsx',sheet_name = 'data')

df = pd.DataFrame(data_file, columns = ['ID','TEXT'])
keywords = ["probability"]

id_text = nltk.Text(str(df.ID).splitlines()) 
text_value = nltk.Text(str(df.TEXT).splitlines())

我希望输出看起来像这样 -

ID - Value 
1 - 10
2 - 20
3 - 15

如果有人能够朝着正确的方向努力,那将会非常有帮助。

1 个答案:

答案 0 :(得分:0)

这个代码应该起作用,或者至少应该解决这个问题 以下是完整代码

import csv
import nltk
impor re
import pandas as pd
from nltk import sent_tokenize, word_tokenize

tweet = []

data_file = pd.read_excel(r'data_excel.xlsx',sheet_name = 'data')
df = pd.DataFrame(data_file, columns = ['ID','TEXT'])


cols = ['ID', 'Num']
newDataFrame = pd.DataFrame(columns=cols)


#this should provide you with a list of both ID and txt
ID = df.iloc[:,0].values
TEXT  = df.iloc[:,1].values


#loop throug the id and set occurence of the number of probability
for i in range(1, len(ID)):
    number_list = re.findall(r'\b\d+\b', TEXT[i])

    newDataFrame.iloc[i].ID = ID
    newDataFrame.iloc[i].Num = number_list

print(newDataFrame)