# Path: lingtools/corpus/subtlexreader.py
# class SubtlexUKBigramDict(dict):
# 
#     """Representation of SUBTLEX-UK bigram information."""
# 
#     def __init__(self, subtlex_path):
#         """Load the SUBTLEX database from the specified file."""
#         # Track total counts in each context, which can be useful for
#         # excluding sparse data.
#         self.context_counts = defaultdict(int)
# 
#         # Set up dict
#         dict.__init__(self)
# 
#         # Fill it in
#         try:
#             subtlex = open(subtlex_path, 'rU')
#         except IOError:
#             raise IOError("Could not open SUBTLEX database at %s." % subtlex_path)
# 
#         # DictReader would be convenient, but given the size of the
#         # file, efficiency is key. Using a standard reader appears to
#         # be 30% faster. Fields: spelling, spelling1, freq, cd,
#         # cdcount, separator, separatorfreq, separatorcd
#         subtlex_reader = csv.reader(subtlex, delimiter='\t')
#         # Skip header
#         subtlex_reader.next()
#         # Read the data
#         for word1, word2, freq, cd, cdcount, _, _, _ in subtlex_reader:
#             freq = int(freq)
#             cd = float(cd)
#             cdcount = int(cdcount)
#             self[(word1, word2)] = \
#                 SubtlexUKBigram(word1, word2, freq, cd, cdcount)
#             self.context_counts[word1] += freq
# 
# Path: lingtools/prob/probability.py
# class ConditionalFreqDist(defaultdict):
# 
#     """A conditional frequency distribution."""
# 
#     def __init__(self):
#         super(ConditionalFreqDist, self).__init__(FreqDist)


import sys
import csv
import argparse
from lingtools.corpus.subtlexreader import SubtlexUKBigramDict
from lingtools.prob.probability import ConditionalFreqDist
# Copyright 2014 Constantine Lignos
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




def bigram_info(subtlex_path, out_path, first_word, second_word):
    """Output information about bigrams."""
    # Check args
    if first_word and second_word:
        print >> sys.stderr, "Specify first or second word, not both."
        sys.exit(1)

    # Load up bigram data
    print "Reading bigrams..."
    bigrams = SubtlexUKBigramDict(subtlex_path)

    # Compute frequencies
    print "Computing frequencies..."

# Path: lingtools/corpus/subtlexreader.py
# class SubtlexUKBigramDict(dict):
# 
#     """Representation of SUBTLEX-UK bigram information."""
# 
#     def __init__(self, subtlex_path):
#         """Load the SUBTLEX database from the specified file."""
#         # Track total counts in each context, which can be useful for
#         # excluding sparse data.
#         self.context_counts = defaultdict(int)
# 
#         # Set up dict
#         dict.__init__(self)
# 
#         # Fill it in
#         try:
#             subtlex = open(subtlex_path, 'rU')
#         except IOError:
#             raise IOError("Could not open SUBTLEX database at %s." % subtlex_path)
# 
#         # DictReader would be convenient, but given the size of the
#         # file, efficiency is key. Using a standard reader appears to
#         # be 30% faster. Fields: spelling, spelling1, freq, cd,
#         # cdcount, separator, separatorfreq, separatorcd
#         subtlex_reader = csv.reader(subtlex, delimiter='\t')
#         # Skip header
#         subtlex_reader.next()
#         # Read the data
#         for word1, word2, freq, cd, cdcount, _, _, _ in subtlex_reader:
#             freq = int(freq)
#             cd = float(cd)
#             cdcount = int(cdcount)
#             self[(word1, word2)] = \
#                 SubtlexUKBigram(word1, word2, freq, cd, cdcount)
#             self.context_counts[word1] += freq
# 
# Path: lingtools/prob/probability.py
# class ConditionalFreqDist(defaultdict):
# 
#     """A conditional frequency distribution."""
# 
#     def __init__(self):
#         super(ConditionalFreqDist, self).__init__(FreqDist)


# Path: bigram_info.py
import sys
import csv
import argparse
from lingtools.corpus.subtlexreader import SubtlexUKBigramDict
from lingtools.prob.probability import ConditionalFreqDist

# Copyright 2014 Constantine Lignos
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




def bigram_info(subtlex_path, out_path, first_word, second_word):
    """Output information about bigrams."""
    # Check args
    if first_word and second_word:
        print >> sys.stderr, "Specify first or second word, not both."
        sys.exit(1)

    # Load up bigram data
    print "Reading bigrams..."
    bigrams = SubtlexUKBigramDict(subtlex_path)

    # Compute frequencies
    print "Computing frequencies..."

    freqs = ConditionalFreqDist()