Commit a87b64e9 authored by Vyshna-Sri's avatar Vyshna-Sri

Probability Calculation

parent ee3771bf
......@@ -16,6 +16,7 @@ android {
install "pillow"
install "opencv-contrib-python-headless"
install "fuzzywuzzy"
install "nltk"
}
}
......@@ -40,7 +41,7 @@ android {
// python.exe file path *********** please change this according to your file path
python{
buildPython "C:/Users/user/AppData/Local/Programs/Python/Python39/python.exe"
buildPython "C:/Users/Cool/AppData/Local/Programs/Python/Python39/python.exe"
}
}
......
......@@ -42,8 +42,8 @@ public class ExtractData {
}
Iterator it = dataSet.iterator();
//System.out.println("datset in extract "+ dataSet.toString());
//System.out.println("datset in extract size"+ dataSet.size());
System.out.println("datset in extract "+ dataSet.toString());
System.out.println("datset in extract size"+ dataSet.size());
//System.out.println("datset in extract "+ counter);
}
......
......@@ -41,8 +41,8 @@ public class ExtractValueForRandomVariable {
}
Iterator it = randomVariable.iterator();
//System.out.println("random variable in extract "+ randomVariable.toString());
//System.out.println("random variable in extract size"+ randomVariable.size());
System.out.println("random variable in extract "+ randomVariable.toString());
System.out.println("random variable in extract size"+ randomVariable.size());
//System.out.println("random variable in extract "+ counter);
}
......
package com.elearning.probabilityandstatisticsanalyserandeducator.dataExtraction;
class ProbabilityCalculationCSVfile {
private String question;
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
package com.elearning.probabilityandstatisticsanalyserandeducator.dataExtraction;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.elearning.probabilityandstatisticsanalyserandeducator.inputMethod.ProbabilityCalculationIM;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class ProbabilityCalculationQuestionComparation {
Python py=null;
ProbabilityCalculationIM probabilityCalculationIM;
String a="If you draw two cards from a deck, what is the probability that you will get the Ace of Diamonds and a black card";
public String ProbabilityCalculationomparewithCSV(String Probabilitycalculationquestions, List<String> reading) {
System.out.println("reading probability calculation questions in Probability calculation "+reading);
String[] keywordlist = {"and","or","Probability","probability","Distribution","distribution","Random","random","Variable","variable","Normal","normal","Exponential","exponential","Element","element","Discrete","discrete","Function","function","P(X = x)̄","ΣP(x)=1","mass","Mass","cumulative","Cumulative","F(x)","P(X≤x)","mean","Mean","Expected","Value","expected","value","E(X)","Variance","variance","var(X)","V(X)","Standard","standard","Deviation","deviation","σ","μ"};
//String[] keywordlist1 = {"Ace","ace","2","3","4","5","6","7","8","9","10","Jack","jack","Queen","queen","King","king","Jocker","jocker"};
//String[] keywordlist2 = {"Red","red","Black","black"};
//String[] keywordlist3 = {"Spade","spade","Club","club","Heart","heart","Diamond","diamond"};
String b="A card is drawn at random from a pack of 52 playing cards. Find the probability that the card drawn is (i) a king (ii) neither a queen nor a jack.";
List<String> questions ;
List<String> matchprocalkeyword = new ArrayList<>();
questions= reading;
String[] dataReader = new String[questions.size()];
questions.toArray(dataReader);
for (int i=0; i<dataReader.length; i++){
}
String questionlist = "";
for (String s : questions){
questionlist += s + "\t";
}
py = Python.getInstance();
//PyObject pyo = py.getModule("probabilitycalculation");
PyObject pyo1 = py.getModule("probabilitycalculation_keywords");
//PyObject obj = pyo.callAttr("main",a,dataReader);
PyObject obj1 = pyo1.callAttr("matchkeyword",keywordlist,Probabilitycalculationquestions);
//String result=obj.toString();
String result1=obj1.toString();
// System.out.println("output matching: "+result);
// System.out.println("matching: "+result1);
matchprocalkeyword = QuestionMatch(result1);
return result1;
}
private List<String> readProbCalculationQuestions(InputStream in){
String[] printQuestion = new String[500];
List<ProbabilityCalculationCSVfile> probCalculationQuestions= new ArrayList<>();
List<String> StringprobcalculationQuestions= new ArrayList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, Charset.forName("UTF-8"))
);
String line = "";
try {
while ((line = reader.readLine()) != null) {
//Split by '"'
String tokens = line;
//Read the csv
ProbabilityCalculationCSVfile question = new ProbabilityCalculationCSVfile();
question.setQuestion(tokens);
probCalculationQuestions.add(question);
StringprobcalculationQuestions.add(tokens);
//printQuestion[lines] = line;
// Log.d("MyActivity", "Just created: " + question);
}
} catch (IOException e) {
//Log.wtf("MyActivity", "Error reading data file on line " + line, e);
e.printStackTrace();
}
finally {
try {
reader.close();
}catch (IOException e) {
e.printStackTrace();
}
}
return StringprobcalculationQuestions;
}
String data;
public List<String> QuestionMatch(String highest) {
int counter = 0;
List<String> wordlist = new ArrayList<String>();
List<String> matchword = new ArrayList<String>();
List<String> valueAccordingtoLine = new ArrayList<>();
String word;
return valueAccordingtoLine;
}
public List<String> ExtractProbValueFromCalculationQuestion(String probabilityvalue) {
List<String> extractprobabilityvalues = new ArrayList<String>();
Pattern pattern1 = Pattern.compile("\\b(0(\\.\\d+)?|1\\.0)");
Matcher matcher1 = pattern1.matcher(probabilityvalue);
boolean value2 = matcher1.matches();
while (matcher1.find()) {
//System.out.println("'" + matcher2.group().trim() + "'");
extractprobabilityvalues.add(matcher1.group().trim());
}
System.out.println("matched data set of extract " + extractprobabilityvalues);
return extractprobabilityvalues;
}
}
......@@ -86,7 +86,7 @@ public class ProbabilityDistributionQuestionComparation {
String[] printQuestion = new String[500];
List<ProbabiltyDistributionCSVfile> probDistributionQuestions= new ArrayList<>();
List<String> StringprobdistributionQuestions= new ArrayList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, Charset.forName("UTF-8"))
......
......@@ -41,8 +41,8 @@ public class ProbabilityValue {
}
Iterator it = probabilityvalue.iterator();
//System.out.println("probability value in extract "+ probabilityvalue.toString());
//System.out.println("probability value in extract size"+ probabilityvalue.size());
System.out.println("probability value in extract "+ probabilityvalue.toString());
System.out.println("probability value in extract size"+ probabilityvalue.size());
// System.out.println("probability value in extract "+ counter);
}
......
import re
regex = r"mean.*?\d+(?:,\d+)*(?:\d+|\.\d+?)"
test_str = "1. 21 bricks have a mean mass of 24.2 kg, and 29 similar bricks have a mass of 23.6 kg. Determine the mean mass of the 50 bricks."
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
\ No newline at end of file
import re
S = "2. 21 bricks have a mean mass of 24.2 kg, and 29 similar bricks have a mass of (23.6) kg. Determine the mean mass of the 50 bricks."
pattern = re.compile(r'\-?(\d+(?:\.\d+)?)')
data = list(map(float, re.findall(pattern, S)))
print(data)
\ No newline at end of file
from polyfuzz.models import EditDistance, TFIDF, Embeddings
from flair.embeddings import TransformerWordEmbeddings
from polyfuzz import PolyFuzz
import pandas as pd
from_list= ["What is the probability that when two cards are drawn from a deck of cards without replacement that both of them will be 8’s?",
"What is the probability that both cards drawn (without replacement) will be spades?",
"What is the probability of drawing a red king and then a black 7 without replacement?",
"What is the probability of being dealt a flush (5 cards of all the same suit) from the first 5 cards in a deck?",
"What is the probability of drawing two face cards, and then 2 numbered cards, without replacement?",
"What is the probability of drawing an Ace 3 times in a row with replacement?",
"A card is drawn at random from a pack of 52 playing cards. Find the probability that the card drawn is (i) a king (ii) neither a queen nor a jack.",
"A card is drawn from a well-shuffled pack of 52 cards. Find the probability of getting: (i) ‘2’ of spades (ii) a jack (iii) a king of red colour (iv) a card of diamond (v) a king or a queen",
"A card is drawn from a well shuffled pack of 52 cards. Find the probability of: (i) ‘2’ of spades (ii) a jack (iii) a king of red colour (iv) a card of diamond (v) a king or a queen (vi) a non-face card (vii) a black face card (viii) a black card (ix) a non-ace (x) non-face card of black colour (xi) neither a spade nor a jack (xii) neither a heart nor a red king",
"A card is drawn at random from a well-shuffled pack of cards numbered 1 to 20. Find the probability of (i) getting a number less than 7 (ii) getting a number divisible by 3.",
"If you pull 2 cards out of a deck, what is the probability that both are spades?"]
to_list = ["What is the probability of drawing a red king and then a black 7 without replacement?"]
embeddings = TransformerWordEmbeddings('bert-base-multilingual-cased')
bert = Embeddings(embeddings, min_similarity=0, model_id="BERT")
tfidf = TFIDF(min_similarity=0)
edit = EditDistance()
string_models = [bert, tfidf, edit]
model = PolyFuzz(string_models)
model.match(from_list, to_list)
model.get_matches("BERT")
\ No newline at end of file
from nltk.tokenize import word_tokenize
from nltk.util import ngrams
def get_ngrams(text, n=2 ):
n_grams = ngrams(word_tokenize(text), n=2)
dataset= [' '.join(grams) for grams in n_grams]
return dataset
# function
def common_member(a, b):
dataset=[]
a_set = set(a)
b_set = set(b)
# check length
if len(a_set.intersection(b_set)) > 0:
dataset=[]
dataset.append(a_set.intersection(b_set))
#datasets=dataset
#print(dataset)
return dataset
\ No newline at end of file
def word2vec(word):
from collections import Counter
from math import sqrt
# count the characters in word
cw = Counter(word)
# precomputes a set of the different characters
sw = set(cw)
# precomputes the "length" of the word vector
lw = sqrt(sum(c*c for c in cw.values()))
# return a tuple
return cw, sw, lw
def cosdis(v1, v2):
# which characters are common to the two words?
common = v1[1].intersection(v2[1])
# by definition of cosine distance we have
return sum(v1[0][ch]*v2[0][ch] for ch in common)/v1[2]/v2[2]
def matchkeyword(klist,klist1,sen):
matchedkeywordlist=[]
list_of_keywords = ['1','2','3','4','5','6']
list_of_keywords1 = ['6side','4side']
Sentence = 'What is the probability of getting a 2 or a 5 when a die is rolled?'
list_of_keywords = klist
list_of_keywords1 = klist1
Sentence = sen
threshold = 0.90
for key in list_of_keywords:
for word in Sentence.split():
try:
# print(key)
# print(word)
res = cosdis(word2vec(word), word2vec(key))
#print(res)
if res > threshold:
print("Found a word with cosine distance > 100 : {} with original word: {}".format(word, key))
matchedkeywordlist.append(word)
except IndexError:
pass
for key1 in list_of_keywords1:
for word in Sentence.split():
try:
# print(key)
# print(word)
res = cosdis(word2vec(word), word2vec(key1))
# print(res)
if res > threshold:
print("Found a word with cosine distance > 80 : {} with original word: {}".format(word, key1))
matchedkeywordlist.append(word)
except IndexError:
pass
return matchedkeywordlist
def word2vec(word):
from collections import Counter
from math import sqrt
# count the characters in word
cw = Counter(word)
# precomputes a set of the different characters
sw = set(cw)
# precomputes the "length" of the word vector
lw = sqrt(sum(c*c for c in cw.values()))
# return a tuple
return cw, sw, lw
def cosdis(v1, v2):
# which characters are common to the two words?
common = v1[1].intersection(v2[1])
# by definition of cosine distance we have
return sum(v1[0][ch]*v2[0][ch] for ch in common)/v1[2]/v2[2]
def matchkeyword(klist,sen):
matchedkeywordlist=[]
#list_of_keywords = ["and","or","Probability","probability","Distribution","distribution","Random","random","Variable","variable","Normal","normal","Exponential","exponential","Element","element","Discrete","discrete","Function","function","P(X = x)̄","ΣP(x)=1","mass","Mass","cumulative","Cumulative","F(x)","P(X≤x)","mean","Mean","Expected","Value","expected","value","E(X)","Variance","variance","var(X)","V(X)","Standard","standard","Deviation","deviation","σ","μ"]
list_of_keywords = klist
Sentence = sen
threshold = 0.90
for key in list_of_keywords:
for word in Sentence.split():
try:
# print(key)
# print(word)
res = cosdis(word2vec(word), word2vec(key))
#print(res)
if res > threshold:
#print("Found a word with cosine distance > 100 : {} with original word: {}".format(word, key))
matchedkeywordlist.append(word)
except IndexError:
pass
return matchedkeywordlist
\ No newline at end of file
What is the probability that two cards drawn at random from a deck of playing cards will both be aces
"If you pull 2 cards out of a deck, what is the probability that both are spades"
"If you draw two cards from a deck, what is the probability that you will get the Ace of Diamonds and a black card"
What is the probability that when two cards are drawn from a deck of cards without replacement that both of them will be 8’s
What is the probability that both cards drawn (without replacement) will be spades
What is the probability of drawing a red king and then a black 7 without replacement
What is the probability of being dealt a flush (5 cards of all the same suit) from the first 5 cards in a deck
"What is the probability of drawing two face cards, and then 2 numbered cards, without replacement"
What is the probability of drawing an Ace 3 times in a row with replacement
"A card is drawn at random from a pack of 52 playing cards. Find the probability that the card drawn is
(i) a king
(ii) neither a queen nor a jack."
"A card is drawn from a well-shuffled pack of 52 cards. Find the probability of getting:
(i) ‘2’ of spades
(ii) a jack
(iii) a king of red colour
(iv) a card of diamond
(v) a king or a queen"
"A card is drawn from a well shuffled pack of 52 cards. Find the probability of:
(i) ‘2’ of spades
(ii) a jack
(iii) a king of red colour
(iv) a card of diamond
(v) a king or a queen
(vi) a non-face card
(vii) a black face card
(viii) a black card
(ix) a non-ace
(x) non-face card of black colour
(xi) neither a spade nor a jack
(xii) neither a heart nor a red king"
"A card is drawn at random from a well-shuffled pack of cards numbered 1 to 20. Find the probability of
(i) getting a number less than 7
(ii) getting a number divisible by 3."
Consider the example of finding the probability of selecting a black card or a 6 from a deck of 52 cards.
"What is the probability of drawing a king and a queen consecutively from a deck of 52 cards, without replacement."
Two cards are drawn at random from an ordinary deck of 52 card. Find the probability P that (a) Both are spade (b) One is a spade and one is heart
"Say, a coin is tossed twice. What is the probability of getting two consecutive tails ?"
A single coin is tossed 5 times. What is the probability of getting at least one head?
How many combinations can 2 dice make?
What are the odds of rolling a 12 with two dice?
What is the probability of rolling a 4 or 7 for two 6 sided dice?
Probability of rolling more than a 5
What is the probability of rolling a 5 when a die is rolled?
What is the probability of getting a 2 or a 5 when a die is rolled?
What is the probability of the occurrence of a number that is odd or less than 5 when a fair die is rolled.
"When two dice are rolled, find the probability of getting a greater number on the first die than the one on the second, given that the sum should equal 8."
A die is rolled thrice. What is the probability that the sum of the rolls is atleast 5.
"A dice is rolled repeatedly and X, the total score from all the rolls is kept. The game stops when the dice lands on either one or two, Find : 1. P(X =10) 2. The probability the first roll was 6 given X was 10"
package com.elearning.probabilityandstatisticsanalyserandeducator.dataExtraction;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
public class DescriptiveStatisticsQuestionComparationTest {
@Test
public void extractDatasetFromDescriptiveQuestion() throws Exception {
String input = "Find the mean value for 10, 20, 30, 40";
List<String> output;
List<String> expected = Arrays.asList("10","20","30","40");
double delta = .1;
DescriptiveStatisticsQuestionComparation descriptiveStatisticsQuestionComparation = new DescriptiveStatisticsQuestionComparation();
output = descriptiveStatisticsQuestionComparation.ExtractDatasetFromDescriptiveQuestion(input);
assertArrayEquals(expected, output, delta);
}
private void assertArrayEquals(List<String> expected, List<String> output, double delta) {
}
}
\ No newline at end of file
package com.elearning.probabilityandstatisticsanalyserandeducator.dataExtraction;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
public class ProbabilityValueTest {
@Test
public void extractionProbabilityValue() throws Exception {
String[] input = {"0.2","0.2","0.2","0.2","0.2"};
List<String> output;
List<String> expected = Arrays.asList("0.2","0.2","0.2","0.2","0.2");
double delta = .1;
ProbabilityValue probabilityValue = new ProbabilityValue();
output = probabilityValue.ExtractionProbabilityValue(input);
assertArrayEquals(expected, output, delta);
}
private void assertArrayEquals(List<String> expected, List<String> output, double delta) {
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment