Guest User

Untitled

a guest
May 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import re\n",
  10. "\n",
  11. "\n",
  12. "def clean_text(text):\n",
  13. " \"\"\"\n",
  14. " Applies some pre-processing on the given text.\n",
  15. "\n",
  16. " Steps :\n",
  17. " - Removing HTML tags\n",
  18. " - Removing punctuation\n",
  19. " - Lowering text\n",
  20. " \"\"\"\n",
  21. " \n",
  22. " # remove HTML tags\n",
  23. " text = re.sub(r'<.*?>', '', text)\n",
  24. " \n",
  25. " # remove the characters [\\], ['] and [\"]\n",
  26. " text = re.sub(r\"\\\\\", \"\", text) \n",
  27. " text = re.sub(r\"\\'\", \"\", text) \n",
  28. " text = re.sub(r\"\\\"\", \"\", text) \n",
  29. " \n",
  30. " # convert text to lowercase\n",
  31. " text = text.strip().lower()\n",
  32. " \n",
  33. " # replace punctuation characters with spaces\n",
  34. " filters='!\"\\'#$%&()*+,-./:;<=>?@[\\\\]^_`{|}~\\t\\n'\n",
  35. " translate_dict = dict((c, \" \") for c in filters)\n",
  36. " translate_map = str.maketrans(translate_dict)\n",
  37. " text = text.translate(translate_map)\n",
  38. "\n",
  39. " return text"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": 2,
  45. "metadata": {},
  46. "outputs": [
  47. {
  48. "data": {
  49. "text/plain": [
  50. "['this', 'is', 'not', 'a', 'sentence']"
  51. ]
  52. },
  53. "execution_count": 2,
  54. "metadata": {},
  55. "output_type": "execute_result"
  56. }
  57. ],
  58. "source": [
  59. "clean_text(\"<div>This is not a sentence.<\\div>\").split()"
  60. ]
  61. }
  62. ],
  63. "metadata": {
  64. "kernelspec": {
  65. "display_name": "Python 3",
  66. "language": "python",
  67. "name": "python3"
  68. },
  69. "language_info": {
  70. "codemirror_mode": {
  71. "name": "ipython",
  72. "version": 3
  73. },
  74. "file_extension": ".py",
  75. "mimetype": "text/x-python",
  76. "name": "python",
  77. "nbconvert_exporter": "python",
  78. "pygments_lexer": "ipython3",
  79. "version": "3.6.4"
  80. }
  81. },
  82. "nbformat": 4,
  83. "nbformat_minor": 2
  84. }
Add Comment
Please, Sign In to add comment