Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gzip
- import glob
- import os
- #Programmed by Glob/Rekvijem/Catalyst
- base = os.getcwd()
- def findLanguage(text):
- #Get language files
- os.chdir(base+'/LanguageFiles')
- languagefiles = glob.glob('*.txt')
- #Get the sizes of the gzipped language files
- base_sizes = {}
- for lang in languagefiles:
- #Make a gzipped version of the file if it doesn't exist
- if not os.path.exists(lang+'.gz'):
- i = open(lang,'rb')
- o = gzip.open(lang+'.gz','wb')
- o.writelines(i)
- i.close()
- o.close()
- #Get the size of the file and append to dictionary
- name = lang.split('.')[0]
- base_sizes[name] = os.path.getsize(lang+'.gz')
- #Get the sizes of the gzipped language files with the text to be identified
- new_sizes = {}
- for lang in languagefiles:
- #Make a temporary file holding the gzipped data
- i = open(lang,'rb')
- o = gzip.open('temp.gz','wb')
- o.write(i.read()+text)
- o.close()
- i.close()
- #Get the size of the file
- name = lang.split('.')[0]
- new_sizes[name] = os.path.getsize('temp.gz')
- #Close the temporary file
- os.remove('temp.gz')
- #Calculate the difference in bytes of each file
- difference = {key : new_sizes[key] - base_sizes[key] for key in base_sizes.keys()}
- #Get the language(languages in a rare rare rare case) with the largest difference
- dif = min(difference.values())
- language = [k for k,v in difference.iteritems() if v == dif]
- #Return the language(s)
- return language
- #Alternatively return a string if only one language found:
- #return language[0] if len(language) == 1 else language
- print findLanguage("In friendship diminution instrument so. Son sure paid door with say them. Two among sir sorry men court.")
- print findLanguage("Gut gassen durren nun wirtin weg art hinaus neckte. Davon ri zu gutes haute in zahne wesen waren. Ja drehte arbeit wo la ja kugeln.")
- print findLanguage("Terrasse et ai etrangle te bataille dissiper musiques bravoure. Continue preparer pressait que etroites empecher habitent oui. Vit ere infiniment descendons simplement eux.")
Advertisement
Add Comment
Please, Sign In to add comment