Created
January 1, 2019 19:29
-
-
Save bluedistro/3761183f2bd808420105cfa45640aba8 to your computer and use it in GitHub Desktop.
scoring reviews
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def score_review(source=None, file_type=None): | |
''' | |
source: the text, as either a string or a list of strings | |
file_type: (str): indicating whether we expecting a file containing the | |
text data or a directory containing a list files holding the text | |
options: 'file' or 'dir' | |
''' | |
text_list = list() | |
if isinstance(source, str) and file_type is None: | |
text_list.append(source) | |
decoded_review, data = decode_review(text_list) | |
# make prediction | |
score = model.predict(data)[0][0] | |
review_rating(score, decoded_review) | |
if isinstance(source, list) and file_type is None: | |
for item in source: | |
text_list = list() | |
text_list.append(item) | |
decoded_review, data = decode_review(text_list) | |
score = model.predict(data)[0][0] | |
review_rating(score, decoded_review) | |
if isinstance(source, str) and file_type == 'file': | |
file_data = open(source).read() | |
text_list.append(file_data) | |
decoded_review, data = decode_review(text_list) | |
# make prediction | |
score = model.predict(data)[0][0] | |
review_rating(score, decoded_review) | |
if isinstance(source, str) and file_type == 'dir': | |
file_content_holder = list() | |
for fname in os.listdir(source): | |
if fname[-4:] == '.txt': | |
f = open(os.path.join(source, fname)) | |
file_content_holder.append(f.read()) | |
f.close() | |
for item in file_content_holder: | |
text_list = list() | |
text_list.append(item) | |
decoded_review, data = decode_review(text_list) | |
score = model.predict(data)[0][0] | |
review_rating(score, decoded_review) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment