13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
16
- # [START language_classify_text_tutorial]
17
16
"""Using the classify_text method to find content categories of text files,
18
17
Then use the content category labels to compare text similarity.
19
18
@@ -94,7 +93,6 @@ def index(path, index_file):
94
93
# [END language_classify_text_tutorial_index]
95
94
96
95
97
- # [START language_classify_text_tutorial_split_labels]
98
96
def split_labels (categories ):
99
97
"""The category labels are of the form "/a/b/c" up to three levels,
100
98
for example "/Computers & Electronics/Software", and these labels
@@ -121,10 +119,8 @@ def split_labels(categories):
121
119
_categories [label ] = confidence
122
120
123
121
return _categories
124
- # [END language_classify_text_tutorial_split_labels]
125
122
126
123
127
- # [START language_classify_text_tutorial_similarity]
128
124
def similarity (categories1 , categories2 ):
129
125
"""Cosine similarity of the categories treated as sparse vectors."""
130
126
categories1 = split_labels (categories1 )
@@ -143,7 +139,6 @@ def similarity(categories1, categories2):
143
139
dot += confidence * categories2 .get (label , 0.0 )
144
140
145
141
return dot / (norm1 * norm2 )
146
- # [END language_classify_text_tutorial_similarity]
147
142
148
143
149
144
# [START language_classify_text_tutorial_query]
@@ -255,4 +250,3 @@ def query_category(index_file, category_string, n_top=3):
255
250
query (args .index_file , args .text )
256
251
if args .command == 'query-category' :
257
252
query_category (args .index_file , args .category )
258
- # [END language_classify_text_tutorial]
0 commit comments