计算频率时如何跳过一定长度的单词

时间:2016-12-12 05:50:44

标签: python

我正在制作一个程序,从互联网上获取三首诗,并使用Python解析HTML并找出字数和名词短语的变化。 在我的函数def(frequency_counter)中,我试图计算三首诗中最常用的单词,并且我试图只计算长度为3的单词(所以像#34; a"和""不包括在内),但我认为我在列表理解中犯了一个错误(item = [item for item in item_ item,如果len(item)> = 3])。我已经将我的导入和前两个函数包含在上下文中,但我遇到的麻烦只是在最后一个小函数中。关于我的列表理解应该如何看的任何提示?

import requests
from bs4 import BeautifulSoup
import html2text
from textblob import TextBlob
from collections import Counter


def get_text(*args):
    text_list =[]
    total_list=[]
    for link in args:
        url = link
        r = requests.get(url)
        soup = BeautifulSoup(r.content,'html.parser')
        title = soup.find('title') #finds title 
        #print(title)
        text = html2text.html2text(soup.prettify())
        lines = text.split("\n")
        for word in lines: #for every item in text
            text_tuple = [title, word] #makes tuple
            text_list.append(text_tuple) #append tuple to empty list
           # print(text_list)
        for item in text_list:  
            title_dictionary = {"title": title, "text": item[1]}
            total_list.append(title_dictionary)
    #print(total_list)
    return total_list

def big_index(text_list):
    each_text = []
    for entry in text_list: #for every entry in text_list, creates smaller 

    total_text = ""
    for x in each_text:
        y = str(x)
        total_text = total_text + y
    total_library = total_text.split("text title:")
    #print(total_text)
    return total_library
    #problem I ran into here: this gives me the books twice, not once. I plan
    #to solve this by taking any counts I get in the future functions and 
    #dividing them by two. Ugly, but I can't figure out where the problem is. 

def frequency_counter(total_library):
    words = []
    for item in total_library:
        item = [item for item in total_library if len(item) >= 3]
        blob1 = TextBlob(item)
        count = blob1.word_counts
        frequency = Counter(count).most_common(10) #10 most common words
        words.append(frequency)
    print(words) 
    return words

1 个答案:

答案 0 :(得分:1)

def frequency_counter(total_library):
    words = []
    items = [item1 for item1 in total_library if len(item1) > 3]
    for item in items:
        blob1 = TextBlob(item)
        count = blob1.word_counts
        frequency = Counter(count).most_common(10) #10 most common words
        words.append(frequency)
    print(words) 
    return words