0
点赞
收藏
分享

微信扫一扫

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

星河出山 2022-03-11 阅读 23
python
在这里插入代码片
```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

举报

相关推荐

0 条评论