根据有限的单词选择句子

编程入门 行业动态 更新时间:2024-10-27 18:31:34
本文介绍了根据有限的单词选择句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何从仅包含5个或少于5个单词的文本中复制句子。需要帮助的朋友... 例如我有两个段落,每个段落包含十个句子。这些句子中有5个由5个单词组成。我如何将这些句子与段落

how to copy sentences from text containing only 5 or less than 5 words. need help friends... for example i have two paragraphs each contains ten sentences. 5 of these sentences consists of 5 words. how can i separate these sentences from that paragraphs

推荐答案

分开,你需要确定如何识别段落中的行以及如何分别和计算每个段落中的单词的条件线。例如; (请注意,您需要根据需要更新拆分字符) you need to identify the conditions of how to identify lines in the paragraph and how to separate and count the words in each line. for example; (note that you need to update the split characters for your requirements) string input = "aa bb cc dd ee. aa bb. aa bb cc dd. aa bb cc dd ee."; var lines = input.Split(new[] { '.'}, StringSplitOptions.RemoveEmptyEntries) // get lines .Select(sentence =>sentence.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) // get words .Where(words=>words.Count()==5) // filter lines with given word count .Select(w=>string.Join(" ", w)); // build line form words foreach(var line in lines) Console.WriteLine(line);

将打印

will print

aa bb cc dd ee aa bb cc dd ee

这是一个正则表达式解决方案。它仍然不处理缩写(词末尾的一个点不结束句子),但它支持支持数字内的小数点而它不做所有那些nasssty字符串拆分X | 。 Here's a Regular Expression solution. It still doesn't handle abbreviations (a dot at the end of a "word" that doesn't end a "sentence"), but it does support decimal points within numbers and it doesn't do all that nasssty string splitting X| . System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex ( @"(?:^|\G|[.?!])\s*(?'Sentence'(?:\S+\s+){0,4}\S+[.?!])" ) ; System.Text.RegularExpressions.MatchCollection mat = reg.Matches ( args [ 0 ] ) ; for ( int i = 0 ; i < mat.Count ; i++ ) { System.Console.WriteLine ( mat [ i ].Groups [ "Sentence" ].Value ) ; }

string text = "a. a. b b. b b. c c c. c c c. d d d d. d d d d. e e e e e. e e e e e."; string[] sentences = text.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); foreach (string sentence in sentences) { if (sentence.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length == 5) { System.Diagnostics.Debug.WriteLine(sentence); } }

更多推荐

根据有限的单词选择句子

本文发布于:2023-11-22 17:22:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:句子   单词

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!