شمارنده کلمات در زبان های برنامه نویسی مختلف — راهنمای کاربردی

«جداسازی واژگان» (Tokenization)، فرایندی است که در آن «کمیت» (Quantity) عظیمی از «دادههای متنی» (Text Data) به واحدهای کوچکتری به نام «توکن» (Token) تقسیمبندی میشوند. یک توکن میتواند، یک «کلمه» (Word)، یک «علامت نقطهگذاری» (Punctuation) و یا یک «دنباله از کلمات» (Sequence of Tokens) باشد. همچنین، در صورت تمایل برنامهنویس، یک توکن میتواند در قالب یک عبارت یا «جمله» (Sentence) تعریف شود. به سیستمهایی که تعداد کلمات موجود در یک داده متنی را شمارش میکنند، «شمارنده کلمات» (Word Counter) گفته میشود.
«پردازش زبان طبیعی» (Natural Language Processing)، حوزهای از «هوش مصنوعی» (Artificial Intelligence) محسوب میشوند که از مفاهیم موجود در آن برای ساختن سیستمهایی نظیر «متنکاوی» (Text Mining)، «دستهبندی متن» (Text Classification)، «چتباتهای هوشمند» (Intelligent Chatbots)، «تحلیل احساسات» (Sentiment Analysis)، «ترجمه ماشینی» (Machine Translation) و سایر موارد استفاده میشود.
برای پیادهسازی سیستمهای ذکر شده، ابتدا نیاز است تا «الگوهای» (Patterns) موجود در دادههای متنی شناسایی و درک شوند. توکنهای شناسایی شده در یک متن، نقش مهمی در پیدا کردن چنین الگوهایی ایفا میکنند. هدف این مطلب، آشنا کردن مخاطبان با مفهوم یک سیستم ساده شمارنده کلمات و پیادهسازی آن در زبانهای برنامهنویسی مختلف است.
صورت مسأله پیادهسازی یک سیستم شمارنده کلمات
یک سیستم شمارنده کلمات باید قادر باشد تا با داشتن یک ورودی دلخواه در قالب «رشته» (String)، کلمات موجود در آن را شناسایی و تعداد آنها را شمارش کند. کلمات موجود در داده یا رشته متنی، از طریق کاراکترهای زیر در متن جداسازی میشوند.
به این دسته از کاراکترها، «حائل» (جداکننده | Delimiter) نیز گفته میشود.
- کاراکتر «فاصله» (Space): این کاراکتر، به وسیله (' ') نمایش داده میشود.
- کاراکتر «جدولبندی» (Tab): این کاراکتر، به وسیله ('t\') نمایش داده میشود.
- کاراکتر «خط جدید» (New Line): این کاراکتر، به وسیله ('n\') نمایش داده میشود.
پیادهسازی سیستم شمارنده کلمات در زبانهای برنامهنویسی مختلف
برای مسأله شمارش کلمات در داده یا رشته متنی، راهحلهای مختلفی وجود دارد. در ادامه، برخی از راهحلهای ساده و جالب برای این مسأله، در زبانهای برنامهنویسی مختلف نمایش داده شده است. ایده اساسی حل این مسأله، ایجاد و نگهداری دو «وضعیت» (States) در برنامه نوشته شده است: وضعیت IN و وضعیت OUT. وضعیت OUT، بیانکننده دیده شدن یک کاراکتر جداکننده یا حائل در داده یا رشته ورودی است.
وضعیت IN نیز، دیده شدن یک کاراکتر کلمهای در داده یا رشته ورودی را نمایش میدهد. وقتی که وضعیت قبلی برابر OUT و کاراکتر بعدی نیز برابر با یک کاراکتر کلمهای باشد، برنامه باید یک واحد به شمارنده تعداد کلمات موجود در داده یا رشته متنی اضافه کند.
سیستم شمارنده کلمات در زبان برنامهنویسی C++
/* C++ program to count no of words from given input string. */ #include <bits/stdc++.h> using namespace std; #define OUT 0 #define IN 1 // returns number of words in str unsigned countWords(char *str) { int state = OUT; unsigned wc = 0; // word count // Scan all characters one by one while (*str) { // If next character is a separator, set the // state as OUT if (*str == ' ' || *str == '\n' || *str == '\t') state = OUT; // If next character is not a word separator and // state is OUT, then set the state as IN and // increment word count else if (state == OUT) { state = IN; ++wc; } // Move to next character ++str; } return wc; } // Driver code int main(void) { char str[] = "One two three\n four\tfive "; cout<<"No of words : "<<countWords(str); return 0; } // This is code is contributed by rathbhupendra
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی C
/* C program to count no of words from given input string. */ #include <stdio.h> #define OUT 0 #define IN 1 // returns number of words in str unsigned countWords(char *str) { int state = OUT; unsigned wc = 0; // word count // Scan all characters one by one while (*str) { // If next character is a separator, set the // state as OUT if (*str == ' ' || *str == '\n' || *str == '\t') state = OUT; // If next character is not a word separator and // state is OUT, then set the state as IN and // increment word count else if (state == OUT) { state = IN; ++wc; } // Move to next character ++str; } return wc; } // Driver program to tes above functions int main(void) { char str[] = "One two three\n four\tfive "; printf("No of words : %u", countWords(str)); return 0; }
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی جاوا
/* Java program to count no of words from given input string. */ public class GFG { static final int OUT = 0; static final int IN = 1; // returns number of words in str static int countWords(String str) { int state = OUT; int wc = 0; // word count int i = 0; // Scan all characters one by one while (i < str.length()) { // If next character is a separator, set the // state as OUT if (str.charAt(i) == ' ' || str.charAt(i) == '\n' || str.charAt(i) == '\t') state = OUT; // If next character is not a word separator // and state is OUT, then set the state as IN // and increment word count else if (state == OUT) { state = IN; ++wc; } // Move to next character ++i; } return wc; } // Driver program to test above functions public static void main(String args[]) { String str = "One two three\n four\tfive "; System.out.println("No of words : " + countWords(str)); } } // This code is contributed by Sumit Ghosh
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی پایتون (نسخه 3)
# Python3 program to count words # in a given string OUT = 0 IN = 1 # Returns number of words in string def countWords(string): state = OUT wc = 0 # Scan all characters one by one for i in range(len(string)): # If next character is a separator, # set the state as OUT if (string[i] == ' ' or string[i] == '\n' or string[i] == '\t'): state = OUT # If next character is not a word # separator and state is OUT, then # set the state as IN and increment # word count elif state == OUT: state = IN wc += 1 # Return the number of words return wc # Driver Code string = "One two three\n four\tfive " print("No. of words : " + str(countWords(string))) # This code is contributed by BHAVYA JAIN
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی C#
// C# program to count no of words // from given input string. using System; class GFG { static int OUT = 0; static int IN = 1; // returns number of words in str static int countWords(String str) { int state = OUT; int wc = 0; // word count int i = 0; // Scan all characters one // by one while (i < str.Length) { // If next character is a separator, // set the state as OUT if (str[i] == ' ' || str[i] == '\n'|| str[i] == '\t') state = OUT; // If next character is not a word // separator and state is OUT, then // set the state as IN and increment // word count else if (state == OUT) { state = IN; ++wc; } // Move to next character ++i; } return wc; } // Driver program to test above functions public static void Main() { String str = "One two three\n four\tfive "; Console.WriteLine("No of words : " + countWords(str)); } } // This code is contributed by Sam007.
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی PHP
<?php // PHP program to count no of // words from given input string $OUT = 0; $IN = 1; // returns number of words in str function countWords($str) { global $OUT, $IN; $state = $OUT; $wc = 0; // word count $i = 0; // Scan all characters one by one while ($i < strlen($str)) { // If next character is // a separator, set the // state as OUT if ($str[$i] == " " || $str[$i] == "\n" || $str[$i] == "\t") $state = $OUT; // If next character is not a // word separator and state is // OUT, then set the state as // IN and increment word count else if ($state == $OUT) { $state = $IN; ++$wc; } // Move to next character ++$i; } return $wc; } // Driver Code $str = "One two three\n four\tfive "; echo "No of words : " . countWords($str); // This code is contributed // by ChitraNayal ?>
خروجی:
No of words : 5
اگر نوشته بالا برای شما مفید بوده است، آموزشهای زیر نیز به شما پیشنهاد میشوند:
- مجموعه آموزشهای برنامهنویسی پایتون Python
- گنجینه آموزشهای برنامه نویسی پایتون (Python)
- مجموعه آموزشهای برنامهنویسی
- زبان برنامه نویسی پایتون (Python) — از صفر تا صد
- زبان برنامه نویسی جاوا (Java) — از صفر تا صد
- متن کاوی (Text Mining) — به زبان ساده
^^
خیلی خوب بود ممنون