import os
import sys
import nltk
import nltk.corpus
from nltk.text import Text

wordlist=open('antconc_wordlist_raw.txt','r',encoding='utf-8').readlines()

'''
car.txt
ChineseFestival.txt
cosmetics.txt
CSabstracts.txt
MedicalBeauty.txt

['car.txt','ChineseFestival.txt','cosmetics.txt','CSabstracts.txt','MedicalBeauty.txt']

'''

'''
for i in ['car.txt','ChineseFestival.txt','cosmetics.txt','CSabstracts.txt','MedicalBeauty.txt']:
    a=open(i,'r',encoding='utf-8')
    b=Text(nltk.tokenize.WhitespaceTokenizer().tokenize(a.read()))
    os.mkdir(i.replace('.txt',''))
    os.chdir(i.replace('.txt',''))
    for j in wordlist:        
        filename=i+'_'+j.replace('\n','')+'.txt'
        saveout=sys.stdout
        c=open(filename,'w',encoding='utf-8')
        sys.stdout=c
        b.concordance(j.replace('\n',''),lines=len(b))
        sys.stdout=saveout
    c.close()
    os.chdir('..\\')
'''


for i in ['car.txt','ChineseFestival.txt','cosmetics.txt','CSabstracts.txt','MedicalBeauty.txt']:
    a=open(i,'r',encoding='utf-8')
    b=Text(nltk.tokenize.WhitespaceTokenizer().tokenize(a.read()))
    os.mkdir(i.replace('.txt',''))
    os.chdir(i.replace('.txt',''))
    for j in wordlist:        
        filename=i+'_'+j.replace('\n','')+'.txt'        
        c=open(filename,'a',encoding='utf-8')        
        d=b.concordance_list(j.replace('\n',''))
        for k in range(len(d)-1):
            c.write(str(len(d))+'\n'+d[k].left_print+'\t'+d[k].query+'\t'+d[k].right_print+'\n')            
    c.close()
    os.chdir('..\\')

