<commit_before>'''
Created on Mar 17, 2017

@author: tonyq
'''

import pandas as pd
import numpy as np
import re, sys
from bs4 import BeautifulSoup
import logging
from keras.preprocessing.sequence import pad_sequences
from tqdm._tqdm import tqdm
from nltk.tokenize import word_tokenize
from numpy import array, zeros
import operator

logger = logging.getLogger(__name__)

uri_re = r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'

def stripTagsAndUris(x):
	if x:
		# BeautifulSoup on content
		soup = BeautifulSoup(x, "html.parser")
		# Stripping all <code> tags with their content if any
		if soup.code:
			soup.code.decompose()
		# Get all the text out of the html
		text = soup.get_text()
		# Returning text stripping out all uris
		return re.sub(uri_re, "", text)
	else:
		return ""
	
def get_words(text):
# 	word_split = re.compile('[^a-zA-Z0-9_\\+\\-]')
# 	return [word.strip().lower() for word in word_split.split(text)]
	text = text.replace('’s', ' ’s')
	text = text.replace('…', ' ')
	text = text.replace('”', ' ')
	text = text.replace('“', ' ')
	text = text.replace('‘', ' ')
	text = text.replace('’', ' ')
	text = text.replace('"', ' ')
	text = text.replace("'", " ")
	text = text.replace('-', ' ')
	text = text.replace('/', ' ')
	text = text.replace("\\", " ")
	return word_tokenize(text)
	
def get_pdTable(path, notag=False):
	logger.info(' Processing pandas csv ')
	pdtable = pd.read_csv(path)
	if notag:
		return pdtable.id, pdtable.question1, pdtable.question2
	else:
		return pdtable.id, pdtable.question1, pdtable.question2, pdtable.is_duplicate

def tokenizeIt(table, clean=False):
	tokenizedTable = []
	maxLen = 0
	for text in tqdm(table, file=sys.stdout):
		if clean:
# 			text = stripTagsAndUris(text)
			text = get_words(text)
			tokenizedTable.append(text)<commit_msg>minor bug fix for text processing<commit_after>'''
Created on Mar 17, 2017

@author: tonyq
'''

import pandas as pd
import numpy as np
import re, sys
from bs4 import BeautifulSoup
import logging
from keras.preprocessing.sequence import pad_sequences
from tqdm._tqdm import tqdm
from nltk.tokenize import word_tokenize
from numpy import array, zeros
import operator

logger = logging.getLogger(__name__)

uri_re = r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'

def stripTagsAndUris(x):
	if x:
		# BeautifulSoup on content
		soup = BeautifulSoup(x, "html.parser")
		# Stripping all <code> tags with their content if any
		if soup.code:
			soup.code.decompose()
		# Get all the text out of the html
		text = soup.get_text()
		# Returning text stripping out all uris
		return re.sub(uri_re, "", text)
	else:
		return ""
	
def get_words(text):
# 	word_split = re.compile('[^a-zA-Z0-9_\\+\\-]')
# 	return [word.strip().lower() for word in word_split.split(text)]
	text = str(text)
	text = text.replace('’s', ' ’s')
	text = text.replace('…', ' ')
	text = text.replace('”', ' ')
	text = text.replace('“', ' ')
	text = text.replace('‘', ' ')
	text = text.replace('’', ' ')
	text = text.replace('"', ' ')
	text = text.replace("'", " ")
	text = text.replace('-', ' ')
	text = text.replace('/', ' ')
	text = text.replace("\\", " ")
	return word_tokenize(text)
	
def get_pdTable(path, notag=False):
	logger.info(' Processing pandas csv ')
	pdtable = pd.read_csv(path)
	if notag:
		return pdtable.id, pdtable.question1, pdtable.question2
	else:
		return pdtable.id, pdtable.question1, pdtable.question2, pdtable.is_duplicate

def tokenizeIt(table, clean=False):
	tokenizedTable = []
	maxLen = 0
	for text in tqdm(table, file=sys.stdout):
		if clean:
# 			text = stripTagsAndUris(text)
			text = get_words(text)
			tokenizedTable.append(text)
{'fraction_non_alphanumeric': 0.2158624229979466, 'fraction_numerical': 0.01026694045174538, 'mean_word_length': 4.237903225806452, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '887225', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1500, 'n_words': 340}