I'm trying to do a Map-Reduce job on big text file which contains my data. To process this data I actually need another array to do lookup's with the data I'm processing.
So ideally the reducers would load this file and populate the array, then start reducing.
I'm trying to use Google Cloud platform because I get £250 free computation, Without going into massive detail about the project, I'm new to map-reduce, only having done a small university course on extreme computing and need a little direction for the best way to achieve this. There are a few things I haven't fixed yet like the output types etc but what I'm struggling with is the setup() function, exactly how to populate my array before the reducer starts reducing, any help would be greatly appreciated!
public static class MapForEquityCalculator extends Mapper<LongWritable, Text, Text, IntWritable> {
public int GetIndex(int R1, int R2, boolean IsSuited) {
if (IsSuited) {
return (int)(((13 - Math.ceil((double)Math.max(R1, R2) / 4)) * 13) + (13 - Math.ceil((double)Math.min(R1, R2) / 4)));
} else {
return (int)(((13 - Math.ceil((double)Math.min(R1, R2) / 4)) * 13) + (13 - Math.ceil((double)Math.max(R1, R2) / 4)));
}
}
public void map(LongWritable key, Text value, Context con) throws IOException, InterruptedException {
String line = value.toString();
String[] cards=line.split(",");
int ind_1 = GetIndex(Integer.parseInt(cards[0]),Integer.parseInt(cards[1]),Integer.parseInt(cards[0])%4 == Integer.parseInt(cards[1])%4);
int ind_2 = GetIndex(Integer.parseInt(cards[2]),Integer.parseInt(cards[3]),Integer.parseInt(cards[2])%4 == Integer.parseInt(cards[3])%4);
int ind_3 = GetIndex(Integer.parseInt(cards[4]),Integer.parseInt(cards[5]),Integer.parseInt(cards[4])%4 == Integer.parseInt(cards[4])%4);
Text outputKey = new Text(Integer.toString(ind_1) +"," + Integer.toString(ind_2) +"," + Integer.toString(ind_3));
Text outputValue = new Text(line);
con.write(outputKey, outputValue);
}
}
public static class ReduceForEquityCalculator extends Reducer<Text, IntWritable, Text, IntWritable> {
public static final int HandRankSize = 32487834;
public static int HR[] = new int[HandRankSize];
protected static final int littleEndianByteArrayToInt(byte[] b, int offset) {
return (b[offset + 3] << 24) + ((b[offset + 2] & 0xFF) << 16)
+ ((b[offset + 1] & 0xFF) << 8) + (b[offset] & 0xFF);
}
public void setup(Context context) throws IOException{
Path pt=new Path("gs://mybucket/HandRanks.dat");
Configuration conf = new Configuration();
FileSystem fs = pt.getFileSystem(conf);
int tableSize = HandRankSize*4;
byte[] b = new byte[tableSize];
try {
FileInputStream fr = new FileInputStream(fs.open(pt));
int bytesRead = fr.read(b, 0, tableSize);
if (bytesRead != tableSize) {
}
fr.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
for (int i = 0; i < HandRankSize; i++) {
HR[i] = littleEndianByteArrayToInt(b, i * 4);
}
}
public void reduce(Text word, Text value, Context con) throws IOException, InterruptedException {
String line = value.toString();
String[] cards_string=line.split(",");
Integer[] cards = new Integer[cards_string.length];
int i=0;
for(String str:cards_string){
cards[i]=Integer.parseInt(str);//Exception in this line
i++;
}
int c0, c1, c2, c3, c4, c5, c6;
int u0, u1, u2, u3, u4, u5;
int su0, su1, su2, su3, su4, su5;
int tu0, tu1, tu2, tu3, tu4, tu5;
u0 = HR[53+cards[0]];
u1 = HR[u0+cards[1]];
su0 = HR[53+cards[2]];
su1 = HR[su0+cards[3]];
tu0 = HR[53+cards[4]];
tu1 = HR[su0+cards[5]];
int A_B_C = 0;
int A_C_B = 0;
int A_BC = 0;
int AB_C = 0;
int AC_B = 0;
int ABC = 0;
int B_A_C = 0;
int B_C_A = 0;
int B_AC = 0;
int BC_A = 0;
int C_A_B = 0;
int C_B_A = 0;
int C_AB = 0;
for (int com1 = 1; com1 < 49; com1++) {
if (com1 != cards[0] && com1 != cards[1] && com1 != cards[2] && com1 != cards[3] && com1 != cards[4] && com1 != cards[5]) {
u2 = HR[u1+com1];
su2 = HR[su1+com1];
tu2 = HR[tu1+com1];
for (int com2 = com1 + 1; com2 < 50; com2++) {
if (com2 != cards[0] && com2 != cards[1] && com2 != cards[2] && com2 != cards[3] && com2 != cards[4] && com2 != cards[5]) {
u3 = HR[u2+com2];
su3 = HR[su2+com2];
tu3 = HR[tu2+com2];
for (int com3 = com2 + 1; com3 < 51; com3++) {
if (com3 != cards[0] && com3 != cards[1] && com3 != cards[2] && com3 != cards[3] && com3 != cards[4] && com3 != cards[5]) {
u4 = HR[u3+com3];
su4 = HR[su3+com3];
tu4 = HR[tu3+com3];
for (int com4 = com3 + 1; com4 < 52; com4++) {
if (com4 != cards[0] && com4 != cards[1] && com4 != cards[2] && com4 != cards[3] && com4 != cards[4] && com4 != cards[5]) {
u5 = HR[u4+com4];
su5 = HR[su4+com4];
tu5 = HR[tu4+com4];
for (int com5 = com4 + 1; com5 < 53; com5++) {
if (com5 != cards[0] && com5 != cards[1] && com5 != cards[2] && com5 != cards[3] && com5 != cards[4] && com5 != cards[5]) {
int rank_a = HR[u5 + com5];
int rank_b = HR[su5 + com5];
int rank_c = HR[tu5 + com5];
if (rank_a > rank_b && rank_b > rank_c) {
A_B_C++;
} else if (rank_a > rank_c && rank_c > rank_b) {
A_C_B++;
} else if (rank_a > rank_b && rank_b == rank_c) {
A_BC++;
} else if (rank_a == rank_b && rank_b > rank_c) {
AB_C++;
} else if (rank_a == rank_c && rank_c > rank_b) {
AC_B++;
} else if (rank_a == rank_c && rank_a == rank_b) {
ABC++;
} else if (rank_b > rank_a && rank_a > rank_c) {
B_A_C++;
} else if (rank_b > rank_c && rank_c > rank_a) {
B_C_A++;
} else if (rank_b > rank_a && rank_a == rank_c) {
B_AC++;
} else if (rank_b == rank_c && rank_c > rank_a) {
BC_A++;
} else if (rank_c > rank_a && rank_a > rank_b) {
C_A_B++;
} else if (rank_c > rank_b && rank_b > rank_a) {
C_B_A++;
} else {
C_AB++;
}
}
}
}
}
}
}
}
}
}
}
Text outputValue = new Text(Integer.toString(A_B_C) +"," + Integer.toString(A_C_B) + "," + Integer.toString(A_BC)+"," + Integer.toString(A_BC) +","+ Integer.toString(AB_C)+","+ Integer.toString(AC_B) +","+ Integer.toString(ABC)+"," +Integer.toString(B_A_C) +","+Integer.toString(B_C_A)+","+Integer.toString(B_AC) +","+Integer.toString(BC_A)+","+Integer.toString(C_A_B) +","+Integer.toString(C_B_A)+","+Integer.toString(C_AB) + "\n");
con.write(word, outputValue);
}
}
}
Related
Basically I'm helping a friend of mine out,
There's a disconnection error with his game (runescape private server) and there's an error that keeps being thrown, which is
DataInputStream.readFully(Unknown Source)
at Client.streamLoaderForName(Client.java:7343)
I read some things up on using while(in.available() > 0), but I don't know where it'd be placed.
This is the method where the error occurs
private CacheArchive streamLoaderForName(int i, String s, String s1, int j, int k)
{
byte abyte0[] = null;
int l = 5;
try
{
if(cacheIndices[0] != null)
abyte0 = cacheIndices[0].get(i);
}
catch(Exception _ex) { }
if(abyte0 != null)
{
CacheArchive streamLoader = new CacheArchive(abyte0);
return streamLoader;
}
int j1 = 0;
while(abyte0 == null)
{
String s2 = "Unknown error";
setLoadingText(k, "Requesting " + s);
Object obj = null;
try
{
int k1 = 0;
DataInputStream datainputstream = jaggrabRequest(s1 + j);
byte[] abyte1 = new byte[6];
datainputstream.readFully(abyte1, 0, 6);
Stream stream = new Stream(abyte1);
stream.currentOffset = 3;
int i2 = stream.read3Bytes() + 6;
int j2 = 6;
abyte0 = new byte[i2];
System.arraycopy(abyte1, 0, abyte0, 0, 6);
while(j2 < i2)
{
int l2 = i2 - j2;
if(l2 > 1000)
l2 = 1000;
int j3 = datainputstream.read(abyte0, j2, l2);
if(j3 < 0)
{
s2 = "Length error: " + j2 + "/" + i2;
throw new IOException("EOF");
}
j2 += j3;
int k3 = (j2 * 100) / i2;
k1 = k3;
}
datainputstream.close();
try
{
if(cacheIndices[0] != null)
cacheIndices[0].put(abyte0.length, abyte0, i);
}
catch(Exception _ex)
{
cacheIndices[0] = null;
}
}
catch(IOException ioexception)
{
ioexception.printStackTrace();
if(s2.equals("Unknown error"))
s2 = "Connection error";
abyte0 = null;
}
catch(NullPointerException _ex)
{
s2 = "Null error";
abyte0 = null;
}
catch(ArrayIndexOutOfBoundsException _ex)
{
s2 = "Bounds error";
abyte0 = null;
}
catch(Exception _ex)
{
s2 = "Unexpected error";
abyte0 = null;
}
if(abyte0 == null)
{
for(int l1 = l; l1 > 0; l1--)
{
if(j1 >= 3)
{
setLoadingText(k, "Game updated - please reload page");
l1 = 10;
} else
{
setLoadingText(k, s2 + " - Retrying in " + l1);
}
try
{
Thread.sleep(1000L);
}
catch(Exception _ex) { }
}
l *= 2;
if(l > 60)
l = 60;
}
}
CacheArchive streamLoader_1 = new CacheArchive(abyte0);
return streamLoader_1;
}
I can't seem to find out where to do any placements or how to fix this error. By the way, the line that's 7343, is
datainputstream.readFully(abyte1, 0, 6);
Also sorry for the poor thread, I don't know how to write it up properly on this.
I have an application in which I connected with a device which is connected by USB when I start to ping this device my application is crashed. This device is a RFID scanner which is connected to the phone by USB. A phone and my application see the connected phone with this scanner.
This is a code :
#Override
public synchronized void run() {
if (context == null || usbDevice == null) {
onStatusChanged(Status.NoContextOrUsbDeviceSpecified);
return;
}
//l("Device -> " + usbDevice);
UsbInterface usbInterface = null;
UsbEndpoint usbEndpointIn = null;
UsbEndpoint usbEndpointOut = null;
for (int i = 0; i < usbDevice.getInterfaceCount(); i++) {
usbInterface = usbDevice.getInterface(i);
//l("Interface[" + i + "] -> " + usbInterface);
if (usbInterface != null) {
for (int j = 0; j < usbInterface.getEndpointCount(); j++) {
UsbEndpoint usbEndpoint = usbInterface.getEndpoint(j);
//l("Endpoint[" + j + "] -> " + usbEndpoint);
if (usbEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (usbEndpoint.getDirection() == UsbConstants.USB_DIR_IN) {
l("Found input!");
usbEndpointIn = usbEndpoint;
}
else {
l("Found output!");
usbEndpointOut = usbEndpoint;
}
if (usbEndpointIn != null && usbEndpointOut != null) {
break;
}
}
}
if (usbEndpointIn != null && usbEndpointOut != null) {
break;
}
}
else {
//l("Interface was null");
}
}
if (usbEndpointIn == null || usbEndpointOut == null) {
onStatusChanged(Status.NoEndpointsFoundOnUsbDevice);
return;
}
UsbManager usbManager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
if (usbDeviceConnection == null) {
onStatusChanged(Status.CouldntOpenUsbDeviceConnection);
return;
}
if (!usbDeviceConnection.claimInterface(usbInterface, true)) {
onStatusChanged(Status.CouldntClaimUsbDeviceInterface);
return;
}
onStatusChanged(Status.ScanningInitializing);
currentUsbDeviceConnection = usbDeviceConnection;
currentUsbInterface = usbInterface;
currentUsbEndpointIn = usbEndpointIn;
currentUsbEndpointOut = usbEndpointOut;
// Toast.makeText(context,send(PING) + " wysłane" , Toast.LENGTH_LONG).show();
final String[] a = new String[1];
pingingThread = new Thread(() -> {
onStatusChanged(Status.PingStarted);
String s = "";
for (byte aPING : PING) {
s = s + aPING;
}
Log.e("Ping " , s);
String aa ="";
for (byte aACK : ACK) {
aa = aa + aACK;
}
Log.e("ACK " , aa);
while (shouldPing && !Thread.interrupted()) {
if (send(PING) >= 0) {
onStatusChanged(Status.PingSuccessful);
} else {
onStatusChanged(Status.PingFailed);
}
SystemClock.sleep(PING_RECEIVE_TIMEOUT / 3);
}
onStatusChanged(Status.PingStopped);
scheduleStopReceiving();
});
byte[] bytes = new byte[36];
int result = currentUsbDeviceConnection.bulkTransfer(currentUsbEndpointIn, bytes, bytes.length, RECEIVE_TIMEOUT);
// Toast.makeText(context, "result " + result , Toast.LENGTH_LONG ).show();
receivingThread = new Thread(() -> {
onStatusChanged(Status.ReceivingStarted);
long lastTagTimestamp = 0;
boolean wasInterrupted = false;
while (shouldReceive) {
byte[] received = receive(36);
if (received != null) {
if (received[0] != (byte) 0xab ||
received[1] != (byte) 0xba ||
received[2] != (byte) 0xcd ||
received[3] != (byte) 0xdc) {
onStatusChanged(Status.ReceivedUnknown);
} else if (received[4] == 0x00) {
if (send(ACK) < 0) {
continue;
}
onStatusChanged(Status.ReceivedTag);
lastTagTimestamp = System.currentTimeMillis();
byte[] tag = new byte[8];
int index = 0;
while (index < tag.length) {
tag[index++] = received[8 + index];
}
long longValue = ByteBuffer.wrap(tag).getLong();
String hexValue = Long.toHexString(longValue).toUpperCase();
onScanCompleted(hexValue);
} else if (received[4] == 0x02) {
onStatusChanged(Status.ReceivedPing);
}
}
if (Thread.interrupted()) {
wasInterrupted = true;
}
if (wasInterrupted) {
if (lastTagTimestamp == 0) {
lastTagTimestamp = System.currentTimeMillis();
}
if (lastTagTimestamp + PING_RECEIVE_TIMEOUT < System.currentTimeMillis()) {
break;
}
}
}
onStatusChanged(Status.ReceivingStopped);
close();
});
shouldPing = true;
shouldReceive = true;
pingingThread.start();
receivingThread.start();
}
In this line I have
java.lang.NoSuchMethodError: No direct method
(Ljava/lang/Object;)V in class
Lrest/hello/org/usbdevice/rfid2/-$Lambda$2; or its super classes
(declaration of 'rest.hello.org.usbdevice.rfid2.-$Lambda$2' appears in
/data/app/rest.hello.org.usbdevice-2/base.apk)
pingingThread = new Thread(() -> {
onStatusChanged(Status.PingStarted);
String s = "";
for (byte aPING : PING) {
s = s + aPING;
}
Log.e("Ping " , s);
String aa ="";
for (byte aACK : ACK) {
aa = aa + aACK;
}
Log.e("ACK " , aa);
Here is my SentiWorNet Algo:
public class SWN3 {
private String pathToSWN = "C:/Users/RAHUL/Desktop/SWN/SentiWordNet_3.0.0.txt";
private HashMap<String, Double>_dict;
public SWN3(){
_dict = new HashMap<String, Double>();
HashMap<String, Vector<Double>> _temp = new HashMap<String, Vector<Double>>();
try{
BufferedReader csv = new BufferedReader(new FileReader(pathToSWN));
String line = "";
while((line = csv.readLine()) != null)
{
String[] data = line.split("\t");
Double score = Double.parseDouble(data[2])-Double.parseDouble(data[3]);
String[] words = data[4].split(" ");
for(String w:words)
{
String[] w_n = w.split("#");
w_n[0] += "#"+data[0];
int index = Integer.parseInt(w_n[1])-1;
if(_temp.containsKey(w_n[0]))
{
Vector<Double> v = _temp.get(w_n[0]);
if(index>v.size())
for(int i = v.size();i<index; i++)
v.add(0.0);
v.add(index, score);
_temp.put(w_n[0], v);
}
else
{
Vector<Double> v = new Vector<Double>();
for(int i = 0;i<index; i++)
v.add(0.0);
v.add(index, score);
_temp.put(w_n[0], v);
}
}
}
Set<String> temp = _temp.keySet();
for (Iterator<String> iterator = temp.iterator(); iterator.hasNext();) {
String word = iterator.next();
Vector<Double> v = _temp.get(word);
double score = 0.0;
double sum = 0.0;
for(int i = 0; i < v.size(); i++)
score += ((double)1/(double)(i+1))*v.get(i);
for(int i = 1; i<=v.size(); i++)
sum += (double)1/(double)i;
score /= sum;
String sent = "";
if(score>=0.75)
sent = "strong_positive";
else
if(score > 0.50 && score<0.75)
sent = "moderately_positive";
else
if(score > 0.25 && score>=0.50)
sent = "positive";
else
if(score > 0 && score>=0.25)
sent = "weak_positive";
else
if(score < 0 && score>=-0.25)
sent = "weak_negative";
else
if(score < -0.25 && score>=-0.5)
sent = "negative";
else
if(score < -0.50 && score>-0.75)
sent = "moderately_negative";
else
if(score<=-0.75)
sent = "strong_negative";
_dict.put(word, score);
}
}
catch(Exception e){e.printStackTrace();}
}
public Double extract(String word)
{
Double total = new Double(0);
if(_dict.get(word+"#n") != null)
total = _dict.get(word+"#n") + total;
if(_dict.get(word+"#a") != null)
total = _dict.get(word+"#a") + total;
if(_dict.get(word+"#r") != null)
total = _dict.get(word+"#r") + total;
if(_dict.get(word+"#v") != null)
total = _dict.get(word+"#v") + total;
return total;
}
public static String SentiWord(String stri) {
SWN3 test = new SWN3();
String sentence=stri;
String[] words = sentence.split("\\s+");
double totalScore = 0;
for(String word : words) {
word = word.replaceAll("([^a-zA-Z\\s])", "");
if (test.extract(word) == null)
continue;
totalScore += test.extract(word);
}
String sent = "";
if(totalScore>=0.75)
sent = "strong_positive";
else
if(totalScore > 0.25 && totalScore<0.75)
sent = "positive";
....
....
return sent;
}
}
And here is my Pos Tagger method:
public class TagText {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
// Initialize the tagger
MaxentTagger tagger = new MaxentTagger("taggers/english-left3words-distsim.tagger");
// The sample string
String sample = "This is a sample text";
// The tagged string
String tagged = tagger.tagString(sample);
//output the tagged sample string onto your console
System.out.println("Input: " + sample);
System.out.println("Output: "+ tagged);
}
}
I need POS Tagger to be integrated with SentiwordNet.I am trying to make a system for Sentimental analysis. Right now this SentiwordNet code is working fine without pos tagging but not giving good results. I just cannot figure it out. Please help.
You could adapt your extract method in SWN3 like this:
public Double extract(String word, String tail) {
if (tail.contains("NN") || tail.contains("NNS")
|| tail.contains("NNP")
|| tail.contains("NNPS"))
return _dict.get(word + "#n");
else if (tail.contains("VB") || tail.contains("VBD")
|| tail.contains("VBG") || tail.contains("VBN")
|| tail.contains("VBP") || tail.contains("VBZ"))
return _dict.get(word + "#v");
else if (tail.contains("JJ") || tail.contains("JJR")
|| tail.contains("JJS"))
return _dict.get(word + "#a");
else if (tail.contains("RB") || tail.contains("RBR")
|| tail.contains("RBS"))
return _dict.get(word + "#r");
else
return null;
}
It maps the tags with the types of words as defined in SentiWordNet. I suggest to change your main method like this:
public static void main(String[] args) {
MaxentTagger tagger = new MaxentTagger("files/english-left3words-distsim.tagger");
//String sample = "This is a sample text";
String sample = "It works much better with this great example!";
sample = sample.replaceAll("([^a-zA-Z\\s])", "");
String[] words = sample.split("\\s+");
String taggedSample = tagger.tagString(sample);
String[] taggedWords = taggedSample.split("\\s+");
System.out.println(tagger.tagString(sample));
double totalScore = 0;
SWN3 test = new SWN3();
System.out.println("-----------");
for (int i=0; i<taggedWords.length;i++) {
String tail = taggedWords[i].substring(words[i].length() + 1);
Double score = null;
if(tail!=null{
score = test.extract(words[i], tail);
System.out.println(taggedWords[i] + "\t" + words[i] + "\t" + tail + "\t" + score);
}
if (score == null)
continue;
totalScore += score;
}
System.out.println("-----------");
System.out.println(totalScore);
}
I used another sentence in sample where it works better. Note that tagging the sentence and tagging words individually can lead to different results.
I hope it helps.
I have a series of notifications which appear two at a time to display a series of a total of 6-10 instructions (depending on the results of a web service call.) This portion of the code is working fine. The problem is when clicking BACKWARDS through the notifications cycle (if you click any of the two displayed notifications it will display the previous two notifications) it gets hungup at step 5 and does not display the 5th notifications and I'm having a bit of trouble determining why.
SCREENSHOT:
http://datasettings.site90.net/Screenshot_2013-10-03-11-37-20.png
SPECIFIC SOURCE:
private void setNotificationStrings(Bundle extras) {
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// The code below sets the values of the current (field and value),
// previous, and beforePrevious
// Those hold the strings to display in the 2 notifications and the
// strings to go back to
// if the user decides to back track
if (i == count) {
done = true;
}
if (extras != null) {
if (((tm.getSimOperator()).equals(getString(R.string.numeric_tmo)))) {
Log.w(TAG, "Extras not null");
previousValue = extras.getString(getString(R.string.config_name_label));
value = extras.getString(getString(R.string.apn_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.config_name_label);
previousValue = valuez.getAsString("name");
Log.w("previousValue", previousValue);
beforePreviousField = "step1";
beforePreviousValue = "> New APN";
//
field = getString(R.string.apn_label);
value = valuez.getAsString("apn");
extras.clear();
currentStep = 2;
if (i == 1) {
j++;
}
Log.d("currentStep = ", " 2");
} else {
previousValue = extras.getString(getString(R.string.apn_label));
if (proxyArr.size() > 0)// if proxy and port are not null
value = extras.getString(getString(R.string.proxy_label));
else {
value = extras.getString(getString(R.string.mmsc_label));
}
if (previousValue != null && value == null) {
previousField = getString(R.string.apn_label);
beforePreviousField = getString(R.string.config_name_label);
beforePreviousValue = valuez.getAsString("name");
if (proxyArr.size() > 0) {
field = getString(R.string.proxy_label);
value = valuez.getAsString("proxy");
} else {
field = getString(R.string.mmsc_label);
value = valuez.getAsString("mmsc");
}
extras.clear();
currentStep = 3;
Log.d("currentStep = ", " 3");
} else {
if (proxyArr.size() > 0) {// if proxy and port are not
// null
previousValue = extras.getString(getString(R.string.apn_label));
value = extras.getString(getString(R.string.port_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.apn_label);
beforePreviousField = getString(R.string.config_name_label);
beforePreviousValue = valuez.getAsString("name");
field = getString(R.string.port_label);
value = valuez.getAsString("port");
extras.clear();
currentStep = 4;
Log.d("currentStep = ", " 4");
} else {
previousValue = extras.getString(getString(R.string.port_label));
value = extras.getString(getString(R.string.mmsc_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.port_label);
beforePreviousField = getString(R.string.proxy_label);
beforePreviousValue = valuez.getAsString("proxy");
field = getString(R.string.mmsc_label);
value = valuez.getAsString("mmsc");
extras.clear();
currentStep = 5;
Log.d("currentStep = ", " 5");
} else {
previousValue = extras.getString(getString(R.string.port_label));
if (mmsproxyArr.size() > 0) {// if mmsproxy
// and
// mmsport
// are not
// null
value = extras.getString(getString(R.string.mms_proxy_label));
} else {
value = extras.getString(getString(R.string.type_label));
}
if (previousValue != null && value == null) {
previousField = getString(R.string.mmsc_label);
beforePreviousField = getString(R.string.port_label);
beforePreviousValue = valuez.getAsString("port");
if (mmsproxyArr.size() > 0) {
field = getString(R.string.mms_proxy_label);
value = valuez.getAsString("mmsproxy");
} else {
field = getString(R.string.type_label);
value = getString(R.string.type);
}
extras.clear();
currentStep = 6;
if (i < nameArr.size() - 1 && currentStep == 6) {
i++;
Log.d("currentStep = ", " 6");
} else {
if (mmsproxyArr.size() > 0) {// if
// mmsproxy
// and
// mmsport
// are
// not
// null
previousValue = extras.getString(getString(R.string.mms_proxy_label));
value = extras.getString(getString(R.string.mms_port_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.mms_proxy_label);
beforePreviousField = getString(R.string.mmsc_label);
beforePreviousValue = valuez.getAsString("mmsc");
field = getString(R.string.mms_port_label);
value = valuez.getAsString("mmsport");
extras.clear();
currentStep = 7;
Log.d("currentStep = ", " 7");
} else {
previousValue = extras.getString(getString(R.string.mms_port_label));
value = extras.getString(getString(R.string.type_label));
if (previousValue != null && value == null) {
previousField = getString(R.string.mms_port_label);
beforePreviousField = getString(R.string.mms_proxy_label);
beforePreviousValue = valuez.getAsString("mmsproxy");
field = getString(R.string.type_label);
value = getString(R.string.type);
extras.clear();
currentStep = 8;
Log.d("currentStep = "," 8");
FULL SOURCE:
https://docs.google.com/document/d/1EjAo4bpnEjj6jExt7RY28EiA-fZIa1ExMBOfxzdGc3I/edit?usp=sharing
My code is getting stuck in the second while loop in my hash search method. It seems like the conditions keep staying true, when they should eventually become false if the key is not in the dataset or if the key is found. Can someone help me find the bug?
Thanks
//////
this is the whole.. i been working on it.. still cant find the error.
public void HashedSearch()
{
HSAverageAccessTime = 0;
HSAverageCompSuc = 0;
HSAverageCompFailed = 0;
HSNumberKeysSuc = 0;
HSNumberKeysFailed = 0;
Initialize();
int SearchKey;
int TotalNumberOfComparisons;
int address;
int M;
M = FindPrime();
for(int i=0; i<NumberOfDataItems; i++)
{
address = OriginalArray[i] % M;
if (HashedArray[address]== -1)
HashedArray[address]= OriginalArray[i];
else
{
address = (address+1) % M;
while(HashedArray[address]!=-1)
{
address=(address+1)%M;
}
HashedArray[address]=OriginalArray[i];
}
}
System.out.println("after mapping" + M);
long startTime = System.nanoTime();
boolean found = false;
for (int k = 0; k <NumberOfKeys; k++)
{
found = false;
SearchKey = KeysArray[k];
TotalNumberOfComparisons = 0;
address = KeysArray[k] % M;
//System.out.println("address" + address);
//System.out.println(" inside if 1 --- address" + address);
while ( HashedArray[address]!= SearchKey && HashedArray[address]!= -1)
{
if (HashedArray [address] == SearchKey)
{
found = true;
TotalNumberOfComparisons++;
HSAverageCompSuc = HSAverageCompSuc + TotalNumberOfComparisons;
BSNumberKeysSuc ++;
}
else
{
System.out.println("Stuck after here");
HSAverageCompFailed = HSAverageCompFailed + TotalNumberOfComparisons;
HSNumberKeysFailed ++;
//address=(address+1)%M;
address++;
}
System.out.println(" outside while --- found" + found);
//if(HashedArray[address] == SearchKey)
//found = true;
//else found = false;
//address=(address+1)%M;
//address++;
}
if(found)
{
HSAverageCompSuc = HSAverageCompSuc + TotalNumberOfComparisons;
BSNumberKeysSuc ++;
}
else
{
HSAverageCompFailed = HSAverageCompFailed + TotalNumberOfComparisons;
HSNumberKeysFailed ++;
}
}
long estimatedTime = System.nanoTime() - startTime;
if (NumberOfKeys != 0)
HSAverageAccessTime = Math.round((estimatedTime/NumberOfKeys));
else
HSAverageAccessTime = 0;
if(HSNumberKeysSuc != 0)
HSAverageCompSuc = Math.round (HSAverageCompSuc / HSNumberKeysSuc) ;
else
HSAverageCompSuc = 0;
if (HSNumberKeysFailed != 0)
HSAverageCompFailed = Math.round (HSAverageCompFailed / HSNumberKeysFailed) ;
else
HSNumberKeysFailed = 0;
System.out.println("time after search" + estimatedTime);
return;
}
while ( HashedArray[address]!= SearchKey && HashedArray[address]!= -1)
{
address=(address+1)%M;
}
Your loop never terminates if your SearchKey or -1 is not in HashedArray. I cannot give you any more clarifications unless you post the complete code.
#Bashir #KevinDiTraglia #Steinar
import javax.swing.*;
//File-related imports
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.util.Arrays;
import java.math.*;
public class ArrayOperations
{
// File Parameters
String DataFilePath;
String DataFileName;
String KeysFilePath;
String KeysFileName;
int NumberOfDataItems;
int NumberOfKeys ;
int N ;
int BucketHashArraySize;
int NoBuckets;
//Array
int[] OriginalArray = new int[1000000];
int[] SortedArray = new int[1000000];
int[] HashedArray = new int[2000000];
int[] BucketHashedArray = new int[2000000];
int[] KeysArray = new int[1000000];
long SSAverageAccessTime;
long SSAverageCompSuc ;
long SSAverageCompFailed;
long SSNumberKeysSuc ;
long SSNumberKeysFailed ;
long BSAverageAccessTime;
long BSAverageCompSuc ;
long BSAverageCompFailed;
long BSNumberKeysSuc ;
long BSNumberKeysFailed ;
long HSAverageAccessTime;
long HSAverageCompSuc ;
long HSAverageCompFailed;
long HSNumberKeysSuc ;
long HSNumberKeysFailed ;
public ArrayOperations()
{
// File Parameters
DataFilePath = null;
DataFileName = null;
KeysFilePath = null;
KeysFileName = null;
NumberOfDataItems=0;
NumberOfKeys =0;
N =0;
BucketHashArraySize = 0;
NoBuckets =0;
// Statistics
SSAverageAccessTime = 0;
SSAverageCompSuc = 0;
SSAverageCompFailed = 0;
SSNumberKeysSuc = 0;
SSNumberKeysFailed = 0;
BSAverageAccessTime = 0;
BSAverageCompSuc = 0;
BSAverageCompFailed = 0;
BSNumberKeysSuc = 0;
BSNumberKeysFailed = 0;
HSAverageAccessTime = 0;
HSAverageCompSuc = 0;
HSAverageCompFailed = 0;
HSNumberKeysSuc = 0;
HSNumberKeysFailed = 0;
}
public void ReadDataFile() throws IOException
{
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG );
chooser.setDialogTitle("Open Data File");
int returnVal = chooser.showOpenDialog(null);
if( returnVal == JFileChooser.APPROVE_OPTION)
{
DataFilePath = chooser.getSelectedFile().getPath();
DataFileName = chooser.getSelectedFile().getName();
}
// read data file and copy it to original array
if (DataFilePath != null)
{
try
{
int index = 0;
Scanner integerTextFile = new Scanner(new File(DataFilePath));
while (integerTextFile.hasNext())
{
// read the next integer
OriginalArray[index] = integerTextFile.nextInt();
index++;
}
// end of file detected
integerTextFile.close();
NumberOfDataItems = index;
}
catch (IOException ioe)
{
System.exit(0);
}
}
else
NumberOfDataItems = 0;
}
public void ReadKeysFile() throws IOException
{
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG );
chooser.setDialogTitle("Open Keys File");
int returnVal = chooser.showOpenDialog(null);
if( returnVal == JFileChooser.APPROVE_OPTION)
{
KeysFilePath = chooser.getSelectedFile().getPath();
KeysFileName = chooser.getSelectedFile().getName();
}
// read data file and copy it to original array
if (KeysFilePath != null)
{
try
{
int index = 0;
Scanner integerTextFile = new Scanner(new File(KeysFilePath));
while (integerTextFile.hasNext())
{
// read the next integer
KeysArray[index]= integerTextFile.nextInt();
index++;
}
// end of file detected
integerTextFile.close();
NumberOfKeys = index;
}
catch (IOException ioe)
{
System.exit(0);
}
}
else
NumberOfKeys = 0;
}
public void SequentialSearch()
{
SSAverageAccessTime = 0;
SSAverageCompSuc = 0;
SSAverageCompFailed = 0;
SSNumberKeysSuc = 0;
SSNumberKeysFailed = 0;
int SearchKey;
int TotalNumberOfComparisons;
long startTime = System.nanoTime();
boolean found = false;
for (int k=0; k<NumberOfKeys; k++)
{
found = false;
SearchKey = KeysArray[k];
TotalNumberOfComparisons = 0;
for (int d=0; d<NumberOfDataItems; d++)
{
TotalNumberOfComparisons++;
if (SearchKey == OriginalArray[d])
{
found = true;
}
if (found)break;
}
if(found)
{
SSAverageCompSuc = SSAverageCompSuc + TotalNumberOfComparisons;
SSNumberKeysSuc ++;
}
else
{
SSAverageCompFailed = SSAverageCompFailed + TotalNumberOfComparisons;
SSNumberKeysFailed ++;
}
}
long estimatedTime = System.nanoTime() - startTime;
if (NumberOfKeys != 0)
SSAverageAccessTime = Math.round((estimatedTime/NumberOfKeys));
else
SSAverageAccessTime = 0;
if(SSNumberKeysSuc != 0)
SSAverageCompSuc = Math.round (SSAverageCompSuc / SSNumberKeysSuc) ;
else
SSAverageCompSuc = 0;
if (SSNumberKeysFailed != 0)
SSAverageCompFailed = Math.round (SSAverageCompFailed / SSNumberKeysFailed) ;
else
SSNumberKeysFailed = 0;
return;
}
public void BinarySearch()
{
BSAverageAccessTime = 0 ;
BSAverageCompSuc = 0 ;
BSAverageCompFailed = 0 ;
BSNumberKeysSuc = 0 ;
BSNumberKeysFailed = 0 ;
int SearchKey;
int TotalNumberOfComparisons;
boolean found = false;
for (int i=0; i<NumberOfDataItems; i++)
{
SortedArray[i] = OriginalArray[i];
}
Arrays.sort(SortedArray);
long startTime = System.nanoTime();
for (int k=0; k<NumberOfKeys; k++)
{
found = false;
SearchKey = KeysArray[k];
TotalNumberOfComparisons = 0;
//binary starts
int start = 0;
int end = SortedArray.length - 1;
int middle;
while ( end >= start )
{
middle = ( start + end )/ 2; // element in middle of array
if ( SortedArray[middle] == SearchKey )
{
TotalNumberOfComparisons++;
found = true;
}
else
if ( SortedArray[middle] > SearchKey )
{ TotalNumberOfComparisons++;
end = middle - 1;
} // search left side of array
else
{ TotalNumberOfComparisons++;
start = middle + 1; } // search right side of array
if (found)
break;
//binaryends
}
if(found)
{
BSAverageCompSuc = BSAverageCompSuc + TotalNumberOfComparisons;
BSNumberKeysSuc ++;
}
else
{
BSAverageCompFailed = BSAverageCompFailed + TotalNumberOfComparisons;
BSNumberKeysFailed ++;
}
}
long estimatedTime = System.nanoTime() - startTime;
if (NumberOfKeys != 0)
BSAverageAccessTime = Math.round((estimatedTime/NumberOfKeys));
else
BSAverageAccessTime = 0;
if(BSNumberKeysSuc != 0)
BSAverageCompSuc = Math.round (BSAverageCompSuc / BSNumberKeysSuc) ;
else
BSAverageCompSuc = 0;
if (BSNumberKeysFailed != 0)
BSAverageCompFailed = Math.round (BSAverageCompFailed / BSNumberKeysFailed) ;
else
BSNumberKeysFailed = 0;
return;
}
public void HashedSearch()
{
HSAverageAccessTime = 0;
HSAverageCompSuc = 0;
HSAverageCompFailed = 0;
HSNumberKeysSuc = 0;
HSNumberKeysFailed = 0;
Initialize();
int SearchKey;
int TotalNumberOfComparisons;
int address;
int M;
M = FindPrime();
for(int i=0; i<NumberOfDataItems; i++)
{
address = OriginalArray[i] % M;
if (HashedArray[address]== -1)
HashedArray[address]= OriginalArray[i];
else
{
address = (address+1) % M;
while(HashedArray[address]!=-1)
{
address=(address+1)%M;
}
HashedArray[address]=OriginalArray[i];
}
}
System.out.println("after mapping" + M);
long startTime = System.nanoTime();
boolean found = false;
for (int k = 0; k <NumberOfKeys; k++)
{
found = false;
SearchKey = KeysArray[k];
TotalNumberOfComparisons = 0;
address = KeysArray[k] % M;
//System.out.println("address" + address);
//System.out.println(" inside if 1 --- address" + address);
while ( HashedArray[address]!= SearchKey && HashedArray[address]!= -1)
{
if (HashedArray [address] == SearchKey)
{
found = true;
TotalNumberOfComparisons++;
HSAverageCompSuc = HSAverageCompSuc + TotalNumberOfComparisons;
BSNumberKeysSuc ++;
}
else
{
System.out.println("Stuck after here");
HSAverageCompFailed = HSAverageCompFailed + TotalNumberOfComparisons;
HSNumberKeysFailed ++;
//address=(address+1)%M;
address++;
}
System.out.println(" outside while --- found" + found);
//if(HashedArray[address] == SearchKey)
//found = true;
//else found = false;
//address=(address+1)%M;
//address++;
}
if(found)
{
HSAverageCompSuc = HSAverageCompSuc + TotalNumberOfComparisons;
BSNumberKeysSuc ++;
}
else
{
HSAverageCompFailed = HSAverageCompFailed + TotalNumberOfComparisons;
HSNumberKeysFailed ++;
}
}
long estimatedTime = System.nanoTime() - startTime;
if (NumberOfKeys != 0)
HSAverageAccessTime = Math.round((estimatedTime/NumberOfKeys));
else
HSAverageAccessTime = 0;
if(HSNumberKeysSuc != 0)
HSAverageCompSuc = Math.round (HSAverageCompSuc / HSNumberKeysSuc) ;
else
HSAverageCompSuc = 0;
if (HSNumberKeysFailed != 0)
HSAverageCompFailed = Math.round (HSAverageCompFailed / HSNumberKeysFailed) ;
else
HSNumberKeysFailed = 0;
System.out.println("time after search" + estimatedTime);
return;
}
public int FindPrime()
{
boolean prime = true;
for(int i=NumberOfDataItems * 2-1; i>=0; i--)
{
System.out.println(" i = " +i);
prime = true;
for(int J =2; J< Math.sqrt(i); J++)
{
if (i % J == 0)
{
prime = false;
}
if (prime == false) break;
}
if (prime == true)
{
System.out.println(i);
return i;
}
}
return -1;
}
public void Initialize()
{
for (int i=0; i<NumberOfDataItems; i++)
{
HashedArray[i] = -1;
}
}
public void BHSearch()
{
}
}