In [1]:
# ---- IMPORT MODULES
from PyDictionary import PyDictionary;
dictionary=PyDictionary();

In [2]:
def GetInfoOnWord( wd, fVerbose = 0):
    if (fVerbose):
        print "\nprocesing word .... ", wd;
    mng = dictionary.meaning(wd);
    syn = dictionary.synonym(wd);
    ant = dictionary.antonym(wd);
    wordInfo = { "word" : wd, "partOfSpeech": "Unknown", "meaning": ""};
    if (mng != None):
        mngList = mng.items();
        if (mngList != None):
            (k, v) = mng.items()[0];
            wordInfo["partOfSpeech"] = k;
            wordInfo["meaning"] = v;
    wordInfo["synonym"] = syn;
    wordInfo["antonym"] = ant;
    return wordInfo;

def Test_GetInfoOnWord():
    word = 'interconnection';
    print GetInfoOnWord(word);

    word2 = "dance";
    print GetInfoOnWord(word2, 1);

In [3]:
# ------ READ WORDS from a file
def GetInputWords( fileName, fVerbose = 0):
    text_file = open( fileName, "r");
    lines = text_file.readlines();
    text_file.close();

    # read in the input words
    if (fVerbose):
        print("\n\n ----- INPUT WORDS ARE ----------- ");
        print lines;
        
    inputWords = [ wd.strip() for wd in lines ];
    return inputWords;

In [4]:
def GenListOfMeanings( wordInfoList, fileName = None, fVerbose = 0):
    
    if (fVerbose):
        print wordInfoList;
        
    outputFile = None;
    if (fileName != None):
        outputFile = open( fileName, "w");
    
    # iterate through each word info and produce an output
    for wdi in wordInfoList:
#        outText = "{:15s}\t{:10s}\t{:s}\t {:s}\t{:s}\n".format(
#            wdi["word"], wdi["partOfSpeech"], wdi["meaning"], wdi["synonym"], wdi["antonym"]);
        outText = "{:15s}\t{:10s}\t".format( wdi["word"], wdi["partOfSpeech"]);
        for j in wdi["meaning"]:
            outText += j + "\t";
        outText += "\n";
        if (outputFile != None):
            outputFile.write( outText);
        else:
            print outText;
    
    if (fileName != None):
        outputFile.close();

In [8]:
def GenMeaningForWordsFromFile( inputFile1, outputFile1):
    inputWords = GetInputWords( inputFile1);
    wordInfoList = [ GetInfoOnWord(wd.strip(), 1) for wd in inputWords ];
    GenListOfMeanings( wordInfoList);
    GenListOfMeanings( wordInfoList, outputFile1);
    return wordInfoList;
    
def TestGetMeaningOfWords():
    return GenMeaningForWordsFromFile('test1.txt', "out1.txt");
    
# Process the words list and generate output with meanings for words
def GetMeaningForJSBWords():
    return GenMeaningForWordsFromFile( 'jsb1000.txt', "full.jsb1000.txt");

In [10]:
import csv;
def LoadMeaningForJSBWords( jsbWordsMeaningsFile):
    # load the CSV File ... TO BE DONE
    mwList = [];
    with open( jsbWordsMeaningsFile, 'rb') as csvfile:
        mwRead = csv.DictReader(csvfile);
        for mw in mwRead:
            mwList.append( mw);
    print len(mwList);
    return mwList;

In [11]:
mwList = LoadMeaningForJSBWords( 'meanings.jsb1000.csv');


1000

In [12]:
import random
first10 = list(map(lambda _: random.choice(mwList), range(10)))

In [13]:
pf10 = [];
for word in first10:
    pf10.append( [ word['Word'], word['Meaning1']]);
print pf10;


[['storage', 'the act of storing something'], ['property', 'something owned; any tangible or intangible possession that is owned by someone'], ['failure', 'an act that fails'], ['timeworn', 'repeated too often; overfamiliar through overuse'], ['cameraman', 'a photographer who operates a movie camera'], ['quarterback', 'play the quarterback'], ['dilute', 'reduced in strength or concentration or quality or purity'], ['complaint', 'an often persistent bodily disorder or disease; a cause for complaining'], ['underground', 'under the level of the ground'], ['giraffe', 'tallest living quadruped; having a spotted coat and small horns and very long neck and legs; of savannahs of tropical Africa']]

In [14]:
from crossword import Crossword;
a = Crossword(16, 16, '-', 3000, pf10);
a.compute_crossword(2);

In [16]:
p = { "a": 1, "b": 2};
print p;

print "\'{}\'".format(a.current_word_list);
print a.current_word_list;


{'a': 1, 'b': 2}
'[underground, giraffe, dilute, quarterback, cameraman, complaint, property, timeworn, failure, storage]'
[underground, giraffe, dilute, quarterback, cameraman, complaint, property, timeworn, failure, storage]

In [17]:
problem1 = {
    "problemNumber": 1,
    "wordsList": pf10,
    "generatedWords": "\'{}\'".format(a.current_word_list),
    
    "wordsBank" : a.word_bank(),
    "wordsFinder": a.word_find(),
    "wordsFinderSolution": a.solution(),
    
    "wordsCrossword": a.display(),
    "legend": a.legend()
};

print problem1;


