Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. from collections import defaultdict
  2. import reverse_geocoder as rg #пакет для определения страны по координатам точки
  3. uid = ''
  4. buffer_query = ''
  5. first_char = ''
  6. local = ''
  7. gps = ''
  8. line_buf = ''
  9. russian_query = defaultdict(int)
  10. english_query = defaultdict(int)
  11. query_in_russia = []
  12. def is_ru(buf):
  13. if buf.find('ru') != -1:
  14. return True
  15. else:
  16. return False
  17. def find_country(coordinates):
  18. ans = rg.search(coordinates, mode=1)
  19. return ans[0]['cc']
  20.  
  21. def is_query(log, first_char, uid):
  22. if log[-1] != uid and uid != '':
  23. return True
  24. if log[0][1] != first_char and first_char != '':
  25. return True
  26. return False
  27. with open("test.txt") as f:
  28. for line in f:
  29. log = list(line.split(';'))
  30. b = gps.replace('[', '').replace(']', '')
  31. coordinates = list(reversed((b.split(','))))
  32. if is_query(log, first_char, uid) and len(buffer_query) > 4:
  33. if is_ru(local):
  34. russian_query[buffer_query] += 1
  35. if not is_ru(local):
  36. english_query[buffer_query] += 1
  37. if gps != "None" and gps != '':
  38. if find_country(coordinates) == "RU":
  39. query_in_russia.append(buffer_query)
  40. buffer_query = log[0]
  41. local = log[2]
  42. gps = log[3]
  43. uid = log[-1]
  44. first_char = log[0][1]
  45. log.clear()
  46. line_buf = line
  47. print(sorted(dict(russian_query).items(), key = lambda x : x[1], reverse = True))
  48. print(sorted(dict(english_query).items(), key = lambda x : x[1], reverse = True))
  49. print(query_in_russia)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement