Exemple #1
1
def assembleEntry(y):
    glosses = []
    examples = []
    etymologies = []
    quotations = []
    pronunciations = []
    pronunciation_entries = set();
    partsOfSpeech = []
    partsOfSpeechHeads = []
    etymology_entries = set();
    synonyms = []
    word_forms = []

    # Preprocessing
    for entry in y.get('entries', []):
        # Parts of speech
        psos = entry.get('partsOfSpeech') or []
        try:
            psos = map(lambda x: x.replace('proper_noun', 'proper noun'), psos)
        except:
            print(repr(psos))
            print(y['title'])
            raise
        if psos:
            partsOfSpeech.append(u"<B>" + u" ,".join(psos) + u"</B>")
            partsOfSpeechHeads.append(psos[0])
        else:
            partsOfSpeech.append("")
            partsOfSpeechHeads.append("")

        # Word forms
        elems = []
        for wf in entry.get('wordForms') or []:
            form = wf.get('form')
            if form:
                elems.append(form)
        word_forms.append(elems)

        # Synonyms
        synonyms.append(clean_synonyms(entry.get('synonyms', [])))

        # Pronunciations
        elems = []
        elem = ""
        # print(entry.get('pronunciations', []))
        for pronunciation in entry.get('pronunciations', []):
            text = pronunciation.get('text')
            if text:
                if text not in pronunciation_entries:
                    pronunciation_entries.add(text)
                    elem += text
                    note = pronunciation.get('note')
                    if note:
                        elem += " (" + note + ")"
                    elems.append(elem)
                    elem = ""
        pronunciations.append(", ".join(elems))
        # print(repr(pronunciations[-1]))

        # Senses
        gloss_entry = []
        example_entry = []
        quote_entry = []
        for sense in entry.get('senses') or []:
            gloss_entry.append(stripHtml(sense.get('gloss', '')))
            example_entry.append([ replace_newlines(stripHtml(example.get('example', ''))) for example in sense.get('examples', [])])
            quote_entry.append([ replace_newlines(stripHtml(quote.get('quote', ''))) for quote in sense.get('quotations', [])])
        glosses.append(gloss_entry)
        examples.append(example_entry)
        quotations.append(quote_entry)

        etymology_text = stripHtml(entry.get('etymology', ''))
        if etymology_text not in etymology_entries:
            etymology_entries.add(etymology_text)
            etymologies.append(etymology_text)
        else:
            etymologies.append('')

    # Assemble string

    # Title
    s = u""
    # s += y['title'] + "\t"

    # Pronunciations
    entry_pronuncs = False
    # pronunciations_filtered = [text for entry in pronunciations for text in entry]
    pronunciations_filtered = list(filter(None, pronunciations))
    if len(pronunciations_filtered) == 1:
        s += u" " + pronunciations_filtered[0] + "<BR>"
    else:
        entry_pronuncs = True

    # Entries & glosses
    single_entry = len(glosses) == 1
    for (entry_num, entry_glosses) in enumerate(glosses, 1):
        if entry_num >= 2:
            s += "<BR>"
        if not single_entry:
            s +=u"{0}. ".format(roman.int_to_roman(entry_num))
        if entry_pronuncs:
            s += prep_string(pronunciations[entry_num - 1])
        s += partsOfSpeech[entry_num - 1]

        # Handle word forms
        pos = partsOfSpeechHeads[entry_num - 1]
        word = y['title']
        if pos == "verb":
            p = en.conjugate(word, 'p')
            pp = en.conjugate(word, 'ppart')
            if p != word + 'ed' or pp != word + 'ed':
                s += u" (p. " + p + u", pp. " + pp + u")"
        elif pos == "noun":
            pl = en.pluralize(word)
            if pl != word + u's':
                s += u" (pl. " + pl + ")"
        elif pos == "adjective":
            pass

        # Glosses
        single_gloss = len(entry_glosses) == 1
        for (gloss_num, gloss) in enumerate(entry_glosses, 1):
            if not single_gloss:
                s += u" {0:d}.".format(gloss_num)
            # else:
            #     s += u":"
            s += u" {0}".format(gloss)
        s += prep_string(", ".join(synonyms[entry_num - 1]) + u"." if synonyms[entry_num - 1] else "", " Synonyms: ")
        # s += prep_string(etymologies[entry_num - 1], u" Etymology: ")

    # Etymologies
    etymologies_filtered = [etym for etym in etymologies if etym]
    if etymologies_filtered:
        s += '<BR><BR><B>Etymology:</B>'
        if len(etymologies_filtered) == 1:
            s += etymologies_filtered[0]
        else:
            for i in range(0, len(glosses)):
                if etymologies[i]:
                    s += u" {0}. {1}".format(roman.int_to_roman(i + 1), etymologies[i])

    # Examples and Quotes
    examples_flat = [example for entry in examples for examples in entry for example in examples if example]
    if examples_flat:
        s += u"<BR><BR><B>Examples:</B>"
        for (num_example, example) in enumerate(examples_flat, 1):
            if len(examples_flat) == 1:
                s += " " + example
            else:
                s += u" {0:d}. {1}".format(num_example, example)

    quotes_flat = [quote for entry in quotations for quotes in entry for quote in quotes if quote]
    if quotes_flat:
        s += u"<BR><BR><B>Quotations:</B>"
        for (num_quote, quote) in enumerate(quotes_flat, 1):
            if len(quotes_flat) == 1:
                s += u" " + quote
            else:
                s += u" {0:d}. {1}".format(num_quote, quote)

    s = escape_characters(s)

    word_forms_flat = [form for entry in word_forms for form in entry if form]
    titles = [y['title']]
    titles.extend(word_forms_flat)
    if 'verb' in partsOfSpeechHeads:
        titles.extend(en.lexeme(y['title']))
    if 'noun' in partsOfSpeechHeads:
        titles.append(en.pluralize(y['title']))
    if 'adjective' in partsOfSpeechHeads:
        adj_forms = [en.comparative(y['title']), en.superlative(y['title'])]
        adj_forms = [form for form in adj_forms if len(form.split(' ')) == 1]
        titles.extend(adj_forms)
    titles = unique(titles)

    if s.strip() == "":
        s = "Empty article."
    s = u'|'.join(titles) + u"\n" + s.strip()

    # return escape_characters(contract_tabs(s))
    return s
