How to Select First 10 Words of a Sentence

How to select first 10 words of a sentence?


implode(' ', array_slice(explode(' ', $sentence), 0, 10));

To add support for other word breaks like commas and dashes, preg_match gives a quick way and doesn't require splitting the string:

function get_words($sentence, $count = 10) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}

As Pebbl mentions, PHP doesn't handle UTF-8 or Unicode all that well, so if that is a concern then you can replace \w for [^\s,\.;\?\!] and \W for [\s,\.;\?\!].

How to get the first 10 words in a string in R?

Here is an small function that unlist the strings, subsets the first ten words and then pastes it back together.

string_fun <- function(x) {
ul = unlist(strsplit(x, split = "\\s+"))[1:10]
paste(ul,collapse=" ")
}

string_fun(x)

df <- read.table(text = "Keyword,City(Column Header)
The length of the string should not be more than 10 is or are in,New York
The Keyword should be of specific length is or are in,Los Angeles
This is an experimental basis program string is or are in,Seattle
Please help me with getting only the first ten words is or are in,Boston", sep = ",", header = TRUE)

df <- as.data.frame(df)

Using apply (the function isn't doing anything in the second column)

df$Keyword <- apply(df[,1:2], 1, string_fun)

EDIT
Probably this is a more general way to use the function.

df[,1] <- as.character(df[,1])
df$Keyword <- unlist(lapply(df[,1], string_fun))

print(df)
# Keyword City.Column.Header.
# 1 The length of the string should not be more than New York
# 2 The Keyword should be of specific length is or are Los Angeles
# 3 This is an experimental basis program string is or Seattle
# 4 Please help me with getting only the first ten Boston

Python get the x first words in a string

The second argument of the split() method is the limit. Don't use it and you will get all words.
Use it like this:

my_string = "the cat and this dog are in the garden"    
splitted = my_string.split()

first = splitted[0]
second = splitted[1]

...

Also, don't call split() every time when you want a word, it is expensive. Do it once and then just use the results later, like in my example.

As you can see, there is no need to add the ' ' delimiter since the default delimiter for the split() function (None) matches all whitespace. You can use it however if you don't want to split on Tab for example.

PHP What is the best way to get the first 5 words of a string?


$pieces = explode(" ", $inputstring);
$first_part = implode(" ", array_splice($pieces, 0, 5));
$other_part = implode(" ", array_splice($pieces, 5));

explode breaks the original string into an array of words, array_splice lets you get certain ranges of those words, and then implode combines the ranges back together into single strings.

Pull first X words (not just characters) from mySQL

You definitely want to use SUBSTRING_INDEX which will return some number of characters until a specified count is achieved based on the occurrence of a delimiter. In your case the call would look like this:

 SELECT SUBSTRING_INDEX(text_field, ' ', 6) FROM ...

In particular, this will return up to six words where we define a word as a set of characters that are not spaces delimited by spaces.

Note: this will return punctuation attached to the last word, which may or may not be desired. It'd be simple enough to replace any punctuation characters at the tail of the string in PHP, but if you want to stay completely within SQL I think you can use TRIM. The syntax for that would be something like:

SELECT TRIM(TRAILING ',' FROM SUBSTRING_INDEX(text_field, ' ', 6)) FROM ...

There may be a better option for removing the trailing punctuation -- but perhaps that's another question (I'm still looking for a better solution than TRIM).

What is the best way to extract the first word from a string in Java?

The second parameter of the split method is optional, and if specified will split the target string only N times.

For example:

String mystring = "the quick brown fox";
String arr[] = mystring.split(" ", 2);

String firstWord = arr[0]; //the
String theRest = arr[1]; //quick brown fox

Alternatively you could use the substring method of String.

How to extract the first and final words from a string?

You have to firstly convert the string to list of words using str.split and then you may access it like:

>>> my_str = "Hello SO user, How are you"
>>> word_list = my_str.split() # list of words

# first word v v last word
>>> word_list[0], word_list[-1]
('Hello', 'you')

From Python 3.x, you may simply do:

>>> first, *middle, last = my_str.split()

How to get the first word of a sentence in PHP?

You can use the explode function as follows:

$myvalue = 'Test me more';
$arr = explode(' ',trim($myvalue));
echo $arr[0]; // will print Test

Another example:

$sentence = 'Hello World this is PHP';
$abbreviation = explode(' ', trim($sentence ))[0];
echo $abbreviation // will print Hello


Related Topics



Leave a reply



Submit