شمارنده کلمات در زبان های برنامه نویسی مختلف — راهنمای کاربردی
«جداسازی واژگان» (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++
1/* C++ program to count no of words
2from given input string. */
3#include <bits/stdc++.h>
4using namespace std;
5
6#define OUT 0
7#define IN 1
8
9// returns number of words in str
10unsigned countWords(char *str)
11{
12 int state = OUT;
13 unsigned wc = 0; // word count
14
15 // Scan all characters one by one
16 while (*str)
17 {
18 // If next character is a separator, set the
19 // state as OUT
20 if (*str == ' ' || *str == '\n' || *str == '\t')
21 state = OUT;
22
23 // If next character is not a word separator and
24 // state is OUT, then set the state as IN and
25 // increment word count
26 else if (state == OUT)
27 {
28 state = IN;
29 ++wc;
30 }
31
32 // Move to next character
33 ++str;
34 }
35
36 return wc;
37}
38
39// Driver code
40int main(void)
41{
42 char str[] = "One two three\n four\tfive ";
43 cout<<"No of words : "<<countWords(str);
44 return 0;
45}
46
47// This is code is contributed by rathbhupendra
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی C
1/* C program to count no of words from given input string. */
2#include <stdio.h>
3
4#define OUT 0
5#define IN 1
6
7// returns number of words in str
8unsigned countWords(char *str)
9{
10 int state = OUT;
11 unsigned wc = 0; // word count
12
13 // Scan all characters one by one
14 while (*str)
15 {
16 // If next character is a separator, set the
17 // state as OUT
18 if (*str == ' ' || *str == '\n' || *str == '\t')
19 state = OUT;
20
21 // If next character is not a word separator and
22 // state is OUT, then set the state as IN and
23 // increment word count
24 else if (state == OUT)
25 {
26 state = IN;
27 ++wc;
28 }
29
30 // Move to next character
31 ++str;
32 }
33
34 return wc;
35}
36
37// Driver program to tes above functions
38int main(void)
39{
40 char str[] = "One two three\n four\tfive ";
41 printf("No of words : %u", countWords(str));
42 return 0;
43}
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی جاوا
1/* Java program to count no of words
2from given input string. */
3public class GFG {
4
5 static final int OUT = 0;
6 static final int IN = 1;
7
8 // returns number of words in str
9 static int countWords(String str)
10 {
11 int state = OUT;
12 int wc = 0; // word count
13 int i = 0;
14
15 // Scan all characters one by one
16 while (i < str.length())
17 {
18 // If next character is a separator, set the
19 // state as OUT
20 if (str.charAt(i) == ' ' || str.charAt(i) == '\n'
21 || str.charAt(i) == '\t')
22 state = OUT;
23
24
25 // If next character is not a word separator
26 // and state is OUT, then set the state as IN
27 // and increment word count
28 else if (state == OUT)
29 {
30 state = IN;
31 ++wc;
32 }
33
34 // Move to next character
35 ++i;
36 }
37 return wc;
38 }
39
40 // Driver program to test above functions
41 public static void main(String args[])
42 {
43 String str = "One two three\n four\tfive ";
44 System.out.println("No of words : " + countWords(str));
45 }
46}
47// This code is contributed by Sumit Ghosh
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی پایتون (نسخه 3)
1# Python3 program to count words
2# in a given string
3OUT = 0
4IN = 1
5
6# Returns number of words in string
7def countWords(string):
8 state = OUT
9 wc = 0
10
11 # Scan all characters one by one
12 for i in range(len(string)):
13
14 # If next character is a separator,
15 # set the state as OUT
16 if (string[i] == ' ' or string[i] == '\n' or
17 string[i] == '\t'):
18 state = OUT
19
20 # If next character is not a word
21 # separator and state is OUT, then
22 # set the state as IN and increment
23 # word count
24 elif state == OUT:
25 state = IN
26 wc += 1
27
28 # Return the number of words
29 return wc
30
31# Driver Code
32string = "One two three\n four\tfive "
33print("No. of words : " + str(countWords(string)))
34
35# This code is contributed by BHAVYA JAIN
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی C#
1// C# program to count no of words
2// from given input string.
3using System;
4
5class GFG {
6
7 static int OUT = 0;
8 static int IN = 1;
9
10 // returns number of words in str
11 static int countWords(String str)
12 {
13 int state = OUT;
14 int wc = 0; // word count
15 int i = 0;
16
17 // Scan all characters one
18 // by one
19 while (i < str.Length)
20 {
21 // If next character is a separator,
22 // set the state as OUT
23 if (str[i] == ' ' || str[i] == '\n'||
24 str[i] == '\t')
25 state = OUT;
26
27
28 // If next character is not a word
29 // separator and state is OUT, then
30 // set the state as IN and increment
31 // word count
32 else if (state == OUT)
33 {
34 state = IN;
35 ++wc;
36 }
37
38 // Move to next character
39 ++i;
40 }
41
42 return wc;
43 }
44
45 // Driver program to test above functions
46 public static void Main()
47 {
48 String str = "One two three\n four\tfive ";
49 Console.WriteLine("No of words : "
50 + countWords(str));
51 }
52}
53
54// This code is contributed by Sam007.
خروجی:
No of words : 5
سیستم شمارنده کلمات در زبان برنامهنویسی PHP
1<?php
2// PHP program to count no of
3// words from given input string
4$OUT = 0;
5$IN = 1;
6
7// returns number of words in str
8function countWords($str)
9{
10 global $OUT, $IN;
11 $state = $OUT;
12 $wc = 0; // word count
13 $i = 0;
14
15 // Scan all characters one by one
16 while ($i < strlen($str))
17 {
18 // If next character is
19 // a separator, set the
20 // state as OUT
21 if ($str[$i] == " " ||
22 $str[$i] == "\n" ||
23 $str[$i] == "\t")
24 $state = $OUT;
25
26 // If next character is not a
27 // word separator and state is
28 // OUT, then set the state as
29 // IN and increment word count
30 else if ($state == $OUT)
31 {
32 $state = $IN;
33 ++$wc;
34 }
35
36 // Move to next character
37 ++$i;
38 }
39
40 return $wc;
41}
42
43// Driver Code
44$str = "One two three\n four\tfive ";
45echo "No of words : " . countWords($str);
46
47// This code is contributed
48// by ChitraNayal
49?>
خروجی:
No of words : 5
اگر نوشته بالا برای شما مفید بوده است، آموزشهای زیر نیز به شما پیشنهاد میشوند:
- مجموعه آموزشهای برنامهنویسی پایتون Python
- گنجینه آموزشهای برنامه نویسی پایتون (Python)
- مجموعه آموزشهای برنامهنویسی
- زبان برنامه نویسی پایتون (Python) — از صفر تا صد
- زبان برنامه نویسی جاوا (Java) — از صفر تا صد
- متن کاوی (Text Mining) — به زبان ساده
^^
خیلی خوب بود ممنون