Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:15:19+00:00 2026-06-03T13:15:19+00:00

Assume I have a string text = A compiler translates code from a source

  • 0

Assume I have a string text = "A compiler translates code from a source language". I want to do two things:

  1. I need to iterate through each word and stem using the NLTK library. The function for stemming is PorterStemmer().stem_word(word). We have to pass the argument ‘word’. How can I stem each word and get back the stemmed sentence?

  2. I need to remove certain stop words from the text string. The list containing the stop words is stored in a text file (space separated)

    stopwordsfile = open('c:/stopwordlist.txt','r+')
    stopwordslist=stopwordsfile.read()
    

    How can I remove those stop words from text and get a cleaned new string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:15:24+00:00Added an answer on June 3, 2026 at 1:15 pm

    I posted this as a comment, but thought I might as well flesh it out into a full answer with some explanation:

    You want to use str.split() to split the string into words, and then stem each word:

    for word in text.split(" "):
        PorterStemmer().stem_word(word)
    

    As you want to get a string of all the stemmed words together, it’s trivial to then join these stems back together. To do this easily and efficiently we use str.join() and a generator expression:

    " ".join(PorterStemmer().stem_word(word) for word in text.split(" "))
    

    Edit:

    For your other problem:

    with open("/path/to/file.txt") as f:
        words = set(f)
    

    Here we open the file using the with statement (which is the best way to open files, as it handles closing them correctly, even on exceptions, and is more readable) and read the contents into a set. We use a set as we don’t care about the order of the words, or duplicates, and it will be more efficient later. I am presuming one word per line – if this isn’t the case, and they are comma separated, or whitespace separated then using str.split() as we did before (with appropriate arguments) is probably a good plan.

    stems = (PorterStemmer().stem_word(word) for word in text.split(" "))
    " ".join(stem for stem in stems if stem not in words)
    

    Here we use the if clause of a generator expression to ignore words that are in the set of words we loaded from a file. Membership checks on a set are O(1), so this should be relatively efficient.

    Edit 2:

    To remove the words before they are stemmed, it’s even simpler:

    " ".join(PorterStemmer().stem_word(word) for word in text.split(" ") if word not in words)
    

    The removal of the given words is simply:

    filtered_words = [word for word in unfiltered_words if not in set_of_words_to_filter]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

assume I have a string 1,2,3,4 Now I want to replace, e.g. the 3rd
Assume we have a string like the following : ,34,23,4,5,634,23,12,5,4,3,1234,23,54,,,,,,,123,43,2,3,4,5,3424,,,,,,,,123,,,1234,,,,,,,45,,,56 How can we convert
Let's assume I have the string NSString* myString = @Hello,; How can I remove
Assume I have the following string: Hellotoevryone<img height=115 width=150 alt= src=/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg />Iamsogladtoseeall. This
Assume I have a class that looks like this: class Sample { public string
I have a list of words (assume they are stored in String[] if you
I have a List of Strings I need to store locally (assume the list
Assume I have two base classes, Container and Gizmo . Class Container has an
I need to pass a url string from the view to the controller, a
I'm using VS 2008 and need to read text files that have UTF-8 Chinese

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.