-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_coverage_data.py
70 lines (51 loc) · 1.96 KB
/
text_coverage_data.py
1
2
3
4
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This script will create the two variables to be used for the coverage problem.
1 -- A universe of words (or elements) to be covered (~29,000)
The elements are contained in the wds_universe set variable.
2 -- A universe of sets to choose from to cover the elements (~55,000)
The sets are contained in the sets_universe list variable.
Running this script should generate the required data. It is recommended to use
the nltk.download() method to retrieve the data in order to automatically
generate the file paths required to run the respective nltk methods.
This script requires the NLP package nltk is downloaded.
"""
import nltk
# nltk.download()
# Using the pop-up window, download the
# "reuters" corpus in the corpora tab if not already installed
from nltk.corpus import reuters
def set_processing(list_set):
cleaned_list = []
i = 0
while i < len(list_set):
if not any(char.isdigit() for char in list_set[i]):
if punctuation_check(list_set[i]):
cleaned_list.append(list_set[i].lower())
i += 1
return set(cleaned_list)
def punctuation_check(inp_string):
return \
"." not in inp_string and \
"'" not in inp_string and \
len(inp_string) > 1
def sentence_processing(sentences):
all_sets = []
for i in range(len(sentences)):
sentence_set = sentences[i]
all_sets.append(set_processing(sentence_set))
return all_sets
# PART 1
# Creating a set of all words representing my universe
wds = set(reuters.words())
list_set = list(wds)
# wds_universe represents all elements that can be "covered"
wds_universe = set_processing(list_set)
# PART 2
# Creating the "sets" from sentences
sentences = reuters.sents()
# paragraphs=reuters.paras()
# Which ever of the above two variables (sentences or paragraphs) I pass to the
# sentenceProcessing function becomes my sets to choose.
sets_universe = sentence_processing(sentences)