-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from datosgobar/dev-nacho
Dev nacho
- Loading branch information
Showing
5 changed files
with
301 additions
and
32 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%load_ext memory_profiler\n", | ||
"from textar import TextClassifier\n", | ||
"import xml.etree.ElementTree as ET\n", | ||
"from lxml import etree\n", | ||
"import numpy as np\n", | ||
"import re\n", | ||
"import os" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 28, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Helper funcs\n", | ||
"\n", | ||
"def parse_blog(tree, min_words=100):\n", | ||
" dates = []\n", | ||
" posts = []\n", | ||
" for elem in tree:\n", | ||
" post = None\n", | ||
" if elem.tag == 'date':\n", | ||
" date = elem.text\n", | ||
" elif elem.tag == 'post':\n", | ||
" post = elem.text\n", | ||
" if post is not None: \n", | ||
" words = re.findall('\\w+\\W',post)\n", | ||
" if len(words) > min_words and np.mean(map(len,words))>2:\n", | ||
" dates.append(date)\n", | ||
" posts.append(post)\n", | ||
" return dates, posts" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 29, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Configs\n", | ||
"DATA_FOLDER = os.path.join('.','data','performance_data','blogs')\n", | ||
"MAX_FILES = 10000" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 30, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"magic = '''<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n", | ||
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\" [\n", | ||
" <!ENTITY nbsp ' '>\n", | ||
" ]>'''\n", | ||
"\n", | ||
"parser = etree.XMLParser(recover=True)\n", | ||
"\n", | ||
"all_dates = []\n", | ||
"all_posts = []\n", | ||
"all_genders = []\n", | ||
"all_ages = []\n", | ||
"all_categories = []\n", | ||
"\n", | ||
"for file_name in os.listdir(DATA_FOLDER)[:MAX_FILES]:\n", | ||
" id_f, gender, age, category, zodiac, ext = file_name.split('.')\n", | ||
" with open(os.path.join(DATA_FOLDER, file_name), 'r') as f:\n", | ||
" try:\n", | ||
" tree = ET.fromstring(magic + f.read(), parser=parser)\n", | ||
" dates, posts = parse_blog(tree)\n", | ||
" all_posts += posts\n", | ||
" all_dates += dates\n", | ||
" all_genders += [gender] * len(dates)\n", | ||
" all_ages += [age] * len(dates)\n", | ||
" all_categories += [category] * len(dates)\n", | ||
" except Exception as e:\n", | ||
" pass\n", | ||
" #print(\"Error en {:s}\".format(file_name))\n", | ||
"all_ids = map(str, range(len(all_posts)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 31, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%timeit\n", | ||
"# Tiempo de la creacion del objeto\n", | ||
"tc = TextClassifier(all_posts, all_ids)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 36, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"1 loop, best of 3: 2.36 s per loop\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%timeit\n", | ||
"# Tiempo de la busqueda\n", | ||
"tc.get_similar(all_ids[1],max_similars=3, term_diff_max_rank=50)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 39, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"1 loop, best of 3: 17.4 s per loop\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%timeit\n", | ||
"# Tiempo de creacion del clasificador\n", | ||
"tc.make_classifier(\"topic\",all_ids, all_categories)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 50, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"10 loops, best of 3: 31.4 ms per loop\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%timeit\n", | ||
"tc.classify(\"topic\", all_ids[1])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 18, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[ 0., 0., 0., ..., 0., 0., 0.]])" | ||
] | ||
}, | ||
"execution_count": 18, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"row.toarray()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array(['0', '1', '10', ..., '997', '998', '999'], \n", | ||
" dtype='|S4')" | ||
] | ||
}, | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python [Root]", | ||
"language": "python", | ||
"name": "Python [Root]" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.12" | ||
}, | ||
"notify_time": "5" | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
__email__ = '[email protected]' | ||
__version__ = '0.0.4' | ||
|
||
from text_classifier import TextClassifier | ||
from .text_classifier import TextClassifier |
Oops, something went wrong.