Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. % function to create a vocabulary from multiple text files under folders
  2.  
  3. function voc = buildVoc(folder, voc)
  4.  
  5. stopword = {'ourselves', 'hers', 'between', 'yourself', 'but', 'again', 'there', ...
  6. 'about', 'once', 'during', 'out', 'very', 'having', 'with', 'they', 'own', ...
  7. 'an', 'be', 'some', 'for', 'do', 'its', 'yours', 'such', 'into', ...
  8. 'of', 'most', 'itself', 'other', 'off', 'is', 's', 'am', 'or', ...
  9. 'who', 'as', 'from', 'him', 'each', 'the', 'themselves', 'until', ...
  10. 'below', 'are', 'we', 'these', 'your', 'his', 'through', 'don', 'nor', ...
  11. 'me', 'were', 'her', 'more', 'himself', 'this', 'down', 'should', 'our', ...
  12. 'their', 'while', 'above', 'both', 'up', 'to', 'ours', 'had', 'she', 'all', ...
  13. 'no', 'when', 'at', 'any', 'before', 'them', 'same', 'and', 'been', 'have', ...
  14. 'in', 'will', 'on', 'does', 'yourselves', 'then', 'that', 'because', ...
  15. 'what', 'over', 'why', 'so', 'can', 'did', 'not', 'now', 'under', 'he', ...
  16. 'you', 'herself', 'has', 'just', 'where', 'too', 'only', 'myself', ...
  17. 'which', 'those', 'i', 'after', 'few', 'whom', 't', 'being', 'if', ...
  18. 'theirs', 'my', 'against', 'a', 'by', 'doing', 'it', 'how', ...
  19. 'further', 'was', 'here', 'than'}; % define English stop words, from NLTK
  20.  
  21.  
  22. files = dir(fullfile(folder,'*.txt'));
  23.  
  24. for file = files'
  25. [fid, msg] = fopen(fullfile(folder,file.name), 'rt');
  26. error(msg);
  27. line = fgets(fid); % Get the first line from the file.
  28. while line ~= -1
  29. %PUT YOUR IMPLEMENTATION HERE
  30. % get first word
  31. [token, remain] = strtok(line);
  32. cont = 1;
  33. count = 0;
  34. while (cont && count ~= 100)
  35. % lowercase the string
  36. token = lower(token);
  37. % remove punctuation from word
  38. token = regexprep(token, '[*/\)(-!,.?]', '');
  39. % assume there is no more remainder
  40. cont = 0;
  41. % if string contains numbers
  42. if (contains(token, {'1','2','3','4','5','6','7','8','9','0'}))
  43. if (~strcmp(remain, ''))
  44. % get the next word
  45. [token, remain] = strtok(remain);
  46. % repeat the loop
  47. cont = 1;
  48. end
  49. % if string contains bullshit
  50. elseif (contains(token, 'â'))
  51. if (~strcmp(remain, ''))
  52. % get the next word
  53. [token, remain] = strtok(remain);
  54. % repeat the loop
  55. cont = 1;
  56. end
  57. % if string is empty
  58. elseif (strcmp(token, ''))
  59. if (~strcmp(remain, ''))
  60. % get the next word
  61. [token, remain] = strtok(remain);
  62. % repeat the loop
  63. cont = 1;
  64. end
  65. % if string is stop word
  66. elseif (ismember({token}, stopword))
  67. if (~strcmp(remain, ''))
  68. % get the next word
  69. [token, remain] = strtok(remain);
  70. % repeat the loop
  71. cont = 1;
  72. end
  73. else
  74. voc{end + 1} = char(token);
  75. % if there is a remainder
  76. if (~strcmp(remain, ''))
  77. % get the next word
  78. [token, remain] = strtok(remain);
  79. % repeat the loop
  80. cont = 1;
  81. end
  82. end
  83. count = count + 1;
  84. end
  85. % get the next line
  86. line = fgets(fid);
  87. end
  88. % close the file
  89. fclose(fid);
  90. end
  91.  
  92. voc = unique(voc);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement