Python ile Türkçe heceleme

Girilen metni önce kelimelere ayırıyor, sonra da hecele() fonksiyonu ile heceleri arka arkaya ekrana yazıyor. Girdiyi metin dosyasından okur hale gelirse hece istatistiğinde kullanılabilir.

#! /usr/bin/python
# -*- coding: utf-8 -*-

import sys

def lowercase(ch):
	return {
	'İ':u'i',
	'I':u'ı',
	'Ç':u'ç',
	'Ğ':u'ğ',
	'Ş':u'ş'	
	}.get(ch, ch.lower())
			
def sesli(ch):
	ch = lowercase(ch)
	if ch in [u'a', u'e', u'i', u'ı', u'o', u'ö', u'u', u'ü']:
		return True
	else:
		return False

def hecele(str):
	index=0
	length=len(str)
	while sesli(str[index]) == False and length>index+1:
		index=index+1
	try:
		if sesli(str[index+1]):
			print str[0:index+1],
			hecele(str[index+1:])
		elif length>index+2:
			if sesli(str[index+2]):
				print str[0:index+1],
				hecele(str[index+1:])
			elif length>index+3:
				if sesli(str[index+3]):
					print str[0:index+2],
					hecele(str[index+2:])
				else:
					if str[index+1:index+4] in [u'str', u'ktr', u'mtr', u'nsp']:
						#print "istisna!.."
						print str[0:index+2],
						hecele(str[index+2:])
					else:
						#print "üç sessiz, normal kural"
						print str[0:index+3],
						hecele(str[index+3:])
			else:
				print unicode(str),
		else: 
			print unicode(str),
	except:
		print unicode(str),
		return
			
		

inp = raw_input("hecelenecek kelime: ").decode(sys.stdin.encoding)
inp = inp.strip('."\',;')
words = inp.split()
for word in words:
	hecele(word)


Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: