Reading Polynomials from Input File (in Java) - java

I need to read a polynomial from a file. The problem I am facing is if the coefficients were more than one digit or if it had a negative sign, I don't know how to make the program recognize multiple digits are a single integer. That is going to really complicate my code when doing it manually since I need to make the program "aware" of its surrounding like when to start and stop. So, are there any in-built libraries that can be used to parse this? (While I was researching, I found Pattern and Matcher classes but not really sure if it can be used in this context).
For example, I know how to code for this 2x^2 + 4x + 8 but not for something like -22x^2 - 652x + 898.
Any help is appreciated.

I think you are on the right trail using Pattern and Matcher Class. I found this post: Splitting a string using Regex in Java
I am not completely sure what you are looking to do with this, or what the file looks like exactly, but pivoting off the above post you could do something like:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Polynomial
{
public static void main (String[] args)
{
final String poly = "-2123x^-232-22x^2-652x+898";
// Does not handle a missing + or - on the first term
Pattern p = Pattern.compile("([-+]\\d+)x?[\\^]?([-]?\\d+)?");
Matcher m = p.matcher(poly);
while (m.find())
{
System.out.println("Entire match:" + m.group(0));
// Showing option of casting to Integer
System.out.println("Coefficient:" + Integer.parseInt(m.group(1)));
// Possibly null since the last few parts don't have exponent or x
if (null != m.group(2))
{
System.out.println("Exponent:" + m.group(2));
}
}
}
}

Related

Pattern for last occurring element

