Liangmeng Yao 20190912-3 word frequency statistics

This job requires See https://edu.cnblogs.com/campus/nenu/2019fall/homework/6583

Code: [ https://e.coding.net/hahaa/summer.git]

Word frequency statistics SPEC 20180918

Five in the bedroom of his bragging familiar with the "Robinson Crusoe" in front of the girls bragging love "Wuthering Heights," "Jane Eyre" and "Gone with the Wind", read through before you say, "War and Peace." However, he has not had four. You guys talk privately, vocabulary this masterpiece of how these may be less than four, we heard that you learn "building law", unanimously elected you write a program called wf, vocabulary English works and gives statistics the number of times each word appears, ready to fight for the Five face.

We hope to achieve the following effects. The following is purely fabricated digital effects.

Function 1 small file input. To demonstrate the program run, and not the result of real persecution Five, ask him personally key
pad to enter commands at the console.

A. A heavy and difficult function

1. On converting .py files to .exe file spend a lot of time

Specific steps:

(1) into the Python Scripts folder. Then, type in cmd command: pip.exe install Pyinstallerthe next will complete pyinstaller installation, the installation is complete there will be pyinstaller.exe in the Scripts folder, the installation was successful.

(2) Finally, to achieve Python script to exe conversion via command line command is as follows:pyinstaller.exe [-parameter] filename

2. Start time no idea no idea where to start, ask seniors through the exchange and analysis of the students this question, then learn the relevant knowledge of python, understand the purpose of the module, a functional understanding of the major collections module in the Counter class and most_common ( ) function as well as learn regular expressions, which for me are difficult.

Counter to track the number of times the value occurs

most_common (n) for n Top implement the function returns a list of tuples of elements to provide current frequency and the frequency of the n

jisuanTotal DEF (Word): 
    user_counters = Counter (Word) 
    Total = 0 
    for user_counter in user_counters: 
        Total + = . 1 
    Print ( " Total " + STR (Total) + " words \ n- " ) 
    LSTs = user_counters.most_common ( 10 ) 
# most_common () for realizing functions of the Top 10, returns a list of tuples providing the front element 10 and the number of frequencies 
    for LST in LSTs: 
        Print ( " % S% D " % (LST [ 0 ], LST [ . 1 ] )) 
        # the element count and displayed
def word_list (filename): # implement a function 
    with Open (filename, encoding = ' UTF-. 8 ' ) AS F: # Open File 
        Content = reached, f.read () method # read () to read the entire contents of the file 
        words = the re.findall (R & lt ' [\ W ^ -] + ' , Content) 
#findall () method can return to match substrings of the form of a list, w match a small write through z, uppercase a to Z, the numerals 0 to 9 
        jisuanTotal (words)

 Second, the functional double difficulty

Function two in a function on the basis of improved learning file open and exception handling, because previously had contact with the relevant knowledge, so for me is difficult.

def file_name (path): # achieve two functions, the input file name without the suffix 
    path = path + ' .txt ' 
    the try : 
        with Open (path, encoding = ' UTF-. 8 ' ) AS F: 
            Content = reached, f.read () 
    except FileNotFoundError: # exception handling, can not find the file, the output file does not exist 
        MSG = " of The file " + + path " does not exist. " 
        Print (MSG) 
    the else : 
        words = the re.findall (R & lt ' [\ W ^ -] + ' , Content) 
        jisuanTotal (words)

Third, the triple function difficulty

Dive into the os module is a module file directory operations, so the focus is to learn os module, it takes a long time point is how to separate the file name and extension, only the output file name, eventually solved by Baidu. os.path.splitext ( "file path") for separating the file name and extension; default return (fname, fextension) tuple

def file_floder (path): # function input to achieve three stores directory name in English works of the document 
    dirs = os.listdir (path)
     for File in dirs:
         IF os.path.isfile (File): # determine whether the file 
           print (os .path.splitext (file) [ 0 ]) 
# os.path.splitext ( "file path") for separating the file name and extension; default return (fname, fextension) tuple 
           with Open (file, encoding = ' UTF -8 ' ) AS F: 
               words = the re.findall (R & lt ' [\ W ^ -] + ' , reached, f.read ()) 
               jisuanTotal (words) 
               Print ( ' ---- ' )

 

 Fourth, the function of quad difficulty

Redirect knowledge of relevant knowledge to understanding, but still no ideas on this topic.

Five, psp

 

 Function 1: because they do not know how to package .py files as .exe files takes a long time.

Function two and three are not familiar with the function of python knowledge, too little python learning.

Function 4 Because there is no thinking, do not understand the redirection, ultimately did not solve the problem.

Guess you like

Origin www.cnblogs.com/summerkingy/p/11538265.html