""" 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) #Declaring words and positions positions = {} inverse_positions = {} i = 0 length = len(no_contractions) for i in range(length): word = no_contractions[i] positions.update({word:i}) inverse_positions.update({i:word}) print positions print inverse_positions #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 #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 = {} #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() wordtypes.update({word : ['PR', i]}) elif word in def_article: word = word.lower() wordtypes.update({word : ['DA', i]}) elif word in indef_article: word = word.lower() wordtypes.update({word : ['InA', i]}) elif word in copula: word = word.lower() wordtypes.update({word : ['CO', i]}) elif word.endswith('ly'): wordtypes.update({word, ['AV', i]}) elif word.endswith('ing'): wordtypes.update({word : ['GR', i]}) elif word in list_conjunctions: word = word.lower() wordtypes.update({word : ['CJ', i]}) elif word in list_possesive_adjective: word = word.lower() wordtypes.update({word : ['PA', i]}) elif word[0].isupper(): wordtypes.update({word : ['N', i]}) else: wordtypes.update({word : ['Unknown', i]}) wordtypes.pop("'\n", None) print wordtypes ''' Clear everything up a bit. From now on, use these variables. ''' sentence = " ".join(no_contractions) wordtypes = wordtypes positions = positions inverse_positions = inverse_positions maximal_position = max(inverse_positions, key=inverse_positions.get) position = inverse_positions.get(maximal_position) print position word = positions.get(position) print word grammaticaltype = wordtypes.get(word) print grammaticaltype possibleposition = maximal_position extra_postition = inverse_positions.get(possibleposition) - 1 print extra_postition extra_grammar = wordtypes.get(positions.get(extra_postition)) print extra_grammar while possibleposition >= 0: if grammaticaltype == 'UNKNOWN' and extra_grammar == 'DA': print possibleposition, 'Ja!' possibleposition = extra_postition
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