句子中的单词数可以用于文本分析,因此,我们需要对它们进行计数。这可以是单个句子或多个句子。我们可以使用strsplit和sapply查找一个句子或多个句子中的单词数。
请看以下句子作为向量-
> x1<-c("Data Science is actually the Statistical analysis") > x1 [1] "Data Science is actually the Statistical analysis" > sapply(strsplit(x1, " "), length) [1] 7 > x2<-c("China faced trouble even after controlling COVID-19") > x2 [1] "China faced trouble even after controlling COVID-19" > sapply(strsplit(x2, " "), length) [1] 7 > x3<-c("Corona virus has changed everything in the world") > x3 [1] "Corona virus has changed everything in the world" > sapply(strsplit(x3, " "), length) [1] 8 > x4<-c("Corruption is the real threat to the success of any country") > x4 [1] "Corruption is the real threat to the success of any country" > sapply(strsplit(x4, " "), length) [1] 11 > x5<-c("Only unity of people can make lands prosper") > x5 [1] "Only unity of people can make lands prosper" > sapply(strsplit(x5, " "), length) [1] 8 > x6<-c("Small strings are easy to read", "Nobody likes large texts because it's boring", + "But the knowledge comes from reading") > x6 [1] "Small strings are easy to read" [2] "Nobody likes large texts because it's boring" [3] "But the knowledge comes from reading" > sapply(strsplit(x6, " "), length) [1] 6 7 6 > x7<-c("Quick Math questions are very simple to answer if you understand basic math calculations like division, percentage, ratio, etc.", + "It is a known fact that answering puzzles is not so easy but if you practice them then you will be able to build a base for solving puzzles.", + "Guesstimation Questions can be answered if you understand the right proxy about the context of the question.", + "Data extraction is the first step of programming in Data Science projects and SQL is highly required for this thing.", + "R programming and Python are widely used in Data Science. Both of these tools serve the same purpose that is analyzing large data sets.", + "Statistics is the base for Data Science and you must have a very good understanding of Statistics concepts to become a Data Scientist. + ", + "Machine Learning is a major part of Data Science projects. There are many machine learning algorithms that solve complex real-life problems in an easy way if applied correctly. + ", + "The main purpose of asking a tricky question is to check your critical thinking ability.", + "With the help probability, you can calculate whether you should do something or not. + ") > x7 [1] "Quick Math questions are very simple to answer if you understand basic math calculations like division, percentage, ratio, etc." [2] "It is a known fact that answering puzzles is not so easy but if you practice them then you will be able to build a base for solving puzzles." [3] "Guesstimation Questions can be answered if you understand the right proxy about the context of the question." [4] "Data extraction is the first step of programming in Data Science projects and SQL is highly required for this thing." [5] "R programming and Python are widely used in Data Science. Both of these tools serve the same purpose that is analyzing large data sets." [6] "Statistics is the base for Data Science and you must have a very good understanding of Statistics concepts to become a Data Scientist.\n" [7] "Machine Learning is a major part of Data Science projects. There are many machine learning algorithms that solve complex real-life problems in an easy way if applied correctly.\n" [8] "The main purpose of asking a tricky question is to check your critical thinking ability." [9] "With the help probability, you can calculate whether you should do something or not.\n" > sapply(strsplit(x7, " "), length) [1] 19 29 17 20 24 23 28 15 14
问题内容: 我认为我已经很好地理解了这一点,但我认为该陈述是错误的。怎么说包含空格,加1。 编辑: 我发现(感谢Blender)可以用更少的代码来做到这一点: 问题答案: 使用方括号,而不是括号: 或: 您也可以使用:
问题内容: 我正在尝试计算SQL字符串中有多少个单词。 我想显示字数。在给定的示例中,尽管单词之间可能有多个空格,但将为3个单词。 问题答案: 您可以使用与此类似的内容。这将获取字符串的长度,然后减去空格后减去字符串的长度。然后,将数字加到第一应该给你的单词数: 参见带有演示的SQL Fiddle 如果您使用以下数据: 和查询: 结果是:
问题内容: 我有一个字符串“ ”。我想查找一个单词在字符串中出现多少次。示例hello发生2次。我尝试了只打印字符的方法- 我想学习如何找到字数统计。 问题答案: 如果要查找单个单词的计数,请使用: 使用和汇总所有单词:
我有一个项目,制作一个程序,将字符串作为输入,然后打印字符串中的字数作为输出。在这方面,我们应该使用3种方法,一种读取输入,一种打印输出,一种计算单词。 我知道我错过了一些基本的东西,但是我已经花了几个小时在这上面,不知道为什么程序不能正常运行。我需要保持程序非常简单,所以我不想编辑太多,只要找到问题并修复它,这样它就能正常运行。 示例:输入一串文本:敏捷的棕色狐狸跳过了懒狗。9个字
问题内容: 我有一个大的文本文件正在读取,因此我需要找出几个单词出现的次数。例如,单词。我正在逐行执行此操作,每一行都是一个字符串。 我要确保我只算合法的-在中起不到作用。这意味着我知道我需要以某种方式使用正则表达式。到目前为止,我正在尝试的是: 我意识到正则表达式目前可能不正确,但我尝试过不这样做,而只是尝试查找单词的出现,并且我也得到了错误的数字。我给人的印象是,它将字符串分割成一个数组,并且
问题内容: 我是Java字符串的新手,问题是我想计算字符串中特定单词的出现次数。假设我的字符串是: 现在,我也不想拆分它,所以我想搜索一个“雄猫”这个词。它在我的字符串中出现了两次! 我正在尝试的是: 它给了我46个计数器的价值!那么解决方案是什么? 问题答案: 您可以使用以下代码: 演示版 它匹配。 表示在找到匹配项时执行循环内给出的任何操作。并且我将by 的值递增,因此很显然,这给出了一个字符