Exemple #2
0
 def test_main_basics(self):
     ##testing the basics
     self.assertEqual(int_to_roman(1), 'I')
     self.assertEqual(int_to_roman(5), 'V')
     self.assertEqual(int_to_roman(10), 'X')
     self.assertEqual(int_to_roman(50), 'L')
     self.assertEqual(int_to_roman(100), 'C')
     self.assertEqual(int_to_roman(500), 'D')
     self.assertEqual(int_to_roman(600), 'DC')
     self.assertEqual(int_to_roman(1000), 'M')
Exemple #3
0
def home(request):
    context = {}

    if request.POST:
        # we have the page being submitted, time to validate it
        form = IntergerForm(request.POST)
        if form.is_valid():
            ##form is valid
            context['result'] = int_to_roman(form.cleaned_data['input'])

    else:
        form = IntergerForm()  # base empty form


    context['form'] = form
    return render(request, 'home.html', context)
 def test_two(self):
     self.assertEqual("II", int_to_roman(2))
 def test_299(self):
     self.assertEqual("CCXCIX", int_to_roman(299))
 def test_one(self):
     self.assertEqual("I", int_to_roman(1))
 def test_1995(self):
     self.assertEqual("MCMXCV", int_to_roman(1995))
 def test_four(self):
     self.assertEqual("IV", int_to_roman(4))
Exemple #9
0
 def test_errors_non_int(self):
     ##this tests to make sure a non int will raise an exception
     with self.assertRaises(Exception) as cm:
         int_to_roman('cow')
     self.assertTrue('Input value is not an interger' == cm.exception.message)
