텍스트 분석기: 프로그램이해
텍스트를 작성할 때 글자수, 단어수, 줄수, 단락수는 매우 중요한 요소입니다. 특히 블로그 글이나 기사 작성 시 이러한 요소들을 정확하게 계산하여 글의 길이와 구성을 확인하는 것이 필요합니다. 이를 위해 텍스트 분석기를 사용하면 매우 유용합니다. 이번 글에서는 실시간으로 글자수, 단어수, 줄수, 단락수를 계산하고, 추가로 평균 단어 길이와 평균 단락 길이도 제공하는 텍스트 분석기 프로그램을 소개합니다.
<!DOCTYPE html>
<html lang=”ko”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>텍스트 분석기</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
textarea {
width: 100%;
height: 300px;
padding: 10px;
box-sizing: border-box;
}
.results {
margin-top: 20px;
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
}
.results p {
margin: 5px 0;
}
</style>
</head>
<body>
<h1>텍스트 분석기</h1>
<textarea id=”textInput” placeholder=”여기에 텍스트를 입력하세요”></textarea>
<div class=”results”>
<p>공백 제외 글자수: <span id=”charCountNoSpaces”>0</span></p>
<p>공백 포함 글자수: <span id=”charCountWithSpaces”>0</span></p>
<p>단어 수: <span id=”wordCount”>0</span></p>
<p>줄 수: <span id=”lineCount”>0</span></p>
<p>단락 수: <span id=”paragraphCount”>0</span></p>
<p>평균 단어 길이: <span id=”averageWordLength”>0</span></p>
<p>평균 단락 길이 (단어 수): <span id=”averageParagraphLength”>0</span></p>
</div>
<script>
function countText() {
const text = document.getElementById(‘textInput’).value;
// 공백 제외 글자수
const charCountNoSpaces = text.replace(/\s/g, ”).length;
// 공백 포함 글자수
const charCountWithSpaces = text.length;
// 단어 수
const words = text.trim().split(/\s+/).filter(word => word.length > 0);
const wordCount = words.length;
// 줄 수
const lineCount = text.split(/\n/).length;
// 단락 수
const paragraphs = text.trim().split(/\n\n+/).filter(paragraph => paragraph.length > 0);
const paragraphCount = paragraphs.length;
// 평균 단어 길이
const totalWordLength = words.reduce((total, word) => total + word.length, 0);
const averageWordLength = wordCount > 0 ? (totalWordLength / wordCount).toFixed(2) : 0;
// 평균 단락 길이 (단어 수)
const totalParagraphLength = paragraphs.reduce((total, paragraph) => total + paragraph.split(/\s+/).filter(word => word.length > 0).length, 0);
const averageParagraphLength = paragraphCount > 0 ? (totalParagraphLength / paragraphCount).toFixed(2) : 0;
// 결과 표시
document.getElementById(‘charCountNoSpaces’).innerText = charCountNoSpaces;
document.getElementById(‘charCountWithSpaces’).innerText = charCountWithSpaces;
document.getElementById(‘wordCount’).innerText = wordCount;
document.getElementById(‘lineCount’).innerText = lineCount;
document.getElementById(‘paragraphCount’).innerText = paragraphCount;
document.getElementById(‘averageWordLength’).innerText = averageWordLength;
document.getElementById(‘averageParagraphLength’).innerText = averageParagraphLength;
}
document.addEventListener(‘DOMContentLoaded’, function() {
document.getElementById(‘textInput’).addEventListener(‘input’, countText);
});
</script>
</body>
</html>
기능 설명
- 공백 제외 글자수: 텍스트에서 공백을 제외한 글자수.
- 공백 포함 글자수: 텍스트에서 공백을 포함한 글자수.
- 단어 수: 텍스트의 단어 개수.
- 줄 수: 텍스트의 줄 개수.
- 단락 수: 텍스트의 단락 개수.
- 평균 단어 길이: 단어의 평균 길이.
- 평균 단락 길이 (단어 수): 단락 당 평균 단어 수.
결론
텍스트 분석기는 글을 작성할 때 매우 유용한 도구입니다. 글자수, 단어수, 줄수, 단락수를 실시간으로 계산하여 글의 길이와 구성을 쉽게 파악할 수 있습니다. 또한, SEO 최적화와 가독성을 높이는 데 큰 도움이 됩니다. 이 글에서 소개한 텍스트 분석기 프로그램을 통해 더 나은 글을 작성해 보세요.
생각해두기::
