stopwords.py
· 480 B · Python
Raw
def countwords(lst):
word_count = {}
for word in lst:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
def wordsOfLength(w, n):
result = []
for word, count in w.items():
if count >= n:
result.append(word)
return result
def stopWords(text, k):
l = text.split()
w = countwords(l)
results = wordsOfLength(w, k)
return results
| 1 | def countwords(lst): |
| 2 | word_count = {} |
| 3 | |
| 4 | for word in lst: |
| 5 | if word in word_count: |
| 6 | word_count[word] += 1 |
| 7 | else: |
| 8 | word_count[word] = 1 |
| 9 | return word_count |
| 10 | |
| 11 | def wordsOfLength(w, n): |
| 12 | result = [] |
| 13 | for word, count in w.items(): |
| 14 | if count >= n: |
| 15 | result.append(word) |
| 16 | return result |
| 17 | |
| 18 | def stopWords(text, k): |
| 19 | l = text.split() |
| 20 | w = countwords(l) |
| 21 | results = wordsOfLength(w, k) |
| 22 | return results |
| 23 | |
| 24 |