""" Grammar 1.1, Mel, 7-9-2016, 13-9-2016 Een testversie om te kijken of mijn grammaticaprogramma functioneert Capitalize nouns """ import sys import re #Sentence input and output print 'Type sentence' sentence = sys.stdin.readline() print 'This is the sentence: ', sentence #Splitting the sentence up quote_list = sentence.split (" ") print 'These are the words: ', ", ".join(quote_list) #Finding length word_length = len(sentence.split()) length = len(sentence) print 'The length of the sentence is :', length, '\n' print 'The wordcount is :', word_length, '\n' #Finding declarative or interogative if '?' in sentence: print "Interogative" elif '.' in sentence: print "Declarative" elif '!' in sentence: print "Exclamative" else: print "You forgot the punctuation marks, you idiot!" #Defining articles article = ['the ', 'The ', 'A ', 'An ', 'a ', 'an '] def_article = ['the ', 'The '] indef_article = ['A ', 'An ', 'a ', 'an '] #Editing for clarity new_sentence = sentence.replace("n't", "'nt") #Defining Dictionary dict_contractions = {"'ll": "will", "'ve": "have", "'s": "is", "'m": "am", "'d": "had", "'nt": "not"} #Morphemes sub_morphemes = re.split("[?,'.]", new_sentence) sub_string = " '".join(sub_morphemes) morphemes = sub_string.split (" ") clean = "\n".join(morphemes) print clean #Find list length length = len(morphemes) print "There are ", length, "morphemes" #Declaring list without contractions no_contractions = [] #Printing without contractions i = 0 for i in range(length): word = morphemes[i] if word in dict_contractions: print dict_contractions[word] no_contractions.append(dict_contractions[word]) else: print word no_contractions.append(word) #Simple word type analysis for pronouns list_pronouns = ['I', 'you', 'You', 'he', 'He', 'She', 'she', 'It', 'it', 'We', 'we', 'they', 'They', 'me', 'Me', 'him', 'Him', 'them', 'Them'] #Joint sentence clean = " ".join(no_contractions) print clean #Define wordtypes = [] #Define libraries def_article = ['the', 'The'] indef_article = ['A', 'An', 'a', 'an'] #Define copula copula = ['am', 'are', 'being', 'was', 'were', 'been', 'be', 'is', 'Am', 'Are', 'Being', 'Was', 'Were', 'Been', 'Be', 'Is'] #Define prepositions prepositions = ['abeam', 'aboard', 'about', 'above', 'abreast', 'abroad', 'absent', 'across', 'adjacent', 'after', 'against', 'along', 'alongside', 'amid', 'among', 'apropos', 'apud', 'around', 'as', 'astride', 'at', 'atop', 'ontop', 'bar', 'before', 'behind', 'below', 'beneath', 'beside', 'besides', 'between', 'beyond', 'but', 'by', 'chez', 'circa', 'come', 'despite', 'down', 'during', 'except', 'for', 'from', 'in', 'inside', 'into', 'less', 'like', 'minus', 'near', 'nearer', 'nearest', 'notwithstanding', 'of', 'off', 'on', 'onto', 'opposite', 'out', 'outside', 'over', 'pace', 'past', 'per', 'post', 'pre', 'pro', 'qua', 're', 'sans', 'short', 'since', 'than', 'through', 'throughout', 'to', 'toward', 'towards', 'under', 'underneath', 'unlike', 'until', 'up', 'upon', 'upside', 'versus', 'via', 'vice', 'with', 'within', 'without', 'worth'] #Define conjunctions coordinateconjunctions = ['and', 'but'] subordinateconjunctions = ['although', 'even if', 'though', 'now that', 'because', 'as', 'since', 'until', 'after', 'when', 'before'] #Define Conjunctions list_conjunctions = ['for', 'and', 'nor', 'but', 'or', 'yet', 'so'] #Define Possesive pronouns list_possesive_adjective = ['my', 'My', 'your', 'Your', 'his', 'His', 'her', 'Her', 'its', 'Its', 'our', 'Our', 'your', 'Your', 'their', 'Their'] #Define dictionary wordtypes = {} wordposition = {} inverted = {} positionlist = [] #Word type i = 0 length = len(no_contractions) for i in range(length): word = no_contractions[i] if word in list_pronouns: word = word.lower() inverted.update({'PR' : word}) wordtypes.update({word : 'PR'}) wordposition.update({i : word}) elif word in def_article: word = word.lower() inverted.update({'DA' : word}) wordtypes.update({word : 'DA'}) wordposition.update({i : word}) elif word in indef_article: word = word.lower() inverted.update({'InA' : word}) wordtypes.update({word : 'InA'}) wordposition.update({i : word}) elif word in coordinateconjunctions: word = word.lower() inverted.update({'CC' : word}) wordtypes.update({word : 'CC'}) wordposition.update({i : word}) elif word in subordinateconjunctions: word = word.lower() inverted.update({'SC' : word}) wordtypes.update({word : 'SC'}) wordposition.update({i : word}) elif word in copula: word = word.lower() inverted.update({'CO' : word}) wordtypes.update({word : 'CO'}) wordposition.update({i : word}) elif word.endswith('ly'): inverted.update({'AV' : word}) wordtypes.update({word, 'AV'}) wordposition.update({i : word}) elif word in list_conjunctions: word = word.lower() inverted.update({'CJ' : word}) wordtypes.update({word : 'CJ'}) wordposition.update({i : word}) elif word in list_possesive_adjective: word = word.lower() inverted.update({'PA' : word}) wordtypes.update({word : 'PA'}) wordposition.update({i : word}) elif word[0].isupper(): inverted.update({'N' : word}) wordtypes.update({word : 'N'}) wordposition.update({i : word}) elif word in prepositions: word = word.lower() inverted.update({'PP' : word}) wordtypes.update({word : 'PP'}) wordposition.update({i : word}) elif word.endswith('ing'): inverted.update({'GR' : word}) wordtypes.update({word : 'GR'}) wordposition.update({i : word}) elif word == "'": inverted.update({'IP' : ","}) wordtypes.update({"," : 'IP'}) wordposition.update({i : ","}) else: inverted.update({'Unknown' : word}) wordtypes.update({word : 'Unknown',}) wordposition.update({i : word}) print wordtypes print wordposition print inverted i = 0 length = len(no_contractions) for i in range(length): word = no_contractions[i] if i<= length: positionlist.append(wordposition.get(i) + " " + wordtypes.get(wordposition.get(i))) i = i + 1 print positionlist print inverted.get('Unknown')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question