从词频创建 ARFF

发布于 2024-10-27 20:48:36 字数 1296 浏览 3 评论 0原文

我有一些代码为我提供了单词列表以及它们在文本中出现的频率,我希望代码能够将前 10 个单词自动转换为带有

@RELATION wordfrequencies

@ATTRIBUTE word string 的 ARFF @ATTRIBUTE 频率数字

和前 10 个数据及其频率。

我正在努力思考如何用我当前的代码来做到这一点,

import re
import nltk

# Quran subset
filename = 'subsetQuran.txt'

# create list of lower case words
word_list = re.split('\s+', file(filename).read().lower())
print 'Words in text:', len(word_list)

word_list2 = [w.strip() for w in word_list if w.strip() not in nltk.corpus.stopwords.words('english')]



# create dictionary of word:frequency pairs
freq_dic = {}
# punctuation and numbers to be removed
punctuation = re.compile(r'[-.?!,":;()|0-9]') 
for word in word_list2:
    # remove punctuation marks
    word = punctuation.sub("", word)
    # form dictionary
    try: 
        freq_dic[word] += 1
    except: 
        freq_dic[word] = 1


print '-'*30

print "sorted by highest frequency first:"
# create list of (val, key) tuple pairs
freq_list2 = [(val, key) for key, val in freq_dic.items()]
# sort by val or frequency
freq_list2.sort(reverse=True)
freq_list3 = list(freq_list2)
# display result
for freq, word in freq_list2:
    print word, freq
f = open("wordfreq.txt", "w")
f.write( str(freq_list3) )
f.close()

任何有关这方面的帮助都是值得赞赏的,这样做的一种方法真的是绞尽脑汁!

I have some code that gives me a list of words with their frequencies that they occur in the text, I'm looking to make it so the code converts the top 10 words automatically into an ARFF with

@RELATION wordfrequencies

@ATTRIBUTE word string
@ATTRIBUTE frequency numeric

and the top 10 as data with their frequency.

I'm struggling with how to do this with my current code

import re
import nltk

# Quran subset
filename = 'subsetQuran.txt'

# create list of lower case words
word_list = re.split('\s+', file(filename).read().lower())
print 'Words in text:', len(word_list)

word_list2 = [w.strip() for w in word_list if w.strip() not in nltk.corpus.stopwords.words('english')]



# create dictionary of word:frequency pairs
freq_dic = {}
# punctuation and numbers to be removed
punctuation = re.compile(r'[-.?!,":;()|0-9]') 
for word in word_list2:
    # remove punctuation marks
    word = punctuation.sub("", word)
    # form dictionary
    try: 
        freq_dic[word] += 1
    except: 
        freq_dic[word] = 1


print '-'*30

print "sorted by highest frequency first:"
# create list of (val, key) tuple pairs
freq_list2 = [(val, key) for key, val in freq_dic.items()]
# sort by val or frequency
freq_list2.sort(reverse=True)
freq_list3 = list(freq_list2)
# display result
for freq, word in freq_list2:
    print word, freq
f = open("wordfreq.txt", "w")
f.write( str(freq_list3) )
f.close()

Any help with this is appreciated, a way of doing this is really racking my brain!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-11-03 20:48:36

我希望你不介意稍微重写一下:

import re
import nltk
from collections import defaultdict

# Quran subset
filename = 'subsetQuran.txt'

# create list of lower case words
word_list = open(filename).read().lower().split()
print 'Words in text:', len(word_list)

# remove stopwords
word_list = [w for w in word_list if w not in nltk.corpus.stopwords.words('english')]

# create dictionary of word:frequency pairs
freq_dic = defaultdict(int)

# punctuation and numbers to be removed
punctuation = re.compile(r'[-.?!,":;()|0-9]') 
for word in word_list:
    # remove punctuation marks
    word = punctuation.sub("", word)
    # increment count for word
    freq_dic[word] += 1

print '-' * 30

print "sorted by highest frequency first:"
# create list of (frequency, word) tuple pairs
freq_list = [(freq, word) for word, freq in freq_dic.items()]

# sort by descending frequency
freq_list.sort(reverse=True)

# display result
for freq, word in freq_list:
    print word, freq

# write ARFF file for 10 most common words
f = open("wordfreq.txt", "w")
f.write("@RELATION wordfrequencies\n")
f.write("@ATTRIBUTE word string\n")
f.write("@ATTRIBUTE frequency numeric\n")
f.write("@DATA\n")
for freq, word in freq_list[ : 10]:
    f.write("'%s',%d\n" % (word, freq))
f.close()

I hope you don't mind the slight rewrite:

import re
import nltk
from collections import defaultdict

# Quran subset
filename = 'subsetQuran.txt'

# create list of lower case words
word_list = open(filename).read().lower().split()
print 'Words in text:', len(word_list)

# remove stopwords
word_list = [w for w in word_list if w not in nltk.corpus.stopwords.words('english')]

# create dictionary of word:frequency pairs
freq_dic = defaultdict(int)

# punctuation and numbers to be removed
punctuation = re.compile(r'[-.?!,":;()|0-9]') 
for word in word_list:
    # remove punctuation marks
    word = punctuation.sub("", word)
    # increment count for word
    freq_dic[word] += 1

print '-' * 30

print "sorted by highest frequency first:"
# create list of (frequency, word) tuple pairs
freq_list = [(freq, word) for word, freq in freq_dic.items()]

# sort by descending frequency
freq_list.sort(reverse=True)

# display result
for freq, word in freq_list:
    print word, freq

# write ARFF file for 10 most common words
f = open("wordfreq.txt", "w")
f.write("@RELATION wordfrequencies\n")
f.write("@ATTRIBUTE word string\n")
f.write("@ATTRIBUTE frequency numeric\n")
f.write("@DATA\n")
for freq, word in freq_list[ : 10]:
    f.write("'%s',%d\n" % (word, freq))
f.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文