so I recently wrote a piece of regex like this:
replaceInputWithLatex("sinus(?<formula>.*?)moveout",
"\\\\sin(${formula})");
replaceInputWithLatex("sinus(?<formula>.+)",
"\\\\sin" + changeColorOfString("Red", "(") + "${formula}" + changeColorOfString("Red", ")"));
replaceInputWithLatex("sinus",
"\\\\sin" + noInputBox);
Here's replaceInputWithLatex function:
private static void replaceInputWithLatex(String pattern, String latexOutput{
regexPattern = Pattern.compile(pattern);
regexMatcher = regexPattern.matcher(mathFormula);
while(regexMatcher.find()){
Log.d("FOUND", regexMatcher.group());
mathFormula = regexMatcher.replaceAll(latexOutput);
replaceInputWithLatex(pattern, latexOutput);
}
}
Let's say I input a string: "sinus1+sinus2x+3moveout".
I would like the 1st match to take this string: "sinus2x+3moveout". And replace it. And in the next iteration match "sinus1+(already_converted)".
However, so far it takes an entire string first. Here are "FOUND" logs:
11-12 19:26:40.750 30244-30244/com.example.user.signum D/FOUND: sinus1+sinus2x+3moveout
11-12 19:26:40.750 30244-30244/com.example.user.signum D/FOUND: sinus2x+3
Latex output look like this(I want both outside parentheses to be red - in reverse order as it is now):
What pattern shall I use? (I've been trying recursive approach, but I haven't come up with a solution yet)
I've mocked up a working example in JavaScript.
Basically, we keep hitting the input with the same regex. We mark the tail end we've already handled with some special characters, then handle the next one to the left, and so on. When the markers cover the whole string, we're done.
console.clear();
var input = "sinus1+sinus2x+3moveout";
do {
console.log(input);
input = input.replace(/(sinus\dx?\+)?(?:\d?moveout|(<<handled>>))$/, function(m,lead){
return lead ? "<<handled>>" : "{{all done}}"
});
} while (input.indexOf("<<") !== -1);
console.log(input);
It's a bit pseudo-codey, but I hope this gives you some useful ideas.

How to find nested tags in LaTeX with a regex

I'm trying to extract theorems from LaTeX source with java. My code almost works, but one test case is failing – nested theorems.
#Test
public void testNestedTheorems() {
String source = "\\begin{theorem}" +
"this is the outer theorem" +
"\\begin{theorem}" +
"this is the inner theorem" +
"\\end{theorem}" +
"\\end{theorem}";
LatexTheoremProofExtractor extractor = new LatexTheoremProofExtractor(source);
extractor.parse();
ArrayList<String> theorems = extractor.getTheorems();
assertNotNull(theorems);
assertEquals(2, theorems.size()); // theorems.size() is 1
assertEquals("this is the outer theorem", theorems.get(0));
assertEquals("this is the inner theorem", theorems.get(1));
}
Here's my theorem extractor which is called by LatexTheoremProofExtractor#parse:
private void extractTheorems() {
// If this has been called before, return
if(theorems != null) {
return;
}
theorems = new ArrayList<String>();
final Matcher matcher = THEOREM_REGEX.matcher(source);
// Add trimmed matches while you can find them
while (matcher.find()) {
theorems.add(matcher.group(1).trim());
}
}
and THEOREM_REGEX is defined as follows:
private static final Pattern THEOREM_REGEX = Pattern.compile(Pattern.quote("\\begin{theorem}")
+ "(.+?)" + Pattern.quote("\\end{theorem}"));
Does anyone have recommendations for dealing with the nested tags?
If you only want to match doubly nested theorems, you can write down an explicit regular expression for it. I guess it would look something like this.
Pattern.compile(
Pattern.quote("\\begin{theorem}")
+ "("
+ "(.+?)"
+ Pattern.quote("\\begin{theorem}")
+ "(.+?)"
+ Pattern.quote("\\end{theorem}")
+ ")*"
+ Pattern.quote("\\end{theorem}"));
(This code should give you the idea but it is untested an probably does not work like written. This is not the point I want to make.)
You can continue this for triple-nesting and so forth for any fixed number of nesting you want. Needless to say that it will become rather awkward pretty soon.
However, if your goal is to match arbitrary deep nestings then it is simply impossible to do with regular expressions. The problem is that the language you want to match is not regular (but context-free). Context-free languages are strictly more powerful than regular languages and regular expressions can only match regular languages precisely. In general, you will need to construct a push-down automaton if you want to match a context-free language.
Further reading:
Chomsky hierarchy
What is meant by “Now you have two problems”?

Now, how can I screen-scrape such a html line (using java)?

I am trying to screen-scrape a html page so I can extract desired valuable data from it and into a text file. So far it's going well until I came across this within the html page:
<td> <b>In inventory</b>: 0.3 kg<br /><b>Equipped</b>: -4.5 kg
The above line in the html code for the page often varies. So it need to figure about a way to scan the line (regardless of what it contains) for the weight (in this case would be 0.3 and -4.5) and store this data into 2 seperate doubles as of such:
double inventoryWeight = 0.3 double equippedWeight = -4.5
I would like this to be done using pure java; if need be, do not hesitate to notify me of any third-party programs which can be executed within my java application to achieve this (but please vividly explain if so).
Thank you a bunch!
RegEx is usually a good solution for scraping text. Parentheses denote "capturing groups", which are stored and can then be accessed using Matcher.group(). [-.\d]+ matches anything consisting of one or more digits (0-9), periods, and hyphens. .* matches anything (but sometimes not newline characters). Here it's just used to essentially "throw away" everything you don't care about.
import java.util.regex.*;
public class Foo {
public static void main(String[] args) {
String regex = ".*inventory<\\/b>: ([-.\\d]+).*Equipped<\\/b>: ([-.\\d]+).*";
String text = "<td> <b>In inventory</b>: 0.3 kg<br /><b>Equipped</b>: -4.5 kg";
// Look for a match
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
// Get the matched text
if (matcher.matches()) {
String inventoryWeight = matcher.group(1);
String equippedWeight = matcher.group(2);
System.out.println("Inventory weight: " + inventoryWeight);
System.out.println("Equipped weight: " + equippedWeight);
} else {
System.out.println("No match!");
}
}
}
Do you have this piece of html as String? If so, just search for <b>Equipped</b>. Then get <b>Equipped</b> end char position plus one. And then build new string by appending char by char until it's not a number or dot.
When you have those numbers in String variables you simply convert them to Doubles by using double aDouble = Double.parseDouble(aString)

Java: crawl numbers

I created a java phonebook (desktop app) , on my pc there's a program that outputs the number of the caller. it's an 8 digits number.
here's how it works
I want to crawl only 8 digits numbers from the popup, so lets say this is a popup:
My name is someone like you, i am 22 years old, i was born in 19/10/1989,
my phone number is 34544512
my brother is someone like me he is 18 years old, born in 9101993
his number is 07777666
in this example, i want to crawl 07777666 and 34544512 only.
I want to check the popup window every 2s for new numbers, if a caller calls me twice, his number will be already stored my db and if not I'll store
Note: if that's can't be done, then forget about the popup, lets say it's just a text being updated every 2 seconds, how to crawl it
This not a homework lol :D
Use Java regular expressions. Create a regex of 8 or more digits and use it. You will be able to extract these 2 phone numbers from your text sample.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]) throws Exception {
String testString = "My name is someone like you, i am 22 years old, i was born in 19/10/1989,"
+ " my phone number is 34544512 3454451266"
+ " my brother is someone like me he is 18 years old, born in 9101993 "
+ " his number is 07777666";
String[] pieces = testString.split("\\s+");
String expression = "\\d{8,}";
Pattern pattern = Pattern.compile(expression);
for (int i = 0; i < pieces.length; i++) {
if (pattern.matches(expression, pieces[i]))
System.out.println(pieces[i]);
}
}
}
Haha... this is so obviously a homework exercise that you're cheating on!
Your professor probably expects you to use regular expressions. If that's over your head, then just tokenize the strings and check each token with Long.parseLong().
Of course, both of these approaches assume that the data will be exactly like your example above, and not have dashes in the phone numbers. If you need to account for dashes (or dots, spaces, etc), then the regex or manual logic gets pretty complex pretty quickly.
UPDATE: If you do need to account for phone numbers with dashes or other characters in them, I would probably:
tokenize the string,
iterate through all tokens, using regex to remove all non-numeric characters, and finally
use regex (or Long.parseLong() and String.length()) to determine whether what's left is an 8-digit number.
If you mean that you want to extract 8-digit numbers from a text String, then you can do that as follows:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex
{
public static void main(String[] args)
{
Matcher m = Pattern.compile("\\b(\\d{8})\\b").matcher(
"Hello 12345678 world 23456789");
while (m.find())
{
System.out.println(m.group(1));
}
}
}
See http://docs.oracle.com/javase/tutorial/essential/regex/

