Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mustafa
committed
Nov 10, 2010
0 parents
commit f74d011
Showing
2 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
ini_set('display_errors','on'); | ||
error_reporting(E_ALL); | ||
header('content-type:text/html;charset=utf-8'); | ||
require_once('dbConnection.php'); | ||
|
||
class wordnetCrawler{ | ||
private $db; | ||
private $letters=array( | ||
'a','b','c','ç','d','e','f','g','ğ','h','ı','i','j','k','l','m', | ||
'n','o','ö','p','r','s','ş','t','u','ü','v','y','z','q','w','x' | ||
); | ||
|
||
public function __construct(){ | ||
$this->db=new dbConnection(); | ||
} | ||
|
||
private function getPage($url){ | ||
return file_get_contents($url); | ||
} | ||
|
||
/* | ||
* TDK büyük türkçe sözlük içinde harflerle arama yaparak kayıtlı | ||
* olan tüm sözcükleri çekmeye çalışır. | ||
* örneğin: http://tdkterim.gov.tr/bts/?kategori=verilst&kelime=a%E7&ayn=dzn | ||
* | ||
* Bulunan kelimeleri detay bağlantılarıyla birlikte veritabanına kaydeder | ||
* */ | ||
public function fetchWordsFromTdk($fletters=null){ | ||
|
||
if($fletters==null) $fletters=$this->letters; | ||
|
||
foreach($fletters as $chr1){ | ||
foreach($this->letters as $chr2){ | ||
$keyword=$chr1.$chr2; | ||
$urlkw=urlencode(mb_convert_encoding($keyword,'ISO-8859-9','UTF-8')); | ||
$pNumber=0; | ||
|
||
// sonuç listesinin kaç sayfa olduğu öğreniliyor. | ||
$url='http://tdkterim.gov.tr/bts/?kelime='.$urlkw | ||
.'&kategori=verilst&ayn=dzn&konts='.($pNumber*60); | ||
$html=$this->getPage($url); | ||
$html=mb_convert_encoding($html,'UTF-8','ISO-8859-9'); | ||
$exp='/ÖNCEKİ SAYFA<\/a> - (\d*?) sayfanın <\/span>/i'; | ||
preg_match_all($exp,$html,$pm); | ||
if(isset($pm[1][0])) $pageCount=(int)$pm[1][0]; | ||
else $pageCount=0; | ||
|
||
|
||
// sonuç sayfalarına tek tek gidilerek sonuçlar toplanıyor | ||
// en az bir sayfa olduğu varsayılıyor. | ||
do{ | ||
|
||
$url='http://tdkterim.gov.tr/bts/?kelime='.$urlkw | ||
.'&kategori=verilst&ayn=dzn&konts='.($pNumber*60); | ||
$html=$this->getPage($url); | ||
$html=mb_convert_encoding($html,'UTF-8','ISO-8859-9'); | ||
|
||
$exp='/<p class="thomicd">.*?<\/p>/i'; | ||
$exp='/<a .*? href="(.*?)"><p class="thomicd">(.*?)<\/td>+/i'; | ||
preg_match_all($exp,$html,$m); | ||
|
||
//toplanan kelimeler bu diziye dolduruluyor | ||
$sql=''; | ||
$words=array(); | ||
foreach($m[2] as $mi=>$i){ | ||
|
||
$i=trim(str_replace(array(' '),'',strip_tags($i))); | ||
$sql.='(\''.addslashes($i).'\',\''.$m[1][$mi].'\'),'; | ||
} | ||
|
||
// eğer sonuç bulunmuşsa | ||
if($sql!=''){ | ||
$sql=mb_substr($sql,0,-1); | ||
$sql='insert IGNORE into words (word,href) values '.$sql; | ||
$this->db->query($sql); | ||
} | ||
|
||
$pNumber++; | ||
echo 'Anahtar= '.$keyword.' Sayfa: '.$pNumber.' Bulunan='.count($m[2])."\n"; | ||
|
||
|
||
}while($pNumber<$pageCount); | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
public function fetchDefinitionsFromTdk(){ | ||
$keyword='kel başa şimşir tarak'; | ||
$urlkw=str_replace(' ','%20',$keyword); | ||
$url='http://tdk.gov.tr/TR/Genel/SozBul.aspx?F6E10F8892433CFFAAF6AA849816B2EF4376734BED947CDE&Kelime='.$urlkw; | ||
|
||
$html=$this->getPage($url); | ||
|
||
//$html=file_get_contents('t1.htm'); | ||
|
||
if(mb_strpos($html,'sözü bulunamadı.')!==false){ | ||
echo 'bulunamadı'."\n"; | ||
} | ||
|
||
// sonuçlar ayrıca işlenecek | ||
$exp='/<\/STRONG> ('.$keyword.') ' | ||
.'(<STRONG><FONT color=DarkBlue>(.*?)<\/FONT><\/STRONG>)?' | ||
.'<\/FONT><\/STRONG><BR><I>' | ||
.'<STRONG><FONT color=mediumblue>(.*?)<\/FONT><\/STRONG>' | ||
.'<STRONG><FONT color=mediumblue>(.*?)<\/FONT><\/STRONG><\/I><\/P> ?<P><TABLE/i'; | ||
|
||
preg_match_all($exp,$html,$m); | ||
print_r($m); | ||
// şimdilik 15-20 denemede çalıştı... alternatif olarak BTS'ye bakılacak | ||
} | ||
} | ||
$a=new wordnetCrawler(); | ||
$a->fetchDefinitionsFromTdk(); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
/* | ||
PHP 4.4.2 sürümünde yazýldý ve denendi. Bu sýnýf mysql veri tabaný sunucusu üzerinde iþlem yapmayý kolaþtýrmak amacýyla yazýldý. | ||
Gerekli veri tabaný hata ve kontrol kodlarýný her uygulamada tekrardan yazmaktansa bu sýnýfý kullanarak zaman kazanýlabilir. | ||
Yazdýn diðer sýnýflara, bu sýnýfý kalýtým yoluyla aktarabilirsiniz. | ||
NOT:PHP5 ve daha üst bir sürüm kullanýyorsanýz bu sýnýf çalýþmayabilir. Çünki php5 sürümünde sýnýflar konusunda köklü deðiþiklikler yapýldý. | ||
YAZAN: Mustafa ATÝX | ||
E-Posta: mr.mustafaatik@gmail.com | ||
*/ | ||
|
||
class dbConnection{ | ||
|
||
var $host='localhost'; | ||
var $username='root'; | ||
var $password='root'; | ||
var $database='wordnet'; | ||
|
||
|
||
var $connection; | ||
var $reader; | ||
var $affectedRows; | ||
var $numRows; | ||
var $error; | ||
var $charSet='utf8'; | ||
var $collate='utf8_turkish_ci'; | ||
|
||
function connect(){ | ||
if($this->connection=new mysqli($this->host,$this->username,$this->password)){ | ||
$this->query('set names "'.$this->charSet.'" collate "'.$this->collate.'"'); | ||
if(@$this->connection->select_db($this->database)){ | ||
$this->query('set names "'.$this->charSet.'" collate "'.$this->collate.'"'); | ||
return true; | ||
} | ||
$this->error='Veri tabaný seçilemedi.'; | ||
return false; | ||
} | ||
$this->error='Veri tabaný sunucusuna baðlanýlamadý.';return false; | ||
} | ||
|
||
function query($sql,$buffered=true){ | ||
$this->affectedRows=0; | ||
$this->numRows=0; | ||
if(!$this->connection && !$this->connect()) return false; | ||
if(($buffered && $this->reader=$this->connection->query($sql)) || | ||
(!$buffered && $this->reader=$this->connection->query($sql))){ | ||
|
||
if(gettype($this->reader)=='object') | ||
$this->numRows=$this->reader->num_rows; | ||
else | ||
$this->affectedRows=$this->connection->affected_rows; | ||
|
||
return true; | ||
} | ||
$this->error='Sorgu çalýþtýrýlamadý.';return false; | ||
} | ||
|
||
function unbufferedQuery($sql){ | ||
return $this->query($sql,false); | ||
} | ||
|
||
function fetchObject(){ | ||
return $this->reader->fetch_object(); | ||
} | ||
function fetchArray(){ | ||
return $this->reader->fetch_array(); | ||
} | ||
function fetchRow(){ | ||
return $this->reader->fetch_row(); | ||
} | ||
function nextIncrement($t){ | ||
$this->query('show table status like \''.$t.'\''); | ||
$nau=$this->fetchObject(); // next auto_increment | ||
return $nau->Auto_increment; | ||
} | ||
function lastIncrement($t){ | ||
return $this->nextIncrement($t)-1; | ||
} | ||
function getInsertId(){ | ||
return $this->connection->insert_id; | ||
} | ||
function getError(){ | ||
return $this->connection->error; | ||
} | ||
function fetchListByQuery($sql,$style='object'){ | ||
if($this->query($sql) ){ | ||
$arr=array(); | ||
if($style=='object') | ||
while($r=$this->fetchObject()) $arr[]=$r; | ||
elseif($style=='array') | ||
while($r=$this->fetchArray()) $arr[]=$r; | ||
elseif($style=='row') | ||
while($r=$this->fetchRow()) $arr[]=$r; | ||
return $arr; | ||
} | ||
return false; | ||
} | ||
function fetchFirstRecord($q){ | ||
if($this->query($q) ) | ||
{ | ||
if($this->numRows>0) | ||
return $this->fetchObject(); | ||
} | ||
return false; | ||
} | ||
} | ||
?> |