我正在尝试创建一个带有字符串的python脚本,并给出连续单词的计数。 让我们说:
string = " i have no idea how to write this script. i have an idea."
output =
['i', 'have'] 2
['have', 'no'] 1
['no', 'idea'] 1
['idea', 'how'] 1
['how', 'to'] 1
['to', 'write'] 1
...
我正在尝试使用python而不从集合中导入集合,计数器。我的内容如下。我正在尝试使用re.findall(#whatpatterndoiuse, string)
来迭代字符串并进行比较,但我很难搞清楚如何操作。
string2 = re.split('\s+', string. lower())
freq_dict = {} #empty dictionary
for word in word_list:
word = punctuation.sub("", word)
freq_dic[word] = freq_dic.get(word,0) + 1
freq_list = freq_dic.items()
freq_list.sort()
for word, freq in freq_list:
print word, freq
使用我不想要的收藏品中的计数器。此外,它产生的输出格式不是我上面提到的格式。
import re
from collections import Counter
words = re.findall('\w+', open('a.txt').read())
print(Counter(zip(words,words[1:])))
答案 0 :(得分:2)
没有拉链解决这个问题非常简单。只需构建每对单词的元组并在dict中跟踪它们的计数。只有几个特殊情况需要注意 - 当输入字符串只有一个单词时,以及当你在字符串的末尾时。
试一试:
def freq(input_string):
freq = {}
words = input.split()
if len(words) == 1:
return freq
for idx, word in enumerate(words):
if idx+1 < len(words):
word_pair = (word, words[idx+1])
if word_pair in freq:
freq[word_pair] += 1
else:
freq[word_pair] = 1
return freq
答案 1 :(得分:1)
您需要解决三个问题:
['i', 'have']
,['have', 'no']
,...); 使用Counter
可以轻松解决第二个问题。 Counter
个对象还提供most_common()
方法来解决第三个问题。
第一个问题可以通过多种方式解决。最紧凑的方法是使用zip
:
>>> import re
>>> s = 'i have no idea how to write this script. i have an idea.'
>>> words = re.findall('\w+', s)
>>> pairs = zip(words, words[1:])
>>> list(pairs)
[('i', 'have'), ('have', 'no'), ('no', 'idea'), ...]
把所有东西放在一起:
import collections
import re
def count_pairs(s):
"""
Returns a mapping that links each pair of words
to its number of occurrences.
"""
words = re.findall('\w+', s.lower())
pairs = zip(words, words[1:])
return collections.Counter(pairs)
def print_freqs(s):
"""
Prints the number of occurrences of word pairs
from the most common to the least common.
"""
cnt = count_pairs(s)
for pair, count in cnt.most_common():
print list(pair), count
编辑:我刚才意识到,我不小心读了“with collections,counter,...”而不是“没有导入集合,... 。“。我的不好,抱歉。
答案 2 :(得分:0)
string = "i have no idea how to write this script. i have an idea."
def count_words(string):
''' warning, won't work with a leading or trailing space,
though all you would have to do is check if there is one, and remove it.'''
x = string.split(' ')
return len(x)
答案 3 :(得分:0)
我认为答案发布在下面。 :)。它需要一个TXT文件,但可以轻松地操作以接收字符串。简单地删除arg1并插入自己的字符串!!!
script, arg1 = argv #takes 2 arguments
#conditions
try:
sys.argv[1]
except IndexError:
print('doesnt work insert 2 arguments\n')
exit()
with open(arg1, 'r') as content_file: #open file
textsplit = content_file.read() #read it
textsplit = textsplit.lower() #lowercase it
word_list = textsplit.split() #split file put into var word_lists
textsplit = re.sub(r"[^\w\s]+", "", textsplit).split() #remove white space
#print textsplit
freq_dic = {} #creates empty dictionary
for i in range( 0, len(textsplit)-1): #counter to itterate
key = textsplit[i] + ',' + textsplit[i+1] # produces corresponding keys
try:
freq_dic[key]+=1 #if
except:
freq_dic[key]=1 #if not
for word in freq_dic:
print [word], freq_dic[word]