words

Get n Number of words using regex in Java

我是研究僧i 提交于 2019-12-08 08:16:57
问题 I have a section of a book, complete with punctuation, line breaks etc. and I want to be able to extract the first n words from the text, and divide that into 5 parts. Regex mystifies me. This is what I am trying. I creates an array of index size 0, with all the input text: public static String getNumberWords2(String s, int nWords){ String[] m = s.split("([a-zA-Z_0-9]+\b.*?)", (nWords / 5)); return "Part One: \n" + m[1] + "\n\n" + "Part Two: \n" + m[2] + "\n\n" + "Part Three: \n" + m[3] + "\n

Get n Number of words using regex in Java

百般思念 提交于 2019-12-08 06:32:24
I have a section of a book, complete with punctuation, line breaks etc. and I want to be able to extract the first n words from the text, and divide that into 5 parts. Regex mystifies me. This is what I am trying. I creates an array of index size 0, with all the input text: public static String getNumberWords2(String s, int nWords){ String[] m = s.split("([a-zA-Z_0-9]+\b.*?)", (nWords / 5)); return "Part One: \n" + m[1] + "\n\n" + "Part Two: \n" + m[2] + "\n\n" + "Part Three: \n" + m[3] + "\n\n" + "Part Four: \n" + m[4] + "\n\n" + "Part Five: \n" + m[5]; } Thanks! I think the simplest, and

Count words in a user-input string in C

时间秒杀一切 提交于 2019-12-07 19:53:05
问题 So, we were given this program in class. "Write a Program in C to count the number of words in a sentence input by the user." This is what i could come up with, but the number of words is always one less than what is the correct number. My teacher told everyone to just add 1 to the word count before printing it. I think it has a bug, if we don't enter any words, i.e. , press Enter instead of typing,the program suggested by my teacher would still give word count as 1 not 0. Do you know of any

Php - return count() in words

本小妞迷上赌 提交于 2019-12-07 18:02:55
问题 I have the following in my code: $my_count = count($total_elements[$array_object]); $my_count now contains the number of elements in $total_elements[$array_object] . I want to convert this number into its corresponding natural number (zero, one, two, ...) In this specific case , I only have 5 numbers : $numbers = array( 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', ); How to retrieve the number of elements in a given array and then echo the corresponding natural number

Regular Expression - Global Search Between 2 Whole Words

好久不见. 提交于 2019-12-06 14:44:29
I'm attempting to search for content between 2 whole words using a regular expression. For example: all the girls went to the mall in town. In the above string I want to find the content between the word all and to : (?<=all).*?(?=to)/g However, it's finding two matches since the expression is not instructed to search between whole words only: " the girls went " //between all and to " in " //between m(all) and (to)wn I had thought to add spaces in the expression, like this: (?<= all ).*?(?= to )/g but this will not work in the above string since all is the first word of the sentence. How can I

Placing words in table grid in word search puzzle?

匆匆过客 提交于 2019-12-06 07:32:34
问题 I am trying to create a words search puzzle generated by script. The words should be placed horizontally, vertically or diagonally. I might need the option to set whether they are allowed to read only forward or backward. I have an array of words such as (apple, banana, grape, lemon, pear) which needs to be placed in the table. I have already created the table but I am stuck at how to place the words in the grid. I am looking for examples with some explanation. Please see my code below: var

Count words in a user-input string in C

和自甴很熟 提交于 2019-12-06 04:35:40
So, we were given this program in class. "Write a Program in C to count the number of words in a sentence input by the user." This is what i could come up with, but the number of words is always one less than what is the correct number. My teacher told everyone to just add 1 to the word count before printing it. I think it has a bug, if we don't enter any words, i.e. , press Enter instead of typing,the program suggested by my teacher would still give word count as 1 not 0. Do you know of any way to get proper word count without just adding 1 at the end? Code: My code(giving 1 less than correct

Php - return count() in words

家住魔仙堡 提交于 2019-12-06 03:51:30
I have the following in my code: $my_count = count($total_elements[$array_object]); $my_count now contains the number of elements in $total_elements[$array_object] . I want to convert this number into its corresponding natural number (zero, one, two, ...) In this specific case , I only have 5 numbers : $numbers = array( 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', ); How to retrieve the number of elements in a given array and then echo the corresponding natural number (human readable number) from the array? Or better yet - is there a better way of doing this? (I have found

PHP : Find repeated words with and without space in text

馋奶兔 提交于 2019-12-05 22:49:44
I can find repeated words in text with this function: $str = 'bob is a good person. mary is a good person. who is the best? are you a good person? bob is the best?'; function repeated($str) { $str=trim($str); $str=ereg_replace('[[:space:]]+', ' ',$str); $words=explode(' ',$str); foreach($words as $w) { $wordstats[($w)]++; } foreach($wordstats as $k=>$v) { if($v>=2) { print "$k"." , "; } } } thats result me like : bob , good , person , is , a , the , best? Q : how i can get result repeated words and Multi-part words between space look like : bob , good , person , is , a , the , best? , good

迭代器和可迭代对象

烂漫一生 提交于 2019-12-05 03:49:46
import re import reprlib RE_WORD = re.compile('\w+') class SentenceIterator: def __init__(self, words): self.words = words self.index = 0 def __next__(self): try: word = self.words[self.index] except IndexError: raise StopIteration() self.index += 1 return word def __iter__(self): # 其实可以不用写 return self class Sentence: def __init__(self, text): self.text = text self.words = RE_WORD.findall(text) def __repr__(self): return 'Setence(%s)' % reprlib.repr(self.text) def __iter__(self): return SentenceIterator(self.words) s4 = Sentence('pig and pepper') it = iter(s4) print(next(it)) print(next(it))