ML_Ruby 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64fda0d7d407a58ec1df7fa697db1e5a08d9fe8b4ca4b28e5533c50d985fa1c6
4
- data.tar.gz: 26b26aac3b3260c77eda5aca6b4e04f3aa1320dd1c725a29d3d25b0e98a7c467
3
+ metadata.gz: 95869d52047e12b4e821f8955856b4dbe0d4dff4efa797a44300fed365103b7a
4
+ data.tar.gz: 6130da39a94674ee68cc5d7c2e834b104f664e82cc19d4d14f9b97b3af297f29
5
5
  SHA512:
6
- metadata.gz: 55dc5ada8223599c639067eaf2cc301491b3c5fccf4d9a6d37ffeab286bec206cdd1154d9bd858f17a2002c9a31255e672660bf4e53f16b7214652796b8e2ca2
7
- data.tar.gz: 1c9c36e421b9295ae086619c06fa333595d910128e0c0e4e4b4257360f2f9b84621742eb20bac257553fdbb31c1a273062e3a8ae955db92092778330b234160a
6
+ metadata.gz: 0bf46c64cd162b99fa821dd2e51c48ed4559b294d3a6d2ac33941f4fc7672b28eb20ac975085dbb7dfa2a278e633bca3760ce1f2d6d2811fdbb07b9372679a61
7
+ data.tar.gz: 63a6abaa30bc00fe04b2937188e7bf22ce67eff41878d77dcaef3e985c26bda05af1eaf4dd6a563a006f6f44d942c21a26354e90f6788b9a9287075cc7488851
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # MLRuby
2
2
 
3
- This Ruby gem leverages Machine Learning(ML) techniques to make predictions(forecasts) and classifications in various applications. It provides capabilities such as predicting next month's billing, forecasting upcoming sales orders, determining user approval status, classifying text, generating similarity scores, and making recommendations. It uses Python3 under the hood, powered by popular machine learning techniques including NLP(Natural Language Processing), Decision Tree, K-Nearest Neighbors and Linear Regression algorithms.
3
+ This Ruby gem leverages Machine Learning(ML) techniques to make predictions(forecasts) and classifications in various applications. It provides capabilities such as predicting next month's billing, generating new house/apartment prices, forecasting upcoming sales orders, determining user approval status, classifying text, generating similarity scores, and making recommendations. It uses Python3 under the hood, powered by popular machine learning techniques including NLP(Natural Language Processing), Decision Tree, K-Nearest Neighbors, Random Forest and Linear Regression algorithms.
4
4
 
5
5
 
6
6
  # Pre-requisite
7
- 1. Please make sure you have Python3 installed in your Machine. The gem will run `which python3` to locate your installed python3 in your Machine. Usually it is installed at `/usr/bin/python3`
7
+ 1. Please make sure you have Python3 installed in your Machine. The gem will run `which python3` command internally in order to locate your installed python3 in your Machine. Usually it is installed at `/usr/bin/python3`
8
8
 
9
- 2. Please make sure you have `scikit-learn` and `pandas` python libraries are installed in Machine.
9
+ 2. Please make sure you have `scikit-learn` and `pandas` python libraries are installed in your Machine.
10
10
 
11
- Here are examples of how to install these python libraries via the command line in MacOS. Install `nltk` if you really need to work with Natural Language Processing(NLP)
11
+ Here are examples of how to install these python libraries via the command line in MacOS and Linux. Install `nltk` if you really need to work with Natural Language Processing(NLP)
12
12
 
13
13
  `/usr/bin/python3 -m pip install scikit-learn`
14
14
 
@@ -163,6 +163,29 @@ training_messages = [
163
163
  ]
164
164
  predictions = ml.predict(new_messages)
