Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import snowballstemmer
  2. import re
  3.  
  4. def stemp(text):
  5.  
  6. with open('/Users/ekaterinadubcova/Documents/2.txt', 'a') as file:
  7. text = open('/Users/ekaterinadubcova/Documents/2.txt', 'w')
  8. text = text.lower() # приведение в lowercase,
  9.  
  10. text = re.sub( r'https?://[\S]+', ' url ', text) # замена интернет ссылок
  11. text = re.sub( r'[\w\./]+\.[a-z]+', ' url ', text)
  12.  
  13. text = re.sub( r'\d+[-/\.]\d+[-/\.]\d+', ' date ', text) # замена даты и времени
  14. text = re.sub( r'\d+ ?гг?', ' date ', text)
  15. text = re.sub( r'\d+:\d+(:\d+)?', ' time ', text)
  16. text = re.sub( r'<[^>]*>', ' ', text) # удаление html тегов
  17. text = re.sub( r'[\W]+', ' ', text ) # удаление лишних символов
  18.  
  19. stemmer = snowballstemmer.stemmer('russian');
  20. text = ' '.join( stemmer.stemWords( text.split() ) )
  21.  
  22. stw = ['в', 'по', 'на', 'из', 'и', 'или', 'не', 'но', 'за', 'над', 'под', 'то','a', 'at', 'on', 'of', 'and', 'or', 'in', 'for', 'at']
  23. remove = r'\b('+'|'.join(stw)+')\b'
  24. text = re.sub(remove,' ', text)
  25.  
  26. text = re.sub( r'\b\w\b', ' ', text ) # удаление отдельно стоящих букв
  27.  
  28. text = re.sub( r'\b\d+\b', ' digit ', text ) # замена цифр
  29.  
  30. file.write('input')
  31. text.close()
  32.  
  33. return text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement