title: Problem during replacing word/phrases from a text file using a set in python?
body: <p>Suppose I am having a list (<code>new_list</code>) with 3000 sentences, where each sentences are separated by a comma mark (<code>,</code>). </p>
  
  <p>Example (a part): </p>
  
  <pre><code>new_list = ['air purity controller, to detect pollution and letting cold air in', 'air quality in my home by air conditioning', 'air conditioner depending on home', 'household alarm clock for time']
  </code></pre>
  
  <p>I want to replace certain words (single word or phrase) from the <code>new_list</code> by adding some special characters (at the start and end). I am doing this by the help of a set.</p>
  
  <p>Example of the set:</p>
  
  <pre><code>dict = {'air conditioner', 'air', 'air quality', 'house', 'air conditioning', 'alarm clock'}
  </code></pre>
  
  <p>The size of the set (dict) is 317. I want to scan each word of the <code>new_list</code> and replace when there is a match with the set by appending special characters at the starting and end position. Further if a match occur and the resulted word is a phrase from the set, then additionally it would add a special character (_) in between along with appending special character at the both starting and end points.</p>
  
  <p>I have tried but failing. Please suggest where I am going wrong (i don't think so, I am wrong). The <code>new_list</code> and dict are shown above.</p>
  
  <pre><code>import re, csv, nltk
  from nltk.corpus import stopwords
  from nltk import regexp_tokenize
  
  with open("raw_data.txt", 'r', encoding = 'utf-8') as f1:
     reader = csv.reader(f1, skipinitialspace=True)
     new_list = next(reader)
     with open('updatd_file.txt', 'w', encoding='utf-8') as f2:
        dic = {'air conditioner', 'air quality', 'air conditioning', 'air', 'house', 'alarm clock'}
        dic = {i : i.replace(' ', '_') for i in dic} 
        pattern = re.compile(r"\b("+"|".join(dic)+r")\b")
        modify_reqs = [pattern.sub(lambda x: "_{}_".format(dic[x.group()]), i) for i in new_list]
        sw = (stopwords.words('english'))
        unfiltered_tokens = [[word for word in regexp_tokenize(word, pattern=r"\s|[\d]|[^\wa-z+]", gaps=True) if word not in sw] for word in modify_reqs]
        f2.write(str(unfiltered_tokens))
  </code></pre>
  
  <p>I am executing this program and writing the results onto a file. When I check the output file, I can see the words in the desired order (missing few words) but sometimes i am unable to see this. How is this strange behavior, I am unable to understand and explore. </p>
  
  <p>That is sometimes, I am able to find the phrase in the correct order (as expected) that is <code>'_air_conditioning_'</code> but next time when I execute this fragment, i find the same word as <code>'_air_'</code>, <code>'conditioning'</code> (separated). Same thing also happened with the other phrases like <code>air quality</code>, <code>air conditioning</code>, etc. The problem is with the phrases not with the single word. 
  Please note that in the set (dict) I have 317 words and <code>new_list</code> containing almost 3000 sentences. Not possible to show all here.
  How is this possible? I am trying this since 7-8 days its frustrating now. </p>