import  codecs
import os
import math
import string
import re
import glob
import sys

No_categories = []
fap=[]
y=codecs.open("stem-based mutiple annotation.txt",'r', encoding = 'utf-8');
rtwo=y.readlines();
y.close()
filenames= os.listdir (".") # get all files' and folders' names in the current directory
for filename in filenames: # loop through all the files and folders
    
    if os.path.isdir(os.path.join(os.path.abspath("."), filename)): # check whether the current object is a folder or not
        No_categories.append(filename)
        #print(os.path.join(os.path.abspath("."), filename))
        m=os.path.join(os.path.abspath("."), filename)
        l=r'm'
        for file in os.listdir(os.path.join(os.path.abspath("."), filename)):
            #print(file)
            if file.endswith('.txt'):
                   x= os.path.join(m, file)#path of each dcoument
                   #print(x)
                   #k=codecs.open(x,'r',encoding ='utf-8');
                   k=codecs.open(x,'r', encoding = 'utf-8');
                   rfile=k.read();
                   fs1=rfile.split();
                   #k.close();
                   u=codecs.open(x,'w','UTF-8');
                   #print(fs1)
                   xc=[];
                   for w in fs1:
                       #print(w)
                       for line in rtwo:
                           fx=line.split('/');
                           if(w.strip()==fx[0].strip()):
                               proc_word=w.replace(w,fx[1]);
                               sp=proc_word.split('\r');
                               xc.append(sp[0]);
                               u.write(sp[0] );
                               u.write(' ');
                               break;                           
                                
                   u.close()