I have developed a speech to text program where the user can speak a short sentence and then inserts that into a text box.
How do I extract the first letters of each word and then insert that into the text field?
For example if the user says: "Hello World". I want to insert HW into the text box.
If you have a string, you could just split it using
input.split(" ") //splitting by space
//maybe you want to replace dots, etc with nothing).
The iterate over the array:
for(String s : input.split(" "))
And then get the first letter of every string in a list/array/etc or append it to a string:
//Outside the for-loop:
String firstLetters = "";
// Insdie the for-loop:
firstLetters = s.charAt(0);
The resulting function:
public String getFirstLetters(String text)
{
String firstLetters = "";
text = text.replaceAll("[.,]", ""); // Replace dots, etc (optional)
for(String s : text.split(" "))
{
firstLetters += s.charAt(0);
}
return firstLetters;
}
The resulting function if you want to use a list (ArrayList matches):
Basically you just use an array/list/etc as argument type and instead of text.split(" ") you just use the argument. Also, remove the line where you would replace characters like dots, etc.
public String getFirstLetters(ArrayList<String> text)
{
String firstLetters = "";
for(String s : text)
{
firstLetters += s.charAt(0);
}
return firstLetters;
}
Use split to get an array separated words, then you can get first N characters with substring(0, N).
Assuming the sentence only contain a-z and A-Z and " " to separate the words , If you want an efficient way to do it, I suggest the below method.
public String getResult(String input){
StringBuilder sb = new StringBuilder();
for(String s : input.split(" ")){
sb.append(s.charAt(0));
}
return sb.toString();
}
Then write it to the text field.
jTextField.setText(getResult(input_String));
You would want to extract the string, put it all into a list, and loop through
String[] old = myTextView.getText().split(" ");
String add="";
for(String s:old)
add+=""+s.charAt(0);
myTextView.setText(add);
Related
I'm in project doing a text mining. it's needed that my program also tokenize when the text using Enter in his/her document (/br if in HTML). Now my program only can detect 'space'. How to do it?
this is my code:
private ArrayList tokenize(String inp) {
ArrayList<String> out = new ArrayList<String>();
String[] split = inp.split(" ");
for (int i = 0; i < split.length; i++) {
if (!split[i].isEmpty()) {
out.add(split[i]);
}
}
return out;
}
You could also use a simple regular expression to do what you want:
String input = "Line of text \nAnother line<br><br><br />html<br />line";
String [] parts = input.split("\\s+|(<br>|<br\\s*/>)+");
System.out.println(Arrays.asList(parts));
It can also replace multiple whitespaces/breaklines in a row. Regular expressions can work really well for this kind of tasks.
Output:
[Line, of, text, Another, line, html, line]
Explanation: \s is short for all whitespaces (space, tab, newline). \s+ means 1 or more whitespaces. <br>|<br\\s*/> means <br> or <br/> or <br /> or <br />. They are in a group: (<br>|<br\\s*/>), so we can use + to identify one or more of them.
The whole stuff together: one or more whitespace characters or one or more of the different versions of <br>.
So your tokenize method could look like this (use generics, if you use java 1.5 or later):
private List<String> tokenize(String inp) {
List<String> out = new ArrayList<String>();
String[] split = inp.split("\\s+|(<br>|<br\\s*/>)+");
for (String s : split) {
if (!s.isEmpty()) {
out.add(s);
}
}
return out;
}
Are you sure splitting at the enters isn't already working? Because with this:
String s = "Hi b\nb bye";
System.out.println(s);
System.out.println();
String [] ss = s.split(" ");
for(String s2 : ss)
{
System.out.println(s2);
}
This is my output:
Hi b
b bye
Hi
b
b
bye
As you can see, the string was split both at the spaces, and at the new line (even though a space was the only regex). However, if this isn't working for you, you could just cycle through the String array and and call String.split("\n"). Then you can just add the new split strings to an ArrayList.
I am trying to get all words that begin with a letter from a long string. How would you do this is java? I don't want to loop through every letter or something inefficient.
EDIT: I also can't use any in built data structures (except arrays of course)- its for a cs class. I can however make my own data structures (which i have created sevral).
You could try obtaining an array collection from your String and then iterating through it:
String s = "my very long string to test";
for(String st : s.split(" ")){
if(st.startsWith("t")){
System.out.println(st);
}
}
You need to be clear about some things. What is a "word"? You want to find only "words" starting with a letter, so I assume that words can have other characters too. But what chars are allowed? What defines the start of such a word? Whitespace, any non letter, any non letter/non digit, ...?
e.g.:
String TestInput = "test séntènce îwhere I'm want,to üfind 1words starting $with le11ers.";
String regex = "(?<=^|\\s)\\pL\\w*";
Pattern p = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS);
Matcher matcher = p.matcher(TestInput);
while (matcher.find()) {
System.out.println(matcher.group());
}
The regex (?<=^|\s)\pL\w* will find sequences that starts with a letter (\pL is a Unicode property for letter), followed by 0 or more "word" characters (Unicode letters and numbers, because of the modifier Pattern.UNICODE_CHARACTER_CLASS).
The lookbehind assertion (?<=^|\s) ensures that there is the start of the string or a whitespace before the sequence.
So my code will print:
test
séntènce ==> contains non ASCII letters
îwhere ==> starts with a non ASCII letter
I ==> 'm is missing, because `'` is not in `\w`
want
üfind ==> starts with a non ASCII letter
starting
le11ers ==> contains digits
Missing words:
,to ==> starting with a ","
1words ==> starting with a digit
$with ==> starting with a "$"
You could build a HashMap -
HashMap<String,String> map = new HashMap<String,String>();
example -
ant, bat, art, cat
Hashmap
a -> ant,art
b -> bat
c -> cat
to find all words that begin with "a", just do
map.get("a")
You can get the first letter of the string and check with API method that if it is letter or not.
String input = "jkk ds 32";
String[] array = input.split(" ");
for (String word : array) {
char[] arr = word.toCharArray();
char c = arr[0];
if (Character.isLetter(c)) {
System.out.println( word + "\t isLetter");
} else {
System.out.println(word + "\t not Letter");
}
}
Following are some sample output:
jkk isLetter
ds isLetter
32 not Letter
Scanner scan = new Scanner(text); // text being the string you are looking in
char test = 'x'; //whatever letter you are looking for
while(scan.hasNext()){
String wordFound = scan.next();
if(wordFound.charAt(0)==test){
//do something with the wordFound
}
}
this will do what you are looking for, inside the if statement do what you want with the word
Regexp way:
public static void main(String[] args) {
String text = "my very long string to test";
Matcher m = Pattern.compile("(^|\\W)(\\w*)").matcher(text);
while (m.find()) {
System.out.println("Found: "+m.group(2));
}
}
You can use split() method. Here is an example :
String string = "your string";
String[] parts = string.split(" C");
for(int i=0; i<parts.length; i++) {
String[] word = parts[i].split(" ");
if( i > 0 ) {
// ignore the rest words because don't starting with C
System.out.println("C" + word[0]);
}
else { // Check 1st excplicitly
for(int j=0; j<word.length; j++) {
if ( word[j].startsWith("c") || word[j].startsWith("C"))
System.out.println(word[j]);
}
}
}
where "C" is you letter. Just then loop around the array. For parts[0] you have to check if it starts with "C". It was my mistake to start looping from i=1. The correct is from 0.
I have a multiple line string that is taken as a user input. I broke the string into ArrayList by str.split("\\s ") and changed a particular word if it occurred, now i want to merge the words in the ArrayList with the replaced word in it and form the string again in a multiple line pattern only. I'm not getting how to do this. Please help.
Using only standard Java (assuming your ArrayList is called words)
StringBuilder sb = new StringBuilder();
for (String current : words)
sb.append(current).append(" ");
String s = sb.toString().trim();
If you have the Guava library you can use Joiner:
String s = Joiner.on(" ").join(words)
Both of these will work even if the type of words is String[].
If you want to preserve the line structure, I suggest the following approach: first break the input string into lines by using .split("\n"). Then, split each lines to words using .split("\\s"). Here's how the code should look like:
public String convert(String input, String wordToReplace, String replacement) {
StringBuilder result = new StringBuilder();
String[] lines = input.split("\n");
for (String line : lines) {
boolean isFirst = true;
for (String current : line.split("\\s")) {
if (!isFirst)
result.append(" ");
isFirst = false;
if (current.equals(wordToReplace))
current = replacement;
result.append(current);
}
result.append("\n");
}
return result.toString().trim();
}
I'm trying to write a code that uses a scanner to input a list of words, all in one string, then alphabetizer each individual word. What I'm getting is just the first word alphabetized by letter, how can i fix this?
the code:
else if(answer.equals("new"))
{
System.out.println("Enter words, separated by commas and spaces.");
String input= scanner.next();
char[] words= input.toCharArray();
Arrays.sort(words);
String sorted= new String(words);
System.out.println(sorted);
}
Result: " ,ahy "
You're reading in a String via scanner.next() and then breaking that String up into characters. So, as you said, it's sorting the single-string by characters via input.toCharArray(). What you need to do is read in all of the words and add them to a String []. After all of the words have been added, use Arrays.sort(yourStringArray) to sort them. See comments for answers to your following questions.
You'll need to split your string into words instead of characters. One option is using String.split. Afterwards, you can join those words back into a single string:
System.out.println("Enter words, separated by commas and spaces.");
String input = scanner.nextLine();
String[] words = input.split(",| ");
Arrays.sort(words);
StringBuilder sb = new StringBuilder();
sb.append(words[0]);
for (int i = 1; i < words.length; i++) {
sb.append(" ");
sb.append(words[i]);
}
String sorted = sb.toString();
System.out.println(sorted);
Note that by default, capital letters are sorted before lowercase. If that's a problem, see this question.
I have a String "Magic Word". I need to trim the string to extract "Magic" only.
I am doing the following code.
String sentence = "Magic Word";
String[] words = sentence.split(" ");
for (String word : words)
{
System.out.println(word);
}
I need only the first word.
Is there any other methods to trim a string to get first word only if space occurs?
String firstWord = "Magic Word";
if(firstWord.contains(" ")){
firstWord= firstWord.substring(0, firstWord.indexOf(" "));
System.out.println(firstWord);
}
You could use String's replaceAll() method which takes a regular expression as input, to replace everything after the space including the space, if a space does indeed exist, with the empty string:
String firstWord = sentence.replaceAll(" .*", "");
This should be the easiest way.
public String firstWord(String string)
{
return (string+" ").split(" ")[0]; //add " " to string to be sure there is something to split
}
modifying previous answer.
String firstWord = null;
if(string.contains(" ")){
firstWord= string.substring(0, string.indexOf(" "));
}
else{
firstWord = string;
}
String input = "This is a line of text";
int i = input.indexOf(" "); // 4
String word = input.substring(0, i); // from 0 to 3
String rest = input.substring(i+1); // after the space to the rest of the line
A dirty solution:
sentence.replaceFirst("\\s*(\\w+).*", "$1")
This has the potential to return the original string if no match, so just add a condition:
if (sentence.matches("\\s*(\\w+).*", "$1"))
output = sentence.replaceFirst("\\s*(\\w+).*", "$1")
Or you can use a cleaner solution:
String parts[] = sentence.trim().split("\\s+");
if (parts.length > 0)
output = parts[0];
The two solutions above makes assumptions about the first character that is not space in the string is word, which might not be true if the string starts with punctuations.
To take care of that:
String parts[] = sentence.trim().replaceAll("[^\\w ]", "").split("\\s+");
if (parts.length > 0)
output = parts[0];
You May Try This->
String newString = "Magic Word";
int index = newString.indexOf(" ");
String firstString = newString.substring(0, index);
System.out.println("firstString = "+firstString);
We should never make simple things more complicated. Programming is about making complicated things simple.
string.split(" ")[0]; //the first word.