translate.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. from .tika import doTranslate1, callServer, Translator, ServerEndpoint
  19. def from_file(filename, srcLang, destLang, serverEndpoint=ServerEndpoint):
  20. '''
  21. Traslates the content of source file to destination language
  22. :param filename: file whose contents needs translation
  23. :param srcLang: name of language of input file
  24. :param destLang: name of language of desired language
  25. :param serverEndpoint: Tika server end point (Optional)
  26. :return: translated content
  27. '''
  28. jsonOutput = doTranslate1(srcLang+':'+destLang, filename, serverEndpoint)
  29. return jsonOutput[1]
  30. def from_buffer(string, srcLang, destLang, serverEndpoint=ServerEndpoint):
  31. '''
  32. Translates content from source language to desired destination language
  33. :param string: input content which needs translation
  34. :param srcLang: name of language of the input content
  35. :param destLang: name of the desired language for translation
  36. :param serverEndpoint:
  37. :return:
  38. '''
  39. status, response = callServer('put', ServerEndpoint, '/translate/all/'+Translator+'/'+srcLang+'/'+destLang,
  40. string, {'Accept': 'text/plain'}, False)
  41. return response
  42. def auto_from_file(filename, destLang, serverEndpoint=ServerEndpoint):
  43. '''
  44. Translates contents of a file to desired language by auto detecting the source language
  45. :param filename: file whose contents needs translation
  46. :param destLang: name of the desired language for translation
  47. :param serverEndpoint: Tika server end point (Optional)
  48. :return:
  49. '''
  50. jsonOutput = doTranslate1(destLang, filename, serverEndpoint)
  51. return jsonOutput[1]
  52. def auto_from_buffer(string, destLang, serverEndpoint=ServerEndpoint):
  53. '''
  54. Translates content to desired language by auto detecting the source language
  55. :param string: input content which needs translation
  56. :param destLang: name of the desired language for translation
  57. :param serverEndpoint: Tika server end point (Optional)
  58. :return:
  59. '''
  60. status, response = callServer('put', ServerEndpoint, '/translate/all/'+Translator+'/'+destLang,
  61. string, {'Accept': 'text/plain'}, False)
  62. return response