Python Forum
how can rectify error from the below code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can rectify error from the below code
#1
Dear ALL,

import numpy

def gettingc1©:
    c1= []
    mystr = c.strip(' ').split()
    for i in mystr[:1]:
        c1.append(i)    
    return c1
    


def frequency1(c1, d):
    from collections import Counter

    termslist = Counter(i for i in d if i==c1).items()
    
    for k, v in termslist:
       
        return v
            
def maximum_frequence1(d):
    from collections import Counter 
    list0 = []
    terms= Counter(word for word in d).most_common()
    if terms[0][1] > terms[1][1]:
         list0.append([0][0])
    for k, v in terms:
        print(k)
        print(v)
        return v



def single_term_p1(c, d): 
    c1= gettingc1©
    return (frequency1 (c1, d)/ maximum_frequence1(d))

d=['biomedical informatics community', 'utility', 'domain ontologies', 'many barriers', 'effective use', 'important requirement', 'domain ontologies', 'high degree', 'coverage', 'domain concepts', 'concept relationships', 'development', u'ontology', 'prone process', 'limited resources result', u'concept', u'relationship', 'difficulty', 'ontology', 'knowledge changes', u'methodology', u'field', 'natural language processing', 'information extraction', 'information retrieval', 'machine', u'technique', 'enrichment', 'ontology', 'text documents', 'article', u'methodology', 'developed systems', 'existing methods', 'development', 'biomedical ontologies']

c= "domain ontologies"

print(single_term_p1(c, d))

OUTPUT

 File "C:/Users/user/example.py", line 49, in <module>
    print(single_term_p1(c, d))

  File "C:/Users/user/example.py", line 43, in single_term_p1
    return (frequency1 (c1, d)/ maximum_frequence1(d))

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
Reply
#2
This is your fifth thread. start using python code tags when posting code, error code tags when posting traceback, etc.. Not doing is breach of the forum rules.
Reply
#3
"NoteType" is the type of None, so having TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' means that you are trying to divide None by and int which likely because frequency1 (c1, d) returned None.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(Feb-28-2017, 08:45 AM)desul Wrote: Dear ALL,

import numpy

def gettingc1(c):
    c1= []
    mystr = c.strip(' ').split()
    for i in mystr[:1]:
        c1.append(i)    
    return c1
    


def frequency1(c1, d):
    from collections import Counter

    termslist = Counter(i for i in d if i==c1).items()
    
    for k, v in termslist:
       
        return v
            
def maximum_frequence1(d):
    from collections import Counter 
    list0 = []
    terms= Counter(word for word in d).most_common()
    if terms[0][1] > terms[1][1]:
         list0.append([0][0])
    for k, v in terms:
        print(k)
        print(v)
        return v



def single_term_p1(c, d): 
    c1= gettingc1(c)
    return (frequency1 (c1, d)/ maximum_frequence1(d))

d=['biomedical informatics community', 'utility', 'domain ontologies', 'many barriers', 'effective use', 'important requirement', 'domain ontologies', 'high degree', 'coverage', 'domain concepts', 'concept relationships', 'development', u'ontology', 'prone process', 'limited resources result', u'concept', u'relationship', 'difficulty', 'ontology', 'knowledge changes', u'methodology', u'field', 'natural language processing', 'information extraction', 'information retrieval', 'machine', u'technique', 'enrichment', 'ontology', 'text documents', 'article', u'methodology', 'developed systems', 'existing methods', 'development', 'biomedical ontologies']

c= "domain ontologies"

print(single_term_p1(c, d))
Output:
 File "C:/Users/user/example.py", line 49, in <module>
    print(single_term_p1(c, d))

  File "C:/Users/user/example.py", line 43, in single_term_p1
    return (frequency1 (c1, d)/ maximum_frequence1(d))

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
Reply
#5
Hello! You pass to getingc1() "domain ontologies" as an argument. Print c1  before the return statement to see what is going out. You get a single list with a single value ' domain'.


In [9]: mystr = ['domain', 'ontologies']

In [10]: c1 = []

In [11]: for i in mystr[:1]:
    ...:     c1.append(i)
    ...:     

In [12]: c1
Out[12]: ['domain']
So in frequency1() you count for a string which is not in 'd'.
In [20]: print(termslist)

dict_items([])
 
So you get an empty container from which you return 'v'. In the end the returned value from frequency1 function is None. You can't divide None with something else.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
in your list d you don't have 'domain' and that is why in your frequency1 function terms is empty list. At the same time your return v is whitin the for k, v in terms loop. So at the end frequency1 just returns None.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020