{'wordsFinder': 'u n d e r g r o u n d w d r i u \nj o l j p i k q i v g n q j s m \nk j q u a r t e r b a c k t t m \ny g u w c a x l l w w a r c q h \nh v m m l f r s m x s m r z u j \nf l s c t f a i l u r e i w o r \nd i l u t e r j l x r r e g q h \nh i q z k v i p u s l a f y z q \nl t i m e w o r n v n m i a c t \nd q e v o h d o e q i a b s t z \nj g x d c o m p l a i n t z s i \nt l i t t t v e i w x s k a q y \nr e a v r c x r i s n s z w w g \nm x u v k u s t o r a g e z a a \nd y h d s c x y o g x y b e h o \ny u j d r p h w o n o b h k v r \n', 'legend': '1. (1,1) across: under the level of the ground\n2. (3,3) across: play the quarterback\n3. (6,1) down: tallest living quadruped; having a spotted coat and small horns and very long neck and legs; of savannahs of tropical Africa\n4. (1,7) across: reduced in strength or concentration or quality or purity\n5. (2,9) across: repeated too often; overfamiliar through overuse\n6. (6,6) across: an act that fails\n7. (12,3) down: a photographer who operates a movie camera\n8. (5,11) across: an often persistent bodily disorder or disease; a cause for complaining\n9. (8,8) down: something owned; any tangible or intangible possession that is owned by someone\n10. (7,14) across: the act of storing something\n', 'wordsBank': 'storage\ncameraman\ngiraffe\ndilute\ntimeworn\nunderground\nfailure\ncomplaint\nproperty\nquarterback\n', 'wordsCrossword': '1         3           - - - - - \n- - - - -   - - - - - - - - - - \n- - 2                 7   - - - \n- - - - -   - - - - -   - - - - \n- - - - -   - - - - -   - - - - \n- - - - - 6             - - - - \n4           - - - - -   - - - - \n- - - - - - - 9 - - -   - - - - \n- 5               - -   - - - - \n- - - - - - -   - - -   - - - - \n- - - - 8                 - - - \n- - - - - - -   - - - - - - - - \n- - - - - - -   - - - - - - - - \n- - - - - - 10             - - - \n- - - - - - -   - - - - - - - - \n- - - - - - - - - - - - - - - - \n', 'generatedWords': "'[underground, giraffe, dilute, quarterback, cameraman, complaint, property, timeworn, failure, storage]'", 'wordsFinderSolution': 'u n d e r g r o u n d - - - - - \n- - - - - i - - - - - - - - - - \n- - q u a r t e r b a c k - - - \n- - - - - a - - - - - a - - - - \n- - - - - f - - - - - m - - - - \n- - - - - f a i l u r e - - - - \nd i l u t e - - - - - r - - - - \n- - - - - - - p - - - a - - - - \n- t i m e w o r n - - m - - - - \n- - - - - - - o - - - a - - - - \n- - - - c o m p l a i n t - - - \n- - - - - - - e - - - - - - - - \n- - - - - - - r - - - - - - - - \n- - - - - - s t o r a g e - - - \n- - - - - - - y - - - - - - - - \n- - - - - - - - - - - - - - - - \n', 'wordsList': [['storage', 'the act of storing something'], ['property', 'something owned; any tangible or intangible possession that is owned by someone'], ['failure', 'an act that fails'], ['timeworn', 'repeated too often; overfamiliar through overuse'], ['cameraman', 'a photographer who operates a movie camera'], ['quarterback', 'play the quarterback'], ['dilute', 'reduced in strength or concentration or quality or purity'], ['complaint', 'an often persistent bodily disorder or disease; a cause for complaining'], ['underground', 'under the level of the ground'], ['giraffe', 'tallest living quadruped; having a spotted coat and small horns and very long neck and legs; of savannahs of tropical Africa']], 'problemNumber': 1}

In [19]:
import json
output1 = json.dumps( problem1, indent=4);

with open('puzzle6.json', 'w') as fp:
    print >>  fp, output1

In [ ]:
# If needed ... then run the test function
# TestGetMeaningOfWords();

In [ ]:
# Read the Input Words List
inputFile2 = 'jsb1000.txt';
inputWords = GetInputWords( inputFile2);
print len(inputWords)

In [ ]:
from stemming.porter2 import stem;
from itertools import chain;

def flatten(listOfLists):
    "Flatten one level of nesting"
    return list(chain.from_iterable(listOfLists))

def stemall( words):
    return set([ stem(word) for word in words]);

In [ ]:
def TestStemming():
    test1 = [ "water", "waterjug", "dilute", "dilution", "waterhshed", "quarter", "quartermaster", "quarterback"];
    print test1;
    print "\n......... Stemming .... ";
    # print stem( "indigestion");
    # print stemall( [ 'indigestion']);
    stem1 = stemall( test1);
    print len(test1);
    print len(stem1);

In [ ]:
def TestTryStemmingOnJSBWords():
    stemInputs = stemall(inputWords);
    print len(stemInputs);
    setForInputs = set(inputWords);
    print len(setForInputs);
    diff1 = setForInputs.difference( stemInputs);
    diff2 = stemInputs.difference(setForInputs);
    print len(diff1);
    print len(diff2);

In [ ]:
import random
list(map(lambda _: random.choice(inputWords), range(10)))

In [ ]:
list(map(lambda _: random.choice(inputWords), range(10)))