---
# System prepended metadata

title: Python - Read File and Calculate Word Occurrence
tags: [Python]

---

### Sum-up:
1. Define a function to deal with punctuation marks.
2. Define a function to sort a dictionary by its value.
3. Read in file and store words in a dictionary.



### 1. Deal with punctuation:

First set a empty str, and then loop through the characters and add to str if each character is an alphabet or a digit.

```
def string_manipulation(s: str):
	result = ""

	for char in s:
		if char.isalpha() or char.isdigit():
			result += char

	return result
```

### 2. Sorting in a dict:

reference: https://docs.python.org/3/howto/sorting.html

use the `sorted()` method.

Loop through key and value as items(a tuple) in a dict, and then pass in the second element(which will be the value) as a sorting key parameter into sorted method.

**point: key=lambda item: item[1]**


```
def print_out_d(d: dict):
	"""
	: param d: (dict) key of type str is a word
					value of type int is the word occurrence
	---------------------------------------------------------------
	This function prints out all the info in d
	"""
	# sort dict by values descending
    
	for key, value in sorted(d.items(), key=lambda item: item[1], reverse=True):
		print(f"{key} -> {value}")
```


### 3. Manipulate the file:
```
def main():
	with open(FILE, "r") as f:
		word_dict = dict()
		for line in f:
			tokens = line.split()
			for token in tokens:
                
                              # manipulate each word to get rid of puncuation
				token = string_manipulation(token)
                
                              # add to the dict
				if token not in word_dict:
					word_dict[token] = 1
				else:
					word_dict[token] += 1

	print_out_d(word_dict)
```

