ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python统计英文短文中单词的频次并自定义显示前n高频

2022-03-02 20:30:21  阅读:227  来源: 互联网

标签:短文 google 自定义 Python text Style style project Guide


在这里插入代码片
```import operator

splits = ['\n', ' ', '-', ':', '/', '*', '_', '(', ')', '"', '”', '“',']','[']
google_style_guide = '''
    Every major open-source project has its own style guide: a set of conventions (sometimes arbitrary) about how to write code for that project. It is much easier to understand a large codebase when all the code in it is in a consistent style.

    “Style” covers a lot of ground, from “use camelCase for variable names” to “never use global variables” to “never use exceptions.” This project (google/styleguide) links to the style guidelines we use for Google code. If you are modifying a project that originated at Google, you may be pointed to this page to see the style guides that apply to that project.

    This project holds the C++ Style Guide, C# Style Guide, Swift Style Guide, Objective-C Style Guide, Java Style Guide, Python Style Guide, R Style Guide, Shell Style Guide, HTML/CSS Style Guide, JavaScript Style Guide, TypeScript Style Guide, AngularJS Style Guide, Common Lisp Style Guide, and Vimscript Style Guide. This project also contains cpplint, a tool to assist with style guide compliance, and google-c-style.el, an Emacs settings file for Google style.
    '''
def division_text(text,top_n=5,splits=splits):
    top_n=int(input("请输入想显示前几位高频词"))
    i=0
    NumOfWords=0
    list_of_the_SerialNumber_of_split=[]
    list_of_word=[]
    dic_of_word_and_sequence={}
    while i< len(text):
        if text[i] in splits :
            list_of_the_SerialNumber_of_split.append(i)
            i+=1
        else :
            i+=1
            continue
    j=0
    while j<len(list_of_the_SerialNumber_of_split) :
        if j<len(list_of_the_SerialNumber_of_split)-1:
            if list_of_the_SerialNumber_of_split[j] == list_of_the_SerialNumber_of_split[j + 1] - 1:
                j += 1
                continue
            else:
                list_of_word.append(text[list_of_the_SerialNumber_of_split[j]:list_of_the_SerialNumber_of_split[j + 1]])
                list_of_word[NumOfWords] = list_of_word[NumOfWords][1:]
                NumOfWords += 1
                j += 1
        else :
            if list_of_the_SerialNumber_of_split[j]==len(text)-1 :
                break
            else :
                list_of_word.append(text[list_of_the_SerialNumber_of_split[j]:])
                list_of_word[NumOfWords]=list_of_word[NumOfWords][1:]
                NumOfWords+=1
    print("含有的单词为:",list_of_word)
    for i in list_of_word:
        dic_of_word_and_sequence[f"{i}"]=list_of_word.count(i)
    a=sorted(dic_of_word_and_sequence.items(),key=operator.itemgetter(1),reverse=True)
    print("单词和频次为:")
    order=1
    for i in a:
        if order>top_n:
            break
        print("{}.  单词:  {},频次: {}".format(order,i[0],i[1]))
        order+=1


ChinaDailly="An important lesson from the nation's great struggles over the years is that China can only earn respect, take the initiative and defend its sovereignty, security and development interests by daring to struggle and excelling in this regard, he said."
division_text(google_style_gui

标签:短文,google,自定义,Python,text,Style,style,project,Guide
来源: https://blog.csdn.net/qq_51979013/article/details/123239409

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有