Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 2018-komp-ling/practicals/Practical1/MaxMatch_only/Max-Match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys

parsed_sent=[]
def maxmatch(sentence, dictionary):
global parsed_sent
if len(sentence) == 0:
return 'list is empty'
for i in range(len(sentence), -1, -1):
firstword = sentence[0:i]
remainder = sentence[i:]
if firstword in dictionary:
parsed_sent.append(firstword)
return maxmatch(remainder, dictionary)
if i == 1:
firstword = sentence[0]
remainder = sentence[1:]
parsed_sent.append(remainder)
parsed_sent.append(firstword)

def main():
with open(sys.argv[1]) as f:
used_dict = [l.strip('\n') for l in f]

global parsed_sent
res = []

for sent in sys.stdin:
maxmatch(sent, used_dict)
res.append(parsed_sent)
print(' '.join(parsed_sent))
parsed_sent=[]


main()
Loading