How to create article spinner regex in Java?

Say for example I want to take this phrase:
{{Hello|What's Up|Howdy} {world|planet} |
{Goodbye|Later}
{people|citizens|inhabitants}}
and randomly make it into one of the following:
Hello world
Goodbye people
What's Up word
What's Up planet
Later citizens
etc.
The basic idea is that enclosed within every pair of braces will be an unlimited number of choices separated by "|". The program needs to go through and randomly choose one choice for each set of braces. Keep in mind that braces can be nested endlessly within each other. I found a thread about this and tried to convert it to Java, but it did not work. Here is the python code that supposedly worked:
import re
from random import randint
def select(m):
choices = m.group(1).split('|')
return choices[randint(0, len(choices)-1)]
def spinner(s):
r = re.compile('{([^{}]*)}')
while True:
s, n = r.subn(select, s)
if n == 0: break
return s.strip()
Here is my attempt to convert that Python code to Java.
public String generateSpun(String text){
String spun = new String(text);
Pattern reg = Pattern.compile("{([^{}]*)}");
Matcher matcher = reg.matcher(spun);
while (matcher.find()){
spun = matcher.replaceFirst(select(matcher.group()));
}
return spun;
}
private String select(String m){
String[] choices = m.split("|");
Random random = new Random();
int index = random.nextInt(choices.length - 1);
return choices[index];
}
Unfortunately, when I try to test this by calling
generateAd("{{Hello|What's Up|Howdy} {world|planet} | {Goodbye|Later} {people|citizens|inhabitants}}");
In the main of my program, it gives me an error in the line in generateSpun where Pattern reg is declared, giving me a PatternSyntaxException.
java.util.regex.PatternSyntaxException: Illegal repetition
{([^{}]*)}
Can someone try to create a Java method that will do what I am trying to do?
Here are some of the problems with your current code:
You should reuse your compiled Pattern, instead of Pattern.compile every time
You should reuse your Random, instead of new Random every time
Be aware that String.split is regex-based, so you must split("\\|")
Be aware that curly braces in Java regex must be escaped to match literally, so Pattern.compile("\\{([^{}]*)\\}");
You should query group(1), not group() which defaults to group 0
You're using replaceFirst wrong, look up Matcher.appendReplacement/Tail instead
Random.nextInt(int n) has exclusive upper bound (like many such methods in Java)
The algorithm itself actually does not handle arbitrarily nested braces properly
Note that escaping is done by preceding with \, and as a Java string literal it needs to be doubled (i.e. "\\" contains a single character, the backslash).
Attachment
Source code and output with above fix but no major change to algorithm
To fix the regex, add backslashes before the outer { and }. These are meta-characters in Java regexes. However, I don't think that will result in a working program. You are modifying the variable spun after it has been bound to the regex, and I do not think the returned Matcher will reflect the updated value.
I also don't think the python code will work for nested choices. Have you actually tried the python code? You say it "supposedly works", but it would be wise to verify that before you spend a lot of time porting it to Java.
Well , I just created one in PHP & Python , demo here http://spin.developerscrib.com , its at a very early stage so might not work to expectation , the source code is on github : https://github.com/razzbee/razzy-spinner
Use this, will work... I did, and working great
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
and here
private String select(String m){
String[] choices = m.split("|");
Random random = new Random();
int index = random.nextInt(choices.length - 1);
return choices[index];
}
m.split("|") use m.split("\\|")
Other wise it splits each an every character
and use Pattern.compile("\\{([^{}]*)\\}");

Categories

Resources