下面是我需要分析并从中提取特定单词的许多行的两个示例
[40.748330000000003, -73.878609999999995] 6 2011-08-28 19:52:47 Sometimes I wish my life was a movie; #unreal I hate the fact I feel lonely surrounded by so many ppl
[37.786221300000001, -122.1965002] 6 2011-08-28 19:55:26 I wish I could lay up with the love of my life And watch cartoons all day.
忽略坐标和数字
这个例子是为了找出每个tweet行中有多少单词出现在这个关键字列表中:
['hate', 1]
['hurt', 1]
['hurting', 1]
['like', 5]
['lonely', 1]
['love', 10]
同时,找出每一条tweet行中关键字的值之和(例如['love',10])
例如,对于这个句子
'I hate to feel lonely at times'
仇恨=1和孤独=1的情感值之和等于2。 行中的字数是7
我试过使用列表到列表的方法,甚至试图通过每个句子和关键字,但这些都没有工作,因为推特和关键字的数量是几个,我需要使用循环格式来找到值
我想知道的是每行中找到的关键字的情感值和每行中有多少单词
提前感谢您的洞察力!!:)
我的代码:
try:
KeywordFileName=input('Input keyword file name: ')
KeywordFile = open(KeywordFileName, 'r')
except FileNotFoundError:
print('The file you entered does not exist or is not in the directory')
exit()
KeyLine = KeywordFile.readline()
while KeyLine != '':
if list != []:
KeyLine = KeywordFile.readline()
KeyLine = KeyLine.rstrip()
list = KeyLine.split(',')
list[1] = int(list[1])
print(list)
else:
break
try:
TweetFileName = input('Input Tweet file name: ')
TweetFile = open(TweetFileName, 'r')
except FileNotFoundError:
print('The file you entered does not exist or is not in the directory')
exit()
TweetLine = TweetFile.readline()
while TweetLine != '':
TweetLine = TweetFile.readline()
TweetLine = TweetLine.rstrip()
最简单的方法是在每个tweet的基础上使用nltk库中的word\u标记化
`
如果您的tweet位于.txtlike this file中,并且tweet的行模式与问题中描述的相同,那么您可以尝试以下方法:
输出:
r'[a-zA-Z].+' #if you use this change find.group(1) to find.group()
r'(?<=\d.\s)[a-zA-Z].+' #if you use this change find.group(1) to find.group()
r'\d{2}:\d{2}:\d{2}\s([a-zA-Z].+)'
r'\b\d{2}:\d{2}:\d{2} (.+)' #group(1)
相关问题 更多 >
编程相关推荐