import glob
import os
import json
import paralleldots
api_key = "iyuDsKbgZSsCxVj6Moe37MzdqCUuvkpf33t6qS3X3cH"
paralleldots.set_api_key(api_key)
output = input(r"C:\Users\User\Desktop\trial")
txt_files = os.path.join("D:\\english\\stou\\check", '*.txt')
dic = {} #dictionary for storing list elements
res = {} #results dictionary for api output
def for_high (list_e): #defining a function for taking list elements and putting them into dic
no_keys = list_e / 20
dic = {
1: l[1:21]
}
i = 1
start_key_value = 21
while i <= no_keys:
i += 1
end_key_value = start_key_value + 20
dic.update({i: ''.join(l[start_key_value:end_key_value])})
start_key_value += 20
for x in dic: #creating a for loop for getting and saving the output and creating a new file
output = paralleldots.emotion(dic.get(x))
res.update({x: output})
with open(os.path.join("C:\\Users\\User\\Desktop\\trial", filename), 'w') as text_file:
text_file.write(json.dumps(res))
for txt_file in glob.glob(txt_files): # for loop for going through all the text files in the input directory
with open(txt_file, "r") as input_file:
filename = os.path.splitext(os.path.basename(txt_file))[0] + '.txt'
l = [l for l in input_file.readlines() if l.strip()]
list_e = int(len(l)) #no. of list elements variable
for txt_file in glob.glob(txt_files): # for loop for going through all the text files in the input directory
with open(txt_file, "r") as input_file:
filename = os.path.splitext(os.path.basename(txt_file))[0] + '.txt'
l = [l for l in input_file.readlines() if l.strip()]
list_e = int(len(l)) #no. of list elements variable
if list_e > 20: #checking if list has more than 20 elements
if list_e % 2 != 0: #checking if list has an odd no. of elements
list_e += 1
for_high(list_e)
else:
for_high(list_e)
else:
in_txt = paralleldots.emotion(l)
filename = os.path.splitext(os.path.basename(txt_file))[0] + '.txt'
with open(os.path.join("C:\\Users\\User\\Desktop\\trial", filename), 'w') as text_file:
text_file.write(str(in_txt))