WordTokenizer.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. from __future__ import annotations
  2. from abc import ABC, abstractmethod
  3. from typing import Iterable
  4. ENGLISH_STOP_WORDS = [
  5. "!",
  6. '"',
  7. "''",
  8. "``",
  9. "#",
  10. "$",
  11. "%",
  12. "&",
  13. "'",
  14. "(",
  15. ")",
  16. "*",
  17. "+",
  18. ",",
  19. "-",
  20. ".",
  21. "/",
  22. ":",
  23. ";",
  24. "<",
  25. "=",
  26. ">",
  27. "?",
  28. "@",
  29. "[",
  30. "\\",
  31. "]",
  32. "^",
  33. "_",
  34. "`",
  35. "{",
  36. "|",
  37. "}",
  38. "~",
  39. "a",
  40. "about",
  41. "above",
  42. "across",
  43. "after",
  44. "afterwards",
  45. "again",
  46. "against",
  47. "ain",
  48. "all",
  49. "almost",
  50. "alone",
  51. "along",
  52. "already",
  53. "also",
  54. "although",
  55. "always",
  56. "am",
  57. "among",
  58. "amongst",
  59. "amoungst", # codespell:ignore
  60. "amount",
  61. "an",
  62. "and",
  63. "another",
  64. "any",
  65. "anyhow",
  66. "anyone",
  67. "anything",
  68. "anyway",
  69. "anywhere",
  70. "are",
  71. "aren",
  72. "around",
  73. "as",
  74. "at",
  75. "back",
  76. "be",
  77. "became",
  78. "because",
  79. "become",
  80. "becomes",
  81. "becoming",
  82. "been",
  83. "before",
  84. "beforehand",
  85. "behind",
  86. "being",
  87. "below",
  88. "beside",
  89. "besides",
  90. "between",
  91. "beyond",
  92. "bill",
  93. "both",
  94. "bottom",
  95. "but",
  96. "by",
  97. "call",
  98. "can",
  99. "cannot",
  100. "cant", # codespell:ignore
  101. "co",
  102. "con",
  103. "could",
  104. "couldn",
  105. "couldnt",
  106. "cry",
  107. "d",
  108. "de",
  109. "describe",
  110. "detail",
  111. "did",
  112. "didn",
  113. "do",
  114. "does",
  115. "doesn",
  116. "doing",
  117. "don",
  118. "done",
  119. "down",
  120. "due",
  121. "during",
  122. "each",
  123. "eg",
  124. "eight",
  125. "either",
  126. "eleven",
  127. "else",
  128. "elsewhere",
  129. "empty",
  130. "enough",
  131. "etc",
  132. "even",
  133. "ever",
  134. "every",
  135. "everyone",
  136. "everything",
  137. "everywhere",
  138. "except",
  139. "few",
  140. "fifteen",
  141. "fifty",
  142. "fill",
  143. "find",
  144. "fire",
  145. "first",
  146. "five",
  147. "for",
  148. "former",
  149. "formerly",
  150. "forty",
  151. "found",
  152. "four",
  153. "from",
  154. "front",
  155. "full",
  156. "further",
  157. "get",
  158. "give",
  159. "go",
  160. "had",
  161. "hadn",
  162. "has",
  163. "hasn",
  164. "hasnt",
  165. "have",
  166. "haven",
  167. "having",
  168. "he",
  169. "hence",
  170. "her",
  171. "here",
  172. "hereafter",
  173. "hereby",
  174. "herein",
  175. "hereupon",
  176. "hers",
  177. "herself",
  178. "him",
  179. "himself",
  180. "his",
  181. "how",
  182. "however",
  183. "hundred",
  184. "i",
  185. "ie",
  186. "if",
  187. "in",
  188. "inc",
  189. "indeed",
  190. "interest",
  191. "into",
  192. "is",
  193. "isn",
  194. "it",
  195. "its",
  196. "itself",
  197. "just",
  198. "keep",
  199. "last",
  200. "latter",
  201. "latterly",
  202. "least",
  203. "less",
  204. "ll",
  205. "ltd",
  206. "m",
  207. "ma",
  208. "made",
  209. "many",
  210. "may",
  211. "me",
  212. "meanwhile",
  213. "might",
  214. "mightn",
  215. "mill",
  216. "mine",
  217. "more",
  218. "moreover",
  219. "most",
  220. "mostly",
  221. "move",
  222. "much",
  223. "must",
  224. "mustn",
  225. "my",
  226. "myself",
  227. "name",
  228. "namely",
  229. "needn",
  230. "neither",
  231. "never",
  232. "nevertheless",
  233. "next",
  234. "nine",
  235. "no",
  236. "nobody",
  237. "none",
  238. "noone", # codespell:ignore
  239. "nor",
  240. "not",
  241. "nothing",
  242. "now",
  243. "nowhere",
  244. "o",
  245. "of",
  246. "off",
  247. "often",
  248. "on",
  249. "once",
  250. "one",
  251. "only",
  252. "onto",
  253. "or",
  254. "other",
  255. "others",
  256. "otherwise",
  257. "our",
  258. "ours",
  259. "ourselves",
  260. "out",
  261. "over",
  262. "own",
  263. "part",
  264. "per",
  265. "perhaps",
  266. "please",
  267. "put",
  268. "rather",
  269. "re",
  270. "s",
  271. "same",
  272. "see",
  273. "seem",
  274. "seemed",
  275. "seeming",
  276. "seems",
  277. "serious",
  278. "several",
  279. "shan",
  280. "she",
  281. "should",
  282. "shouldn",
  283. "show",
  284. "side",
  285. "since",
  286. "sincere",
  287. "six",
  288. "sixty",
  289. "so",
  290. "some",
  291. "somehow",
  292. "someone",
  293. "something",
  294. "sometime",
  295. "sometimes",
  296. "somewhere",
  297. "still",
  298. "such",
  299. "system",
  300. "t",
  301. "take",
  302. "ten",
  303. "than",
  304. "that",
  305. "the",
  306. "their",
  307. "theirs",
  308. "them",
  309. "themselves",
  310. "then",
  311. "thence",
  312. "there",
  313. "thereafter",
  314. "thereby",
  315. "therefore",
  316. "therein",
  317. "thereupon",
  318. "these",
  319. "they",
  320. "thick",
  321. "thin",
  322. "third",
  323. "this",
  324. "those",
  325. "though",
  326. "three",
  327. "through",
  328. "throughout",
  329. "thru",
  330. "thus",
  331. "to",
  332. "together",
  333. "too",
  334. "top",
  335. "toward",
  336. "towards",
  337. "twelve",
  338. "twenty",
  339. "two",
  340. "un",
  341. "under",
  342. "until",
  343. "up",
  344. "upon",
  345. "us",
  346. "ve",
  347. "very",
  348. "via",
  349. "was",
  350. "wasn", # codespell:ignore
  351. "we",
  352. "well",
  353. "were",
  354. "weren",
  355. "what",
  356. "whatever",
  357. "when",
  358. "whence",
  359. "whenever",
  360. "where",
  361. "whereafter",
  362. "whereas",
  363. "whereby",
  364. "wherein",
  365. "whereupon",
  366. "wherever",
  367. "whether",
  368. "which",
  369. "while",
  370. "whither",
  371. "who",
  372. "whoever",
  373. "whole",
  374. "whom",
  375. "whose",
  376. "why",
  377. "will",
  378. "with",
  379. "within",
  380. "without",
  381. "won",
  382. "would",
  383. "wouldn",
  384. "y",
  385. "yet",
  386. "you",
  387. "your",
  388. "yours",
  389. "yourself",
  390. "yourselves",
  391. ]
  392. class WordTokenizer(ABC):
  393. @abstractmethod
  394. def set_vocab(self, vocab: Iterable[str]):
  395. pass
  396. @abstractmethod
  397. def get_vocab(self, vocab: Iterable[str]):
  398. pass
  399. @abstractmethod
  400. def tokenize(self, text: str, **kwargs) -> list[int]:
  401. pass
  402. @abstractmethod
  403. def save(self, output_path: str):
  404. pass
  405. @staticmethod
  406. @abstractmethod
  407. def load(input_path: str):
  408. pass