165
165
  ```
166
+
167
+ - ### Natural Language Processing(NLP): Support Vector Machine Algorithm - Categorize/Classify Comments/Texts/Documents/Articles
168
+
169
+ Imagine you're managing a customer feedback system, and you want to categorize customer comments effectively. With the capabilities of this gem, you can seamlessly classify comments/texts/documents/articles into their appropriate categories.
170
+ ```
171
+ training_documents = [
172
+ "Machine learning techniques include neural networks and decision trees.",
173
+ "Web development skills are essential for building modern websites.",
174
+ "Natural language processing (NLP) is a subfield of artificial intelligence.",
175
+ "Data science involves data analysis and statistical modeling.",
176
+ "Computer vision is used in image and video processing applications."
177
+ ]
178
+ categories = ["Machine Learning", "Web Development", "Artificial Intelligence", "Data Science", "Computer Vision"]
179
+ ml = MLRuby::NaturalLanguageProcessing::SupportVectorMachine::Model.new(training_documents, categories)
180
+ new_documents = [
181
+ "I am Ruby On Rails Expert",
182
+ "I am interested in understanding natural language processing.",
183
+ "I want to pursue an academic degree on neural networks.",
184
+ "I have more than 12 years of professional working experience in JavaScript stack"
185
+ ]
186
+ predictions = ml.predict(new_documents)
187
+ ```
188
+
166
189
  It's important to note that the size of your training dataset plays a significant role in enhancing the accuracy of the model's predictions. By incorporating real-world, authentic data and expanding the amount of training data for the model, it gains a better understanding of patterns and trends within the data which leads to more precise and reliable predictions.
167
190
  ## Contributing
168
191
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MLRuby
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/ML_Ruby.rb CHANGED
@@ -74,6 +74,19 @@ module MLRuby
74
74
  end
75
75
  end
76
76
  end
77
+ module SupportVectorMachine
78
+ class Model
79
+ def initialize(training_data=[], categories=[])
80
+ @training_data = training_data
81
+ @categories = categories
82
+ end
83
+ def predict(new_data=[])
84
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/natural_language_processing/support_vector_machine.py"
85
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} '#{@training_data}' '#{@categories}' '#{new_data}'`
86
+ result.gsub("\n", "").gsub(/[\[\]]/, '').split("' '").map { |element| element.gsub(/'/, '') }
87
+ end
88
+ end
89
+ end
77
90
  end
78
91
 
79
92
  end
@@ -0,0 +1,29 @@
1
+ import numpy as np
2
+ from sklearn.feature_extraction.text import TfidfVectorizer
3
+ from sklearn.svm import SVC
4
+ from sklearn.metrics import classification_report
5
+ import sys
6
+ import ast
7
+
8
+ class SupportVectorMachine:
9
+ def __init__(self, documents, categories):
10
+ self.documents = documents
11
+ self.categories = categories
12
+
13
+ def fit_and_transform(self):
14
+ self.vectorizer = TfidfVectorizer(max_features=1000, stop_words='english')
15
+ X = self.vectorizer.fit_transform(self.documents)
16
+ self.classifier = SVC(kernel='linear')
17
+ self.classifier.fit(X, self.categories)
18
+
19
+
20
+ documents = ast.literal_eval(sys.argv[1])
21
+ categories = ast.literal_eval(sys.argv[2])
22
+ new_documents = ast.literal_eval(sys.argv[3])
23
+
24
+ support_vector_machine_model = SupportVectorMachine(documents, categories)
25
+ support_vector_machine_model.fit_and_transform()
26
+
27
+ new_text_vectorized = support_vector_machine_model.vectorizer.transform(new_documents)
28
+ predicted_topics = support_vector_machine_model.classifier.predict(new_text_vectorized)
29
+ print(predicted_topics)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ML_Ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdul Barek
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2023-09-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This Ruby gem leverages Machine Learning(ML) techniques to make predictions(forecasts)
14
14
  and classifications in various applications. It provides capabilities such as predicting
@@ -32,8 +32,8 @@ files:
32
32
  - lib/python/decision_tree_classifier.py
33
33
  - lib/python/k_nearest_neighbors.py
34
34
  - lib/python/linear_regression.py
35
+ - lib/python/natural_language_processing/support_vector_machine.py
35
36
  - lib/python/natural_language_processing/text_classifier.py
36
- - lib/python/natural_language_processing/text_summary.py
37
37
  - lib/python/random_forest.py
38
38
  - sig/ML_Ruby.rbs
39
39
  homepage: https://github.com/barek2k2/ML_Ruby
@@ -1,60 +0,0 @@
1
- import requests
2
-
3
- # Replace 'YOUR_API_KEY' with your actual Hugging Face API key
4
- api_key = 'YOUR_API_KEY'
5
-
6
- # Text to be summarized
7
- text = """
8
- Freedom of expression is a fundamental human right that holds immense importance in any democratic society. It serves as a platform for individuals to voice their opinions, share their thoughts and ideas, and engage in meaningful discussions and debates. However, the extent to which freedom of expression is upheld and practised varies from one country to another. In Bangladesh's case, although freedom of expression is enshrined in the constitution, its implementation faces significant challenges.
9
-
10
- At first glance, it may appear that people in Bangladesh enjoy the freedom to express themselves without fear of persecution. The media also seems relatively free to report on matters of public interest. However, a closer examination reveals the existence of substantial limitations to this freedom.
11
-
12
- One of the serious issues with freedom of expression in Bangladesh is its heavy bias towards certain individuals and groups. While some individuals, particularly those who are part of the ruling elite or possess political influence, can freely criticise the government and its policies, others do not have the same privilege. Those who lack political connections or are not part of the ruling elite face restrictions in expressing their opinions. They often become targets of persecution when they dare to speak out against the government or its policies.
13
-
14
- Furthermore, freedom of expression is confined to specific topics in the country. People are allowed to discuss issues that do not directly affect the larger population, but they are prohibited from being critical of the government's failures or the shortcomings of its institutions. To achieve this, the government employs various tactics such as intimidation, harassment, imprisonment, and even violence. The media is not immune to such suppression, as journalists are frequently targeted and attacked for reporting on sensitive issues. Such restrictions do not align with the principles of democracy.
15
-
16
- In Bangladesh, a unique style has emerged, favouring those who have political connections or are part of the ruling elite. This creates a significant power imbalance, silencing the voices of the majority while allowing the minority with access to power to dominate the conversation.
17
-
18
- How did the country arrive at this point? Is it solely due to the efforts of successive governments to silence dissidents, or have the institutions tasked with protecting the rights of the people also succumbed to vested interests?
19
-
20
- While it is valid to discuss the failure of institutions, a sizable portion of the blame falls on the media in Bangladesh as well. Impartial and unbiased journalism is increasingly limited in the country – not only due to informal embargoes and formal legal restrictions but also due to other factors. One crucial factor is the prevalence of media and journalists being aligned with partisan politics. While journalists having political alignments is not inherently wrong, it becomes problematic when they forget the fundamental principle of journalism: presenting the facts. Journalists with political biases or perceived biases often manipulate or distort information to support a specific political agenda or gain personal advantages. They tend to align themselves with the political party in power, compromising the freedom of expression that is crucial in a democratic society.
21
-
22
- Another concerning trend in Bangladesh is the influence of businesses or business conglomerates that own media outlets. These entities often leverage their media ownership to protect their business interests by eliminating competitors or evading accountability for crimes and wrongdoing. In such cases, the media outlets tend to align closely with the power structure, serving the interests of those in power, rather than speaking up for the people.
23
-
24
- Even in the presence of formal and informal restrictions imposed by the state, journalists should be able to report freely on matters of public interest. This freedom is essential for the press to remain relevant and create a platform for open dialogue among the people, their representatives, and public institutions. By doing so, the press can uphold the citizens' right to freedom of expression
25
-
26
- Journalists and media organisations in Bangladesh must reaffirm their commitment to impartiality, independence, and the presentation of facts. By adhering to these principles, they can counteract the influence of partisan politics and business interests on journalism and contribute to the preservation of press freedom in the country. Additionally, efforts should be made to foster a media environment that encourages diverse perspectives and safeguards the integrity of journalism.
27
-
28
- In a democracy, freedom of expression encompasses the right to criticise the government and its policies, and the press is expected to be free from bias. If Bangladesh aims to progress towards a more democratic society, it must ensure that freedom of expression is guaranteed to all its citizens, irrespective of their political affiliations or social status. But the question remains: how can the country do so?
29
-
30
- """
31
-
32
- # Define the endpoint URL
33
- endpoint = 'https://api-inference.huggingface.co/models/facebook/bart-large-cnn'
34
-
35
- # Define the headers with your API key
36
- api_key = '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'
37
- headers = {
38
- 'Authorization': f'Bearer {api_key}',
39
- }
40
-
41
- # Define the payload with the text to be summarized
42
- payload = {
43
- 'inputs': text,
44
- 'options': {
45
- 'task': 'summarization',
46
- },
47
- }
48
-
49
- # Make a POST request to the Hugging Face API
50
- response = requests.post(endpoint, json=payload, headers=headers)
51
-
52
- # Check if the request was successful
53
- if response.status_code == 200:
54
- result = response.json()
55
- print(result)
56
- #summary = result['summary']
57
- #print("Summary:")
58
- #print(summary)
59
- else:
60
- print("Error:", response.status_code, response.text)