【发布时间】:2017-03-29 11:08:40
【问题描述】:
我想计算单个文本文件中包含的每篇文章的单词列表的出现次数。 每篇文章都可以被识别,因为它们都以一个共同的标签“
广告'”开头。
这是文本文件的示例:
"[<p>Advertisement , By TIM ARANGO , SABRINA TAVERNISE and CEYLAN YEGINSU JUNE 28, 2016
,Credit Ilhas News Agency, via Agence France-Presse — Getty Images,ISTANBUL ......]
[<p>Advertisement , By MILAN SCHREUER and ALISSA J. RUBIN OCT. 5, 2016
, BRUSSELS — A man wounded two police officers with a knife in Brussels around noon
on Wednesday in what the authorities called “a potential terrorist attack.” ,
The two ......]"
我想要做的是计算每个单词的频率我有一个 csv 文件(20 个单词)并像这样写输出:
id, attack, war, terrorism, people, killed, said
article_1, 45, 5, 4, 6, 2,1
article_2, 10, 3, 2, 1, 0,0
csv中的单词是这样存储的:
attack
people
killed
attacks
state
islamic
按照建议,我首先尝试通过标签<p> 拆分整个文本文件,然后再开始计算单词。然后我标记了文件文本中的列表。
这是我目前所拥有的:
opener = open("News_words_most_common.csv")
words = opener.read()
my_pattern = ('\w+')
x = re.findall(my_pattern, words)
file_open = open("Training_News_6.csv")
files = file_open.read()
r = files.lower()
stops = set(stopwords.words("english"))
words = r.split("<p>")
token= word_tokenize(words)
string = str(words)
token= word_tokenize(string)
print(token)
这是输出:
['[', "'", "''", '|', '[', "'", ',', "'advertisement",
',', 'by', 'milan', 'schreuer'.....']', '|', "''", '\\n', "'", ']']
下一步将围绕拆分的文章进行循环(现在转入标记化的单词列表)并计算第一个文件中单词的频率。如果您对如何交互和计数有任何建议,请告诉我!
我在 Anaconda 上使用 Python 3.5
【问题讨论】:
-
是的,它是相关的。我知道如何使用计数器模块。我已经这样做了来创建单词列表。最重要的是计算我的单个文本文件中包含的每篇文章中列表中单词的频率。
标签: python python-3.x counter word-frequency