Exemple #10
0
 def test_test_cases_listed(self):
     #these are the test cases listed on the evernote page.
     self.assertEqual(int_to_roman(6), 'VI')
     self.assertEqual(int_to_roman(9), 'IX')
     self.assertEqual(int_to_roman(18), 'XVIII')
     self.assertEqual(int_to_roman(19), 'XIX')
     self.assertEqual(int_to_roman(38), 'XXXVIII')
     self.assertEqual(int_to_roman(39), 'XXXIX')
     self.assertEqual(int_to_roman(40), 'XL')
     self.assertEqual(int_to_roman(98), 'XCVIII')
     self.assertEqual(int_to_roman(388), 'CCCLXXXVIII')
     self.assertEqual(int_to_roman(499), 'CDXCIX')
     self.assertEqual(int_to_roman(867), 'DCCCLXVII')
     self.assertEqual(int_to_roman(1998), 'MCMXCVIII')
     self.assertEqual(int_to_roman(3999), 'MMMCMXCIX')
 def test_eleven(self):
     self.assertEqual("XI", int_to_roman(11))
 def test_ten(self):
     self.assertEqual("X", int_to_roman(10))
 def test_nine(self):
     self.assertEqual("IX", int_to_roman(9))
 def test_eight(self):
     self.assertEqual("VIII", int_to_roman(8))
 def test_seven(self):
     self.assertEqual("VII", int_to_roman(7))
 def test_six(self):
     self.assertEqual("VI", int_to_roman(6))
 def test_five(self):
     self.assertEqual("V", int_to_roman(5))
 def test_twelve(self):
     self.assertEqual("XII", int_to_roman(12))
Exemple #19
0
 def test_errors_less_than_expected(self):
     ##tests to make sure nothing less than 1 will work
     with self.assertRaises(Exception) as cm:
         int_to_roman(-10)
     self.assertTrue('input is out of Range 1-3999' == cm.exception.message)
Exemple #20
0
from roman import int_to_roman

input_number = int(input("enter a number and i will put it into a roman numeral\n"))
print(int_to_roman(input_number))
Exemple #21
0
    def test_main_function(self):
        #random int testing validated by an online converter
        self.assertEqual(int_to_roman(1986), 'MCMLXXXVI')
        self.assertEqual(int_to_roman(3), 'III')
        self.assertEqual(int_to_roman(1), 'I')
        self.assertEqual(int_to_roman(5), 'V')
        self.assertEqual(int_to_roman(3320), 'MMMCCCXX')

        self.assertEqual(int_to_roman(3320), 'MMMCCCXX')
        self.assertEqual(int_to_roman(2724), 'MMDCCXXIV')
        self.assertEqual(int_to_roman(3632), 'MMMDCXXXII')
        self.assertEqual(int_to_roman(1190), 'MCXC')

        self.assertEqual(int_to_roman(795), 'DCCXCV')
        self.assertEqual(int_to_roman(3815), 'MMMDCCCXV')
        self.assertEqual(int_to_roman(2177), 'MMCLXXVII')
        self.assertEqual(int_to_roman(603), 'DCIII')

        self.assertEqual(int_to_roman(495), 'CDXCV')
        self.assertEqual(int_to_roman(383), 'CCCLXXXIII')
        self.assertEqual(int_to_roman(287), 'CCLXXXVII')
        self.assertEqual(int_to_roman(2678), 'MMDCLXXVIII')

        self.assertEqual(int_to_roman(3044), 'MMMXLIV')
        self.assertEqual(int_to_roman(1115), 'MCXV')
        self.assertEqual(int_to_roman(2598), 'MMDXCVIII')
        self.assertEqual(int_to_roman(2358), 'MMCCCLVIII')
 def test_800(self):
     self.assertEqual("DCCC", int_to_roman(800))
 def test_forty(self):
     self.assertEqual("XL", int_to_roman(40))
 def test_three(self):
     self.assertEqual("III", int_to_roman(3))