I import my function using myfuncs but it fails I did test the same file paths only slightly different file name but it fails. The python code was 5 years old not sure if something changed or not.
I did go to this thread and I did the test and it did works successfully for the test and my actual looks to be the same.
function is not defined error in Python
#!/usr/bin/python
# Our function is pulled in here
from myfunction import pyth_test
pyth_test(1,2)
This test works however my actual function file does not both files are in the same directory both imports are from import * same format just slightly different file names which should not matter.
the test to see if import works
from myfunction import *
pyth_test(1,2)
This works successfully
3
however when I try the actual impor of the function I need to use I get this error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-1a2412372452> in <module>
6 f.close()
7
----> 8 bag_of_words = myfuncs.get_bag_of_words(titles_lines)
9 keywords = myfuncs.get_keywords(titles_lines, bag_of_words)
NameError: name 'myfuncs' is not defined
Code Below is what I run to call the function from the file called myfuncs.py
run.py
from myfuncs import *
f = open('s2-titles.txt', encoding="utf8")
titles_lines = f.readlines()
f.close()
bag_of_words = myfuncs.get_bag_of_words(titles_lines)
keywords = myfuncs.get_keywords(titles_lines, bag_of_words)
myfuncs.py
#!/usr/bin/env python
# coding: utf-8
def get_bag_of_words(titles_lines):
# bag of words
bag_of_words = {}
# [1: ]skips the first line which is the header
for line in titles_lines[1:]:
courseid, course_bag_of_words = get_course_bag_of_words(line)
for word in course_bag_of_words:
if word not in course_bag_of_words:
bag_of_words[word] = course_bag_of_words[word]
else:
bag_of_words[word] += course_bag_of_words[word]
return bag_of_words
def get_course_bag_of_words(line):
course_bag_of_words = {}
#split by weirdcombo to prevent weird splits
courseid, title, description = line.split('XXXYYYZZZ')
title = title.lower()
description = description.lower()
wordlist = title.split() + description.split()
if len(wordlist) >=10:
for word in wordlist:
if word not in course_bag_of_words:
course_bag_of_words[word] = 1
else:
course_bag_of_words[word] += 1
return courseid, course_bag_of_words
def get_sorted_results(d):
kv_list = d.items()
vk_list = []
for kv in kv_list:
k,v = kv
vk = v,k
vk_list.append(vk)
vk_list.sort()
vk_list.reverse()
k_list = []
for vk in vk_list[:10]:
v,k = vk
k_list.append(k)
return k_list
def get_keywords(titles_lines, bag_of_words):
n = sum(bag_of_words.values())
keywords = {}
for line in titles_lines[1:]:
courseid, course_bag_of_words = get_course_bag_of_words(line)
term_importance = {}
for word in course_bag_of_words:
tf_course =(float(course_bag_of_words[word])/
sum(course_bag_of_words.values())
)
tf_overall = float(bag_of_words[word]) /n
term_importance[word] = tf_course/tf_overall
keywords[courseid] = get_sorted_results(term_importance)
if courseid == '74953':
for word in keywords[courseid]:
print('has importance', term_importance['word'])
return keywords