'NLP - Python - Stop Words

I had a little problem in one case in python. The cases are as follows:

"in NLP, stop words are commonly used words like "a", "is", and "the". They are typically filtered out during processing.

Implement a function that takes a string text and integer k, and returns the list of words that occur in the text at least k times. The words must be returned in the order of their first occurrence in the text."

And here's my code:

#!/bin/python3

import math
import os
import random
import re
import sys

def stopWords(text, k):
    stop_words = ['and','fox','jumps','over','dog','runs','away','to','a','house','lazy','quick']
    text = text.split()
    text = [word for word in text if word not in stop_words]
    text = [word for word in text if len(word) > k]
    return text

if _name_ == '_main_':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    text = input()

    k = int(input().strip())

    result = stopWords(text, k)

    fptr.write('\n'.join(result))
    fptr.write('\n')

    fptr.close()

Here is my input:

Input
text = the quick brown fox jumps over the lazy dog runs away a brown house
k = 2

I want output like this:

Output:
the
brown

but my result is:

Output:
the
brown
the
brown
brown

how to fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source