1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Djunehor\Grammar; |
4
|
|
|
|
5
|
|
|
class Word |
6
|
|
|
{ |
7
|
|
|
private $string; |
8
|
|
|
|
9
|
|
|
private $partsOfSpeech = [ |
10
|
|
|
'prep.' => 'Preposition', |
11
|
|
|
'n.' => 'Noun', |
12
|
|
|
'v.' => 'Verb', |
13
|
|
|
'a.' => 'Adjective', |
14
|
|
|
'adv.' => 'Adverb', |
15
|
|
|
'pron.' => 'Pronoun', |
16
|
|
|
'interj.' => 'Interjection', |
17
|
|
|
'conj.' => 'Conjunction', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
private $table; |
21
|
|
|
|
22
|
14 |
|
public function __construct($string = null) |
23
|
|
|
{ |
24
|
14 |
|
if ($string) { |
25
|
|
|
$this->string = $string; |
26
|
|
|
} |
27
|
14 |
|
$this->table = config('laravel-grammar.table', 'entries'); |
28
|
14 |
|
} |
29
|
|
|
|
30
|
1 |
|
public function getPartsOfSpeech() |
31
|
|
|
{ |
32
|
1 |
|
return array_values($this->partsOfSpeech); |
33
|
|
|
} |
34
|
|
|
|
35
|
11 |
|
public function getWordPartOfSpeech($string) |
36
|
|
|
{ |
37
|
11 |
|
$this->string = $string; |
38
|
11 |
|
$row = \DB::table($this->table)->where('word', ucfirst($this->string))->first(); |
39
|
|
|
|
40
|
11 |
|
return $row ? $this->parts($row->word_type) : []; |
41
|
|
|
} |
42
|
|
|
|
43
|
11 |
|
public function parts($string) |
44
|
|
|
{ |
45
|
11 |
|
$array = explode(' ', str_replace(',', '', $string)); |
46
|
|
|
|
47
|
11 |
|
$parts = []; |
48
|
|
|
|
49
|
11 |
|
foreach ($array as $item) { |
50
|
11 |
|
if (array_key_exists($item, $this->partsOfSpeech)) { |
51
|
11 |
|
$parts[] = $this->partsOfSpeech[$item]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
11 |
|
return $parts; |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
public function checkIs($part) |
59
|
|
|
{ |
60
|
9 |
|
return in_array($part, $this->getWordPartOfSpeech($this->string)); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function isNoun() |
64
|
|
|
{ |
65
|
1 |
|
return $this->checkIs('Noun'); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function isPronoun() |
69
|
|
|
{ |
70
|
1 |
|
return $this->checkIs('Pronoun'); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function isAdjective() |
74
|
|
|
{ |
75
|
1 |
|
return $this->checkIs('Adjective'); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function isAdverb() |
79
|
|
|
{ |
80
|
1 |
|
return $this->checkIs('Adverb'); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function isPreposition() |
84
|
|
|
{ |
85
|
1 |
|
return $this->checkIs('Preposition'); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function isConjunction() |
89
|
|
|
{ |
90
|
1 |
|
return $this->checkIs('Conjunction'); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function isInterjection() |
94
|
|
|
{ |
95
|
1 |
|
return $this->checkIs('Interjection'); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function isVerb() |
99
|
|
|
{ |
100
|
1 |
|
return $this->checkIs('Verb'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|