<commit_before>from collections import defaultdict
from httplib import HTTPConnection
from optparse import make_option
import csv
import json
import re
import sys
import urllib2

from django.core.management.base import BaseCommand

from pymongo import Connection

import nltk
from dateutil.parser import parse as dateparse
import lxml.etree


connection = Connection()
db = connection['capitolwords']


class Command(BaseCommand):

    option_list = BaseCommand.option_list + (
            make_option('--date',
                         action='store',
                         dest='date',
                         default=None,
                         help='Date for which to generate n-grams'),
            )

    def list_dates(self):
        url = ('http://localhost:8983/solr/select'
               '?q=*:*'
               '&facet.mincount=1'
               '&rows=0'
               '&facet.field=date'
               '&wt=json'
               '&facet.method=enum'
               '&facet=true'
               '&facet.limit=1000000')

        data = json.loads(urllib2.urlopen(url).read())
        dates = [x.split('T')[0] for x in data['facet_counts']['facet_fields']['date'][::2]]
        return dates


    def make_ngrams(self, text, n):
        for graf in text:
            sentences = nltk.tokenize.sent_tokenize(graf.replace('\n', '').lower())
            for sentence in sentences:


                # Remove unnecessary punctuation
                sentence = re.sub(r"(\/|--|`|'|\(|\)\;\?)", ' ', sentence)
                sentence = re.sub(r"(\.|,)", '', sentence)

                #words = nltk.tokenize.word_tokenize(sentence)
                words = sentence.split()

                # Remove punctuation-only tokens
                words = [x for x in words
                            if re.search(r'[a-z]', x)]

                ngrams = nltk.util.ngrams(words, n)
                for ngram in ngrams:
                    yield ngram

<commit_msg>Don't replace regular apostrophes with spaces when creating ngrams<commit_after>from collections import defaultdict
from httplib import HTTPConnection
from optparse import make_option
import csv
import json
import re
import sys
import urllib2

from django.core.management.base import BaseCommand

from pymongo import Connection

import nltk
from dateutil.parser import parse as dateparse
import lxml.etree


connection = Connection()
db = connection['capitolwords']


class Command(BaseCommand):

    option_list = BaseCommand.option_list + (
            make_option('--date',
                         action='store',
                         dest='date',
                         default=None,
                         help='Date for which to generate n-grams'),
            )

    def list_dates(self):
        url = ('http://localhost:8983/solr/select'
               '?q=*:*'
               '&facet.mincount=1'
               '&rows=0'
               '&facet.field=date'
               '&wt=json'
               '&facet.method=enum'
               '&facet=true'
               '&facet.limit=1000000')

        data = json.loads(urllib2.urlopen(url).read())
        dates = [x.split('T')[0] for x in data['facet_counts']['facet_fields']['date'][::2]]
        return dates


    def make_ngrams(self, text, n):
        for graf in text:
            sentences = nltk.tokenize.sent_tokenize(graf.replace('\n', '').lower())
            for sentence in sentences:


                # Remove unnecessary punctuation
                sentence = re.sub(r"(\/|--|`|\(|\)\;\?)", ' ', sentence)
                sentence = re.sub(r"(\.|,)", '', sentence)
                sentence = re.sub(r"''", '', sentence)

                #words = nltk.tokenize.word_tokenize(sentence)
                words = sentence.split()

                # Remove punctuation-only tokens
                words = [x for x in words
                            if re.search(r'[a-z]', x)]

                ngrams = nltk.util.ngrams(words, n)
                for ngram in ngrams:
                    yield ngram


{'fraction_non_alphanumeric': 0.12042532146389713, 'fraction_numerical': 0.008407517309594461, 'mean_word_length': 1.72757923128793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3100855', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1118, 'n_words': 269}