asdsafas 정보
asdsafas
본문
test
11232
<?php
class SimpleSentimentAnalyzer
{
protected array $positiveWords = ['좋다', '행복', '기쁘다', '사랑', '최고', '감사', '즐겁다'];
protected array $negativeWords = ['나쁘다', '싫다', '짜증', '화나다', '우울', '지루하다', '최악'];
public function analyze(string $text): string
{
$positiveScore = $this->countMatches($text, $this->positiveWords);
$negativeScore = $this->countMatches($text, $this->negativeWords);
if ($positiveScore > $negativeScore) {
return '긍정';
} elseif ($negativeScore > $positiveScore) {
return '부정';
} else {
return '중립';
}
}
protected function countMatches(string $text, array $keywords): int
{
$count = 0;
foreach ($keywords as $word) {
$count += substr_count($text, $word);
}
return $count;
}
}
// 예제 실행
$analyzer = new SimpleSentimentAnalyzer();
// 사용자 입력 예시
$input = '오늘은 정말 기쁘고 행복한 날이다!';
// 감정 분석
$result = $analyzer->analyze($input);
// 결과 출력
echo "입력 문장: $input\n";
echo "분석 결과: $result\n";
추천
0
0
댓글 0개