├── error_messages.py ├── requirements.txt ├── constants.py ├── CONTRIBUTING.md ├── firestore.py ├── sample_extract_input_data_from_GA4.sql ├── sample_extract_content_data_from_GA4.sql ├── README.md ├── firestore_test.py ├── sample_content_data.csv ├── main.py ├── main_test.py └── LICENSE /error_messages.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """All error messages used in the Content Recommendations using word2vec.""" 16 | 17 | from typing import Final 18 | 19 | NOT_EXISTS_INPUT_FILE: Final[str] = ( 20 | 'The input file dose not exist.' 21 | ) 22 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | absl-py==2.1.0 16 | certifi==2024.6.2 17 | charset-normalizer==3.1.0 18 | fst-pso==1.8.1 19 | FuzzyTM==2.0.5 20 | gensim==4.3.0 21 | idna==3.7 22 | miniful==0.0.6 23 | numpy==1.24.2 24 | pandas==1.5.3 25 | pyFUME==0.2.25 26 | python-dateutil==2.8.2 27 | pytz==2022.7.1 28 | requests==2.32.3 29 | scipy==1.10.1 30 | simpful==2.10.0 31 | six==1.16.0 32 | smart-open==6.3.0 33 | urllib3==2.2.2 -------------------------------------------------------------------------------- /constants.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """All constants used over Content Recommendation using word2vec.""" 16 | 17 | from typing import Final 18 | 19 | # Recommend outputs related constants. 20 | KEYWORD: Final[str] = 'keyword' 21 | RCM_RESULT: Final[str] = 'rcm_result' 22 | RANK: Final[str] = 'rank' 23 | SCORE: Final[str] = 'score' 24 | 25 | # Import output of content recommendations related constants. 26 | RANK: Final[str] = 'rank' 27 | SCORE: Final[str] = 'score' 28 | DELIMITER: Final[str] = ',' 29 | SKIPROWS: Final[str] = 1 30 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute 2 | 3 | We'd love to accept your patches and contributions to this project. 4 | 5 | ## Before you begin 6 | 7 | ### Sign our Contributor License Agreement 8 | 9 | Contributions to this project must be accompanied by a 10 | [Contributor License Agreement](https://cla.developers.google.com/about) (CLA). 11 | You (or your employer) retain the copyright to your contribution; this simply 12 | gives us permission to use and redistribute your contributions as part of the 13 | project. 14 | 15 | If you or your current employer have already signed the Google CLA (even if it 16 | was for a different project), you probably don't need to do it again. 17 | 18 | Visit to see your current agreements or to 19 | sign a new one. 20 | 21 | ### Review our community guidelines 22 | 23 | This project follows 24 | [Google's Open Source Community Guidelines](https://opensource.google/conduct/). 25 | 26 | ## Contribution process 27 | 28 | ### Code reviews 29 | 30 | All submissions, including submissions by project members, require review. We 31 | use GitHub pull requests for this purpose. Consult 32 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 33 | information on using pull requests. -------------------------------------------------------------------------------- /firestore.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Functions for loading & storing data into Firestore. 16 | 17 | Run Firestore functions from project's root directory. 18 | 19 | Example: 20 | `python firestore.py -i [Input data path]` 21 | """ 22 | 23 | import os 24 | 25 | import constants 26 | import error_messages 27 | import numpy as np 28 | 29 | 30 | _CSV_INPUT_DTYPES = np.dtype([ 31 | (constants.KEYWORD, 'O'), 32 | (constants.RCM_RESULT, 'O'), 33 | (constants.RANK, 'i2'), 34 | (constants.SCORE, 'f4'), 35 | ]) 36 | 37 | 38 | def read_csv_to_np_array(path: str) -> np.ndarray: 39 | """Reads csv data and returns numpy array. 40 | 41 | Args: 42 | path: A path to read csv data that you store data into Firestore. 43 | 44 | Returns: 45 | A numpy array loded from csv path. 46 | 47 | Raises: 48 | IOError: if the path is not found, or the resource cannot be opened. 49 | """ 50 | if os.path.exists(path): 51 | rcm_output_data = np.loadtxt( 52 | path, 53 | delimiter=constants.DELIMITER, 54 | skiprows=constants.SKIPROWS, 55 | dtype=_CSV_INPUT_DTYPES, 56 | ) 57 | else: 58 | raise IOError(error_messages.NOT_EXISTS_INPUT_FILE) 59 | 60 | return rcm_output_data 61 | -------------------------------------------------------------------------------- /sample_extract_input_data_from_GA4.sql: -------------------------------------------------------------------------------- 1 | /*Copyright 2023 Google LLC. 2 | This solution, including any related sample code or data, is made available on an “as is,” “as available,” and “with all faults” basis, solely for illustrative purposes, and without warranty or representation of any kind. This solution is experimental, unsupported and provided solely for your convenience. Your use of it is subject to your agreements with Google, as applicable, and may constitute a beta feature as defined under those agreements. To the extent that you make any data available to Google in connection with your use of the solution, you represent and warrant that you have all necessary and appropriate rights, consents and permissions to permit Google to use and process that data. By using any portion of this solution, you acknowledge, assume and accept all risks, known and unknown, associated with its usage, including with respect to your deployment of any portion of this solution in your systems, or usage in connection with your business, if at all. 3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 5 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ 6 | 7 | WITH USER_ID_AND_CONTENT_DATA AS ( 8 | SELECT DISTINCT 9 | user_pseudo_id AS user_id, 10 | event_timestamp as t, 11 | UPPER(REGEXP_EXTRACT(prm.value.string_value, r'/([^/]+)/?$')) AS item, 12 | FROM 13 | `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*` as T 14 | CROSS JOIN 15 | T.event_params AS prm 16 | WHERE 17 | event_name = 'add_to_cart' 18 | AND prm.key = 'page_location' 19 | AND _TABLE_SUFFIX BETWEEN '20201201' AND '20201231' 20 | AND geo.country = 'United States' 21 | ) 22 | SELECT 23 | user_id, 24 | STRING_AGG(item, ',' ORDER BY t asc) AS item_list, 25 | COUNT(item) AS cnt 26 | FROM 27 | USER_ID_AND_CONTENT_DATA 28 | WHERE 29 | item not like '%.HTML' 30 | GROUP BY 31 | user_id 32 | ; -------------------------------------------------------------------------------- /sample_extract_content_data_from_GA4.sql: -------------------------------------------------------------------------------- 1 | /*Copyright 2023 Google LLC. 2 | This solution, including any related sample code or data, is made available on an “as is,” “as available,” and “with all faults” basis, solely for illustrative purposes, and without warranty or representation of any kind. This solution is experimental, unsupported and provided solely for your convenience. Your use of it is subject to your agreements with Google, as applicable, and may constitute a beta feature as defined under those agreements. To the extent that you make any data available to Google in connection with your use of the solution, you represent and warrant that you have all necessary and appropriate rights, consents and permissions to permit Google to use and process that data. By using any portion of this solution, you acknowledge, assume and accept all risks, known and unknown, associated with its usage, including with respect to your deployment of any portion of this solution in your systems, or usage in connection with your business, if at all. 3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 5 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ 6 | 7 | WITH COTENT_URL_TITLE_ID AS ( 8 | SELECT 9 | (SELECT value.string_value FROM UNNEST(event_params) AS params WHERE params.key = 'page_location') AS url 10 | , (SELECT value.string_value FROM UNNEST(event_params) AS params WHERE params.key = 'page_title') AS title 11 | , (SELECT UPPER(REGEXP_EXTRACT(value.string_value, r'/([^/]+)/?$')) FROM UNNEST(event_params) AS params WHERE params.key = 'page_location') AS item 12 | FROM 13 | `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*` as T 14 | WHERE 15 | event_name ='add_to_cart' 16 | AND _TABLE_SUFFIX BETWEEN '20201201' AND '20201231' 17 | AND geo.country = 'United States' 18 | ) 19 | SELECT DISTINCT 20 | item 21 | , title 22 | , url 23 | FROM 24 | COTENT_URL_TITLE_ID 25 | WHERE 26 | item not like '%.HTML' 27 | ; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 16 | 17 | > [!CAUTION] 18 | > Note that this solution has been archived as of May 2025. 19 | > No further development or updates will be made to the solution on the Github. 20 | 21 | # Content Recommendation using word2vec 22 | 23 | **Disclaimer: This is not an official Google product.** 24 | 25 | ## Problem 26 | Content recommendation engines are one of key parts to increase page views in 27 | publisher's website or apps, but many publishers do not manage own content 28 | recommendation because creating a content recommendation engine yourself is 29 | hard work. 30 | 31 | ## Solution 32 | Content Recommendation using word2vec provides sample contents recommendation 33 | engine code using Google Analytics 4 data from BigQuery based on word2vec 34 | embedding. 35 | 36 | ## Deploy 37 | ### Requirements 38 | - Google Analytics 4 data in BigQuery 39 | - Python 3.11.4+ 40 | 41 | ### Initial Setup 42 | 1. Setup Python environment (e.g. pyenv etc.) with libraries based on requirements.txt. 43 | 44 | Example 45 | ``` 46 | pip install requirements.txt 47 | ``` 48 | 49 | 2. Prepare training data to use SQL for BigQuery based on sample sql 50 | (sample_extract_input_data_from_GA4.sql). In training data, each row has 51 | user_id, item_list ordered by time. 52 | 53 | 3. Prepare content data to use SQL for BigQuery based on sample sql 54 | (sample_extract_content_data_from_GA4.sql). Contents data need include 55 | contents id, contents title and contents url etc in each row. 56 | 57 | 4. Optional: adjust hyper parameter in word2vec or term of input data if you 58 | want. 59 | 60 | 5. Optional: Directory extract input and contents data from BigQuery. 61 | 62 | ### Scheduled execution 63 | 1. Run main.py in the root directory. 64 | ``` 65 | python main.py -i [Input data path] -c [Content data path] -o [Output path] 66 | 67 | Example with sample data: 68 | python main.py -i sample_input_data.csv -c sample_content_data.csv -o output.csv 69 | ``` 70 | -------------------------------------------------------------------------------- /firestore_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Tests for firestore.py.""" 16 | 17 | import os 18 | import unittest 19 | from unittest import mock 20 | 21 | from absl.testing import parameterized 22 | import constants 23 | import firestore 24 | import numpy as np 25 | 26 | 27 | _FAKE_NP_LOADTXT_INPUT_2_LINES = np.array( 28 | [('key_item_1', 'rcm_item_1', 1, 0.9), 29 | ('key_item_1', 'rcm_item_2', 2, 0.8)], 30 | dtype=firestore._CSV_INPUT_DTYPES, 31 | ) 32 | _FAKE_NP_LOADTXT_INPUT_1_LINE = np.array( 33 | [('key_item_1', 'rcm_item_1', 1, 0.9)], 34 | dtype=firestore._CSV_INPUT_DTYPES, 35 | ) 36 | _FAKE_COMMON_PATH = '/path/to' 37 | _FAKE_INPUT_FILEPATH = os.path.join(_FAKE_COMMON_PATH, 'input.csv') 38 | _FAKE_WRONG_FILEPATH = '/faile_file_path' 39 | 40 | 41 | class FirestoreTest(parameterized.TestCase): 42 | @parameterized.named_parameters([ 43 | { 44 | 'testcase_name': '2_rows_file_success', 45 | 'loadtxt_input_data': _FAKE_NP_LOADTXT_INPUT_2_LINES, 46 | }, 47 | { 48 | 'testcase_name': '1_row_file_success', 49 | 'loadtxt_input_data': _FAKE_NP_LOADTXT_INPUT_1_LINE, 50 | } 51 | ]) 52 | @mock.patch.object(firestore.os.path, 'exists', return_value=True) 53 | def test_read_csv_to_np_array_with_success( 54 | self, 55 | _, 56 | loadtxt_input_data, 57 | ): 58 | mock_np_loadtxt = mock.patch.object( 59 | firestore.np, 60 | 'loadtxt', 61 | return_value=loadtxt_input_data 62 | ).start() 63 | csv_path = _FAKE_INPUT_FILEPATH 64 | 65 | actual = firestore.read_csv_to_np_array(csv_path) 66 | 67 | mock_np_loadtxt.assert_called_with( 68 | _FAKE_INPUT_FILEPATH, 69 | delimiter=constants.DELIMITER, 70 | skiprows=constants.SKIPROWS, 71 | dtype=np.dtype( 72 | [(constants.KEYWORD, 'O'), 73 | (constants.RCM_RESULT, 'O'), 74 | (constants.RANK, 'i2'), 75 | (constants.SCORE, 'f4')] 76 | ) 77 | ) 78 | np.testing.assert_array_equal( 79 | loadtxt_input_data, 80 | actual, 81 | ) 82 | 83 | @mock.patch.object(firestore.os.path, 'exists', return_value=False) 84 | def test_read_csv_to_np_array_with_failure_wrong_file_path( 85 | self, 86 | _ 87 | ): 88 | with self.assertRaisesRegex( 89 | IOError, 90 | 'The input file dose not exist.' 91 | ): 92 | firestore.read_csv_to_np_array(_FAKE_WRONG_FILEPATH) 93 | 94 | 95 | if __name__ == '__main__': 96 | unittest.main() 97 | -------------------------------------------------------------------------------- /sample_content_data.csv: -------------------------------------------------------------------------------- 1 | item,title,url 2 | SOCKS,Socks | Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Socks 3 | BACKPACKS,Backpacks | Bags | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/bags/backpacks/ 4 | LIFESTYLE,Lifestyle,https://shop.googlemerchandisestore.com/Google+Redesign/Lifestyle 5 | ECO+FRIENDLY,Eco-Friendly | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/eco+friendly 6 | BAGS,Bags | Lifestyle | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Lifestyle/Bags 7 | KIDS,Kids | Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Kids 8 | MENS+T+SHIRTS,Men's T-Shirts | Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Mens/Mens+T+Shirts 9 | NOTEBOOKS,Notebooks | Stationery | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Stationery/Notebooks 10 | SHOP+BY+BRAND,Shop by Brand | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Shop+by+Brand 11 | GIFT+CARDS,Gift Cards | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Gift+Cards 12 | MUGS+TUMBLERS,Mugs & Tumblers | Drinkware | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Drinkware/Mugs+Tumblers 13 | KIDS+INFANT,Infant | Kids' Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Kids/Kids+Infant 14 | BLM,Black Lives Matter | Google Merchandise Store,https://shop.googlemerchandisestore.com/BLM 15 | CLEARANCE,Sale | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Clearance 16 | APPAREL,Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel 17 | STATIONERY,Stationery | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Stationery 18 | CAMPUS+COLLECTION,Campus Collection | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Campus+Collection 19 | WRITING,Writing | Stationery | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Stationery/Writing 20 | YOUTUBE,YouTube | Shop by Brand | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Shop+by+Brand/YouTube 21 | HATS,Hats | Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Hats 22 | ANDROID,Android | Shop by Brand | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Shop+by+Brand/Android 23 | WATER+BOTTLES,Water Bottles | Drinkware | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Drinkware/Water+Bottles 24 | SMALL+GOODS,Small Goods | Lifestyle | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Lifestyle/Small+Goods 25 | MENS,Men's / Unisex | Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Mens 26 | NEW,New | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/New 27 | DRINKWARE,Drinkware | Lifestyle | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Lifestyle/Drinkware 28 | WOMENS,Womens | Apparel | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Womens 29 | GOOGLE,Google | Shop by Brand | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Shop+by+Brand/Google 30 | STICKERS,Stickers | Stationery | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Stationery/Stickers 31 | I+AM+REMARKABLE,#IamRemarkable | Shop by Brand | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Shop+by+Brand/I+am+Remarkable 32 | OFFICE,Office | Google Merchandise Store,https://shop.googlemerchandisestore.com/Google+Redesign/Office 33 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Functions for training & predicting content by content recommendation using word2vec. 16 | 17 | Run from project's root directory. 18 | 19 | Example: 20 | `python -i [Input data path] -c [Content data path] -o [Output path]` 21 | `python -i [Input data path] -c [Content data path] -o [Output path] -r -ri 22 | [Item name to call ranking results]` 23 | 24 | Please check README.md and sample data in the root project for the format of 25 | input data and content data. 26 | """ 27 | 28 | import argparse 29 | import collections 30 | import itertools 31 | import logging 32 | from typing import Collection 33 | 34 | import constants 35 | import gensim 36 | import pandas as pd 37 | 38 | 39 | _SG = 1 40 | _WINDOWS = 5 41 | _MIN_COUNT = 5 42 | _VECTOR_SIZE = 100 43 | _HS = 0 44 | _NEGATIVE = 5 45 | _SEED = 1 46 | 47 | _TOP_N = 7 48 | 49 | logging.basicConfig( 50 | format='%(asctime)s %(message)s', 51 | datefmt='%m/%d/%Y %I:%M:%S %p', 52 | level=logging.INFO, 53 | ) 54 | 55 | 56 | def _read_csv(path: str) -> pd.DataFrame: 57 | """Read csv data and return dataframe. 58 | 59 | Args: 60 | path: path to read csv data 61 | 62 | Returns: 63 | A dataframe loded from path. 64 | 65 | Raises: 66 | IOError: if the path is not found, or the resource cannot be opened. 67 | """ 68 | try: 69 | df = pd.read_csv(path) 70 | except IOError as e: 71 | logging.exception('Can not load csv data with %s.', path) 72 | raise e 73 | 74 | return df 75 | 76 | 77 | def execute_embedding_w2v( 78 | training_data: pd.DataFrame, 79 | ) -> gensim.models.word2vec.Word2Vec: 80 | """Executes embedding content data by word2vec. 81 | 82 | Args: 83 | training_data: A DataFrame of training data that the content ID 84 | is for each line each user in the order that a certain user saw the 85 | content as the list type. Example is [['ITEM_A', 'ITEM_B', 'ITEM_C'], 86 | ['ITEM_B', 'ITEM_A', 'ITEM_B'], ['ITEM_C', 'ITEM_D', 'ITEM_E']]. 87 | Returns: 88 | A model of embedding resul by word2vec. 89 | """ 90 | model = gensim.models.word2vec.Word2Vec( 91 | sentences=training_data, 92 | sg=_SG, 93 | window=_WINDOWS, 94 | min_count=_MIN_COUNT, 95 | vector_size=_VECTOR_SIZE, 96 | hs=_HS, 97 | negative=_NEGATIVE, 98 | seed=_SEED, 99 | ) 100 | logging.info('Finished training of gensim word2vec.') 101 | 102 | return model 103 | 104 | 105 | def sort_recommendation_results( 106 | model: gensim.models.word2vec.Word2Vec, 107 | df_content: pd.DataFrame, 108 | ) -> pd.DataFrame: 109 | """Sorts recommendation results for easy use as output data. 110 | 111 | Args: 112 | model: A model that was trained by gensin word2vec. 113 | df_content: A DataFrame of content data with content id, 114 | content title and content URL. 115 | 116 | Returns: 117 | A dataframe sorted recommendation data with key content id, recommend 118 | content id, rank, score. 119 | """ 120 | df_result = pd.DataFrame({ 121 | constants.KEYWORD: pd.Series(dtype='object'), 122 | constants.RCM_RESULT: pd.Series(dtype='object'), 123 | constants.RANK: pd.Series(dtype='int64'), 124 | constants.SCORE: pd.Series(dtype='float64'), 125 | }) 126 | 127 | for _, content in df_content.iterrows(): 128 | try: 129 | ret = model.wv.most_similar(positive=content[0], topn=_TOP_N) 130 | except KeyError as e: 131 | logging.debug( 132 | 'Error happend during loading content item id: %s, %s', content[0], e 133 | ) 134 | continue 135 | for i, (rcm_result, score) in enumerate(ret): 136 | record = pd.DataFrame( 137 | [[content[0], 138 | rcm_result, 139 | int(i + 1), 140 | score]], 141 | columns=df_result.columns 142 | ) 143 | df_result = pd.concat([df_result, record]) 144 | 145 | logging.info('Completed process to sort embedding data.') 146 | return df_result 147 | 148 | 149 | def execute_ranking_process( 150 | training_data: Collection[Collection[str]], 151 | ranking_item_name: str 152 | ) -> pd.DataFrame: 153 | """Calculate rank of item ids. 154 | 155 | Args: 156 | training_data: An input data to calculate rank. 157 | ranking_item_name: A name of the item that calls the ranking data in the 158 | output. 159 | 160 | Returns: 161 | A dataframe of ranking result top_n. 162 | """ 163 | 164 | ranking_data = collections.Counter(list( 165 | itertools.chain.from_iterable(training_data) 166 | )) 167 | ranking_data = ranking_data.most_common(_TOP_N) 168 | 169 | df_ranking = pd.DataFrame({ 170 | constants.KEYWORD: pd.Series(dtype='object'), 171 | constants.RCM_RESULT: pd.Series(dtype='object'), 172 | constants.RANK: pd.Series(dtype='int64'), 173 | constants.SCORE: pd.Series(dtype='float64'), 174 | }) 175 | 176 | for i, tuple_items in enumerate(ranking_data): 177 | try: 178 | record = pd.DataFrame([[ranking_item_name, 179 | tuple_items[0], 180 | int(i + 1), 181 | 0] 182 | ], columns=df_ranking.columns 183 | ) 184 | df_ranking = pd.concat([df_ranking, record]) 185 | except KeyError as e: 186 | logging.debug( 187 | 'Error happend during loading content item id: %s, %s', 188 | tuple_items[0], 189 | e, 190 | ) 191 | continue 192 | logging.info('Completed process to execute calculation of ranking.') 193 | 194 | return df_ranking 195 | 196 | 197 | def execute_content_recommendation_w2v_from_csv( 198 | input_file_path: str, 199 | content_file_path: str, 200 | output_file_path: str, 201 | is_ranking_process: bool = False, 202 | ranking_item_name: str = 'undefined', 203 | ) -> None: 204 | """Trains and predicts contensts recommendation with word2vec. 205 | 206 | Args: 207 | input_file_path: A CSV format file path of training data that the content ID 208 | is for each line each user in the order that a certain user saw the 209 | content. 210 | content_file_path: A CSV format file path of content data with content id, 211 | content title and content URL. 212 | output_file_path: A CSV format file path of output. 213 | is_ranking_process: A flag whether to run the ranking process. 214 | ranking_item_name: A keyword to call the ranking result in outputs. 215 | """ 216 | df_training = _read_csv(input_file_path) 217 | logging.info('Loaded training data with %s.', input_file_path) 218 | training_data = df_training['item_list'].apply(lambda x: x.split(',') 219 | ).tolist() 220 | logging.info( 221 | 'Completed a process of loaded data into training data for' 222 | 'gensim word2vec.' 223 | ) 224 | 225 | df_content = _read_csv(content_file_path) 226 | logging.info('Loaded content data.') 227 | 228 | model = execute_embedding_w2v(training_data) 229 | 230 | df_result = sort_recommendation_results(model, df_content) 231 | 232 | if is_ranking_process: 233 | df_ranking = execute_ranking_process(training_data, ranking_item_name) 234 | df_result = pd.concat([df_result, df_ranking]) 235 | 236 | df_result.to_csv(output_file_path, index=False) 237 | logging.info('Completed exportion of predicted data.') 238 | 239 | logging.info('Completed process.') 240 | 241 | 242 | def parse_cli_args() -> argparse.Namespace: 243 | """Parses command line arguments. 244 | 245 | Returns: 246 | An instance of argparse.Namespace with arg values. 247 | """ 248 | parser = argparse.ArgumentParser() 249 | parser.add_argument( 250 | '--input', '-i', 251 | help='Input data file path to train models.', 252 | default=None, 253 | required=True, 254 | type=str, 255 | ) 256 | parser.add_argument( 257 | '--content', '-c', 258 | help='Content file path to macth content id with URL in the outputs.', 259 | default=None, 260 | required=True, 261 | type=str, 262 | ) 263 | parser.add_argument( 264 | '--output', '-o', 265 | help='Output file path for prediction results.', 266 | default=None, 267 | required=True, 268 | type=str, 269 | ) 270 | parser.add_argument( 271 | '--is_ranking', '-r', 272 | help='Whether to run the ranking process.', 273 | default=False, 274 | required=False, 275 | action=argparse.BooleanOptionalAction, 276 | ) 277 | parser.add_argument( 278 | '--ranking_item_name', '-ri', 279 | help='Set the keyword to call the ranking result.', 280 | default='undefined', 281 | required=False, 282 | type=str, 283 | ) 284 | 285 | return parser.parse_args() 286 | 287 | 288 | def main() -> None: 289 | """Executes contenst recommendation using word2vec for file type.""" 290 | args = parse_cli_args() 291 | execute_content_recommendation_w2v_from_csv(args.input, 292 | args.content, 293 | args.output, 294 | args.is_ranking, 295 | args.ranking_item_name, 296 | ) 297 | 298 | 299 | if __name__ == '__main__': 300 | main() 301 | -------------------------------------------------------------------------------- /main_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Tests for main.py.""" 16 | import argparse 17 | import os 18 | import sys 19 | import unittest 20 | from unittest import mock 21 | import main 22 | import pandas as pd 23 | 24 | _USER_ID = 'user_id' 25 | _ITEM_LIST = 'item_list' 26 | _CNT = 'cnt' 27 | _ITEM = 'item' 28 | _TITLE = 'title' 29 | _URL = 'url' 30 | _KEYWORD = 'keyword' 31 | _RCM_RESULTS = 'rcm_result' 32 | _RANK = 'rank' 33 | _SCORE = 'score' 34 | _DUMMY_RANKING_PROCESS_TRUE = True 35 | _DUMMY_RANKING_PROCESS_FALSE = False 36 | _DUMMY_RANKING_ITEN_NAME = 'Dummy_Ranking' 37 | _DEFAULT_RANKING_ITEM_NAME = 'undefined' 38 | 39 | _DUMMY_COMMON_PATH = '/path/to' 40 | _DUMMY_INPUT_FILEPATH = os.path.join(_DUMMY_COMMON_PATH, 'input.csv') 41 | _DUMMY_CONTENT_FILEPATH = os.path.join(_DUMMY_COMMON_PATH, 'content.csv') 42 | _DUMMY_OUTPUT_FILEPATH = os.path.join(_DUMMY_COMMON_PATH, 'ouput.csv') 43 | _DUMMY_DF_TRAINING_FILEPATH = os.path.join('./', 'input.csv') 44 | 45 | COL_NAMES_INPUT = ( 46 | _USER_ID, 47 | _ITEM_LIST, 48 | _CNT 49 | ) 50 | 51 | _DUMMY_DF_TRAINNG = pd.DataFrame({ 52 | _USER_ID: ['user_a', 'user_b', 'user_c', 'user_d'], 53 | _ITEM_LIST: ['ITEM_A,ITEM_B,ITEM_C,ITEM_B,ITEM_A,ITEM_B', 54 | 'ITEM_B,ITEM_A,ITEM_B,ITEM_A,ITEM_B,ITEM_C', 55 | 'ITEM_B,ITEM_A,ITEM_B,ITEM_A,ITEM_B,ITEM_C', 56 | 'ITEM_C,ITEM_D,ITEM_E,ITEM_D,ITEM_C,ITEM_A'], 57 | _CNT: [6, 6, 6, 6] 58 | }) 59 | 60 | _DUMMY_DF_CONTENTS = pd.DataFrame({ 61 | _ITEM: ['ITEM_A', 'ITEM_B', 'ITEM_C', 'ITEM_D', 'ITEM_E'], 62 | _TITLE: ['ITEM A', 'ITEM B', 'ITEM C', 'ITEM D', 'ITEM E'], 63 | _URL: ['https://example.com/item_a', 64 | 'https://example.com/item_b', 65 | 'https://example.com/item_c', 66 | 'https://example.com/item_d', 67 | 'https://example.com/item_e'] 68 | }) 69 | _DUMMY_TRAINING_DATA = [['ITEM_A', 'ITEM_B', 'ITEM_C', 'ITEM_B', 'ITEM_A', 70 | 'ITEM_B'], 71 | ['ITEM_B', 'ITEM_A', 'ITEM_B', 'ITEM_A', 'ITEM_B', 72 | 'ITEM_C'], 73 | ['ITEM_B', 'ITEM_A', 'ITEM_B', 'ITEM_A', 'ITEM_B', 74 | 'ITEM_C'], 75 | ['ITEM_C', 'ITEM_D', 'ITEM_E', 'ITEM_D', 'ITEM_C', 76 | 'ITEM_A']] 77 | 78 | _DUMMY_DF_CONTENTS = pd.DataFrame({ 79 | _ITEM: ['ITEM_A', 'ITEM_B', 'ITEM_C', 'ITEM_D', 'ITEM_E'], 80 | _TITLE: ['ITEM A', 'ITEM B', 'ITEM C', 'ITEM D', 'ITEM E'], 81 | _URL: ['https://example.com/item_a', 82 | 'https://example.com/item_b', 83 | 'https://example.com/item_c', 84 | 'https://example.com/item_d', 85 | 'https://example.com/item_e'] 86 | }) 87 | _DUMMY_DF_ERROR_CONTENTS = pd.DataFrame({ 88 | _ITEM: ['ITEM_F'], 89 | _TITLE: ['ITEM F'], 90 | _URL: ['https://example.com/item_f'], 91 | }) 92 | _DUMMY_DF_RESULTS = pd.DataFrame({ 93 | _KEYWORD: ['ITEM_A', 'ITEM_A', 'ITEM_B', 'ITEM_B', 'ITEM_C', 'ITEM_C'], 94 | _RCM_RESULTS: ['ITEM_B', 'ITEM_C', 'ITEM_A', 'ITEM_C', 'ITEM_A', 'ITEM_B'], 95 | _RANK: [1, 2, 1, 2, 1, 2], 96 | _SCORE: [mock.ANY, mock.ANY, mock.ANY, mock.ANY, mock.ANY, mock.ANY] 97 | }) 98 | _DUMMY_DF_RANKING = pd.DataFrame({ 99 | _KEYWORD: [_DUMMY_RANKING_ITEN_NAME, 100 | _DUMMY_RANKING_ITEN_NAME, 101 | _DUMMY_RANKING_ITEN_NAME, 102 | _DUMMY_RANKING_ITEN_NAME, 103 | _DUMMY_RANKING_ITEN_NAME 104 | ], 105 | _RCM_RESULTS: ['ITEM_B', 'ITEM_A', 'ITEM_C', 'ITEM_D', 'ITEM_E'], 106 | _RANK: [1, 2, 3, 4, 5], 107 | _SCORE: [0.0, 0.0, 0.0, 0.0, 0.0] 108 | }) 109 | 110 | _DUMMY_MODEL = main.gensim.models.word2vec.Word2Vec( 111 | sentences=_DUMMY_TRAINING_DATA, 112 | sg=main._SG, 113 | window=main._WINDOWS, 114 | min_count=main._MIN_COUNT, 115 | vector_size=main._VECTOR_SIZE, 116 | hs=main._HS, 117 | negative=main._NEGATIVE, 118 | seed=main._SEED, 119 | ) 120 | 121 | 122 | class MainTest(unittest.TestCase): 123 | def test_success_read_csv(self): 124 | """Ensures success with correct csv.""" 125 | expected_df = _DUMMY_DF_TRAINNG 126 | 127 | _DUMMY_DF_TRAINNG.to_csv(_DUMMY_DF_TRAINING_FILEPATH, index=False) 128 | actual_df = main._read_csv(_DUMMY_DF_TRAINING_FILEPATH) 129 | 130 | pd.testing.assert_frame_equal(actual_df, expected_df) 131 | 132 | def test_raise_error_read_csv_with_empty_path(self): 133 | """Ensures failed with empty path.""" 134 | with self.assertRaisesRegex(FileNotFoundError, 'No such file or directory'): 135 | _ = main._read_csv('') 136 | 137 | @mock.patch('main.pd.read_csv', side_effect=[_DUMMY_DF_TRAINNG, 138 | _DUMMY_DF_CONTENTS, 139 | ]) 140 | def test_execute_content_recommendation_w2v_from_csv_with_required_params( 141 | self, 142 | _, 143 | ): 144 | """Ensures success with correct csv.""" 145 | with mock.patch('main.pd.DataFrame.to_csv') as mock_to_csv: 146 | main.execute_content_recommendation_w2v_from_csv(_DUMMY_INPUT_FILEPATH, 147 | _DUMMY_CONTENT_FILEPATH, 148 | _DUMMY_OUTPUT_FILEPATH, 149 | ) 150 | 151 | mock_to_csv.assert_called_once_with(_DUMMY_OUTPUT_FILEPATH, index=False) 152 | 153 | 154 | @mock.patch('main.pd.read_csv', side_effect=[_DUMMY_DF_TRAINNG, 155 | _DUMMY_DF_CONTENTS, 156 | ]) 157 | def test_execute_content_recommendation_w2v_from_csv_with_all_params(self, _): 158 | """Ensures success with correct csv.""" 159 | with mock.patch('main.pd.DataFrame.to_csv') as mock_to_csv: 160 | main.execute_content_recommendation_w2v_from_csv( 161 | _DUMMY_INPUT_FILEPATH, 162 | _DUMMY_CONTENT_FILEPATH, 163 | _DUMMY_OUTPUT_FILEPATH, 164 | _DUMMY_RANKING_PROCESS_TRUE, 165 | _DUMMY_RANKING_ITEN_NAME, 166 | ) 167 | 168 | mock_to_csv.assert_called_once_with(_DUMMY_OUTPUT_FILEPATH, index=False) 169 | 170 | def test_execute_embedding_w2v(self): 171 | """Ensures success with correct trainin_data.""" 172 | with mock.patch('main.gensim.models.word2vec.Word2Vec') as mock_gensim: 173 | _ = main.execute_embedding_w2v(_DUMMY_TRAINING_DATA) 174 | 175 | mock_gensim.assert_called_once_with(sentences=mock.ANY, 176 | sg=main._SG, 177 | window=main._WINDOWS, 178 | min_count=main._MIN_COUNT, 179 | vector_size=main._VECTOR_SIZE, 180 | hs=main._HS, 181 | negative=main._NEGATIVE, 182 | seed=main._SEED, 183 | ) 184 | 185 | def test_sort_recommendation_result(self): 186 | """Ensures success with sort_recommendation_result function.""" 187 | actual_df_result = main.sort_recommendation_results(_DUMMY_MODEL, 188 | _DUMMY_DF_CONTENTS, 189 | ) 190 | pd.testing.assert_frame_equal( 191 | actual_df_result.reset_index(drop=True).drop(columns=[_SCORE]), 192 | _DUMMY_DF_RESULTS.reset_index(drop=True).drop(columns=[_SCORE]), 193 | ) 194 | 195 | def test_sort_recommendation_result_with_keyerror(self): 196 | """Ensures keyerror with sort_recommendation_result function.""" 197 | with self.assertLogs(level='DEBUG') as log_output: 198 | _ = main.sort_recommendation_results(_DUMMY_MODEL, 199 | _DUMMY_DF_ERROR_CONTENTS, 200 | ) 201 | self.assertIn('Error happend during loading content item id', 202 | log_output.output[0]) 203 | 204 | def test_parse_cli_args_returns_namespace_with_required_args(self): 205 | """Ensures parse_cli_args with success case.""" 206 | test_args = [ 207 | 'main.py', 208 | '-i', 209 | _DUMMY_INPUT_FILEPATH, 210 | '-c', 211 | _DUMMY_CONTENT_FILEPATH, 212 | '-o', 213 | _DUMMY_OUTPUT_FILEPATH, 214 | ] 215 | expected = argparse.Namespace( 216 | input=_DUMMY_INPUT_FILEPATH, 217 | content=_DUMMY_CONTENT_FILEPATH, 218 | output=_DUMMY_OUTPUT_FILEPATH, 219 | is_ranking=_DUMMY_RANKING_PROCESS_FALSE, 220 | ranking_item_name=_DEFAULT_RANKING_ITEM_NAME, 221 | ) 222 | 223 | with mock.patch.object(sys, 'argv', test_args): 224 | actual = main.parse_cli_args() 225 | 226 | self.assertIsInstance(actual, argparse.Namespace) 227 | self.assertEqual(actual, expected) 228 | 229 | def test_parse_cli_args_returns_namespace_with_args(self): 230 | """Ensures parse_cli_args with success case.""" 231 | test_args = [ 232 | 'main.py', 233 | '-i', 234 | _DUMMY_INPUT_FILEPATH, 235 | '-c', 236 | _DUMMY_CONTENT_FILEPATH, 237 | '-o', 238 | _DUMMY_OUTPUT_FILEPATH, 239 | '-r', 240 | '-ri', 241 | _DUMMY_RANKING_ITEN_NAME, 242 | ] 243 | expected = argparse.Namespace( 244 | input=_DUMMY_INPUT_FILEPATH, 245 | content=_DUMMY_CONTENT_FILEPATH, 246 | output=_DUMMY_OUTPUT_FILEPATH, 247 | is_ranking=_DUMMY_RANKING_PROCESS_TRUE, 248 | ranking_item_name=_DUMMY_RANKING_ITEN_NAME 249 | ) 250 | 251 | with mock.patch.object(sys, 'argv', test_args): 252 | actual = main.parse_cli_args() 253 | 254 | self.assertIsInstance(actual, argparse.Namespace) 255 | self.assertEqual(actual, expected) 256 | 257 | def test_execute_ranking_process(self): 258 | actual_df = main.execute_ranking_process( 259 | _DUMMY_TRAINING_DATA, 260 | _DUMMY_RANKING_ITEN_NAME, 261 | ) 262 | 263 | pd.testing.assert_frame_equal(actual_df.reset_index(drop=True), 264 | _DUMMY_DF_RANKING.reset_index(drop=True), 265 | ) 266 | 267 | 268 | if __name__ == '__main__': 269 | unittest.main() 270 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------