def test_query(self):
        checker = SpellChecker()
        validTestData = [('hello', 'Rare'), ('fantastic', 'Rare'), ('order', 'Common'),
                         ('absolute', 'Uncommon'), ('smartphone', 'Unknown'), ('aksfjalsf', 'Unknown')]

        invalidTestData = [315, 0, -4]

        for query, response in validTestData:
            self.assertTrue(checker.query(query) == response)

        for invalidQuery in invalidTestData:
            with self.assertRaises(TypeError):
                checker.query(invalidQuery)
Example #2
0
        def spellCheckedResultForPlayerName(self, playerName):
            league = NBA_LEAGUE
            firstnames, lastnames, fullnames, playerURLNames = self.getPlayerListings(league)
            checker = SpellChecker(playerName, firstnames, lastnames)
            correctName = checker.correctFullName()
            if correctName == '':
                league = WNBA_LEAGUE
                firstnames, lastnames, fullnames, playerURLNames = self.getPlayerListings(league)
	        checker = SpellChecker(playerName, firstnames, lastnames)
	        correctName = checker.correctFullName()
		
	    if correctName == '':
	    	return (correctName, league, '')
	    else:
	    	return (correctName, league, playerURLNames[correctName.lower()])
Example #3
0
def hello2():
    user_input = request.form["user-input"]
    print("\"{}\"".format(user_input))
    context_identifier = ContextIdentifier()
    context = context_identifier.getContext(user_input)
    bot_response = ""
    suggested_word = []
    
    if context == Context.unknown:
        print("Unknown?")
        suggested_word = SpellChecker().getWordSuggestion(user_input)

    elif context == Context.help:
        bot_response = "Terdapat beberapa hal yang dapat dilakukan:\n"
        bot_response += "- Menambah tugas (coba \"Tolong ingatkan kalau ada kuis IF3110 Bab 2 pada 22/04/21\")\n"
        bot_response += "- Melihat semua tugas (coba \"bot tugas apa saja sejauh ini ya?\")\n"
        bot_response += "-. Melihat tugas pada periode tertentu (coba \"Apa saja deadline antara 03/04/2021 sampai 15/04/2021\")\n"
        bot_response += "- Melihat tugas beberapa hari/minggu ke depan (coba \"Ada tugas apa saja 2 hari ke depan\")\n"
        bot_response += "- Melihat tugas yang deadline-nya hari ini (coba \"Deadline tucil hari ini apa saja, ya?\")\n"
        bot_response += "- Menampilkan deadline dari suatu tugas tertentu (coba \"Deadline tucil IF2230 itu kapan?\")\n"
        bot_response += "- Memperbarui tugas (coba \"Deadline tucil IF2230 diundur menjadi 02/02/2021\")\n"
        bot_response += "- Menghapus/menyelesaikan tugas (coba \"bot ujian IF2230 sudah selesai ya jadi gausah disimpan lagi\")\n"
        bot_response += "Kata kunci:\n" + "\n".join(list(map(lambda x: "- " + x, ["kuis", "tubes", "tucil", "ujian"])))
    
    else:
        extractor = Extractor()
        print("\"{}\"".format(user_input))
        command = extractor.extract(user_input, context)
        
        if command == None:
            suggested_word = SpellChecker().getWordSuggestion(user_input)
        else:
            command.execute()
            bot_response = command.getResult()
            
    if bot_response == "":
        if len(suggested_word) > 0:
            bot_response = "Mungkin maksud kata kunci Anda: " + ", ".join(suggested_word)
        else:
            bot_response = "Saya tidak paham .-."
            
    chat_data.append((user_input, bot_response.split("\n")))
    return render_template("index.html", message_data = chat_data[(-5 if len(chat_data) >= 5 else 0):])
##
 # Harvey Mudd College, CS159
 # Swarthmore College, CS65
 # Copyright (c) 2018 Harvey Mudd College Computer Science Department, Claremont, CA
 # Copyright (c) 2018 Swarthmore College Computer Science Department, Swarthmore, PA
##


import argparse
import sys
from SpellChecker import SpellChecker
from LanguageModel import LanguageModel
from EditDistance import EditDistanceFinder

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--languagemodel", "-l", type=argparse.FileType('rb'), required=True)
    parser.add_argument("--editmodel", "-e", type=argparse.FileType('rb'), required=True)
    parser.add_argument("--corpus", "-c", type=argparse.FileType('r'), default=sys.stdin)
    args = parser.parse_args()

    s=SpellChecker(max_distance=2)
    s.load_language_model(args.languagemodel)
    s.load_channel_model(args.editmodel)

    for line in args.corpus:
        print("LINE: ", line)
        corrected = s.autocorrect_line(line)
        print("CORRECTED: ", corrected)

import pickle
import sys

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--languagemodel",
                        "-l",
                        type=argparse.FileType('rb'),
                        required=True)
    parser.add_argument("--editmodel",
                        "-e",
                        type=argparse.FileType('rb'),
                        required=True)
    args = parser.parse_args()

    s = SpellChecker(max_distance=2)
    s.load_language_model(args.languagemodel)
    s.load_channel_model(args.editmodel)

    print(s.channel_model.prob("hello", "hello"))
    print(s.channel_model.prob("hellp", "hello"))
    print(s.channel_model.prob("hllp", "hello"))

    print(s.check_text("they did not yb any menas"))
    """
    >>> [['they'], ['did'], ['not'], ['by', 'b', 'ye', 'y', 'yo', 'ob', 'ya', 'ab'], ['any'],
    >>>  ['means', 'mens', 'mena', 'zenas', 'menan', 'mends']]
    """

    print(s.autocorrect_line("they did not yb any menas"))
    """
 def test_class_count(self):
     checker = SpellChecker()
     self.assertTrue(checker.class_count('class_countTESTFILE.txt') == (5, 2, 3, 3, 13))
 def setUp(self):
     self.mySpellChecker = SpellChecker()
class Test(unittest.TestCase):

    def setUp(self):
        self.mySpellChecker = SpellChecker()

    def tearDown(self):
        pass

    def testCheckWordReturnsSameIfCorrect(self):
        self.mySpellChecker.addWord( "foo" )
        self.assertEqual( "foo", self.mySpellChecker.checkWord( "foo" ), "checkWord() did not return the original when it was correct" )

    def testCheckWordReturnsMessageIfNoCorrectionFound(self):
        self.assertEqual( "no correction found", self.mySpellChecker.checkWord( "foo" ), "checkWord() did not return the correct message when no correction found" )

    def testCheckWordCorrectsMixedAndTitleCaseToLowerCase(self):
        self.mySpellChecker.addWord( "carrot" )
        self.assertEqual( "carrot", self.mySpellChecker.checkWord( "CaRrOT" ), "checkWord() did not correct mixed case to lower case" )
        self.assertEqual( "carrot", self.mySpellChecker.checkWord( "Carrot" ), "checkWord() did not correct title case to lower case" )

    def testCheckWordCorrectsLowerAndMixedCaseToTitleCase(self):
        self.mySpellChecker.addWord( "Moscow" )
        self.assertEqual( "Moscow", self.mySpellChecker.checkWord( "mosCow" ), "checkWord() did not correct mixed case to title case" )
        self.assertEqual( "Moscow", self.mySpellChecker.checkWord( "moscow" ), "checkWord() did not correct lower case to title case" )

    def testCheckWordCorrectsLowerAndTitleCaseToMixedCase(self):
        self.mySpellChecker.addWord( "Jean-Pierre" )
        self.assertEqual( "Jean-Pierre", self.mySpellChecker.checkWord( "Jean-pierre" ), "checkWord() did not correct to mixed case" )
        self.assertEqual( "Jean-Pierre", self.mySpellChecker.checkWord( "jean-pierre" ), "checkWord() did not correct to mixed case" )

    def testCheckWordCorrectsRepeatingCharactersAtBeginning(self):
        self.mySpellChecker.addWord( "phone" )

        self.assertEqual( "phone", self.mySpellChecker.checkWord( "pphone" ), "checkWord() did not correct repeating first character" )
        self.assertEqual( "phone", self.mySpellChecker.checkWord( "ppphone" ), "checkWord() did not correct repeating first character" )

    def testCheckWordCorrectsRepeatingCharactersAtBeginningAndNotLater(self):
        self.mySpellChecker.addWord( "pope" )

        self.assertEqual( "pope", self.mySpellChecker.checkWord( "ppope" ), "checkWord() did not correct repeating first character" )
        self.assertEqual( "pope", self.mySpellChecker.checkWord( "pppope" ), "checkWord() did not correct repeating first character" )

    def testCheckWordCorrectsRepeatingCharactersInMiddle(self):
        self.mySpellChecker.addWord( "phone" )

        self.assertEqual( "phone", self.mySpellChecker.checkWord( "phoone" ), "checkWord() did not correct repeating middle character" )
        self.assertEqual( "phone", self.mySpellChecker.checkWord( "phoooone" ), "checkWord() did not correct repeating middle character" )

    def testCheckWordCorrectsRepeatingCharactersInMultiplePlaces(self):
        self.mySpellChecker.addWord( "phone" )

        self.assertEqual( "phone", self.mySpellChecker.checkWord( "pphhhone" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "phone", self.mySpellChecker.checkWord( "pphooone" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "phone", self.mySpellChecker.checkWord( "pphonnne" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "phone", self.mySpellChecker.checkWord( "pphoneee" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "phone", self.mySpellChecker.checkWord( "pphhoonnee" ), "checkWord() did not correct repeating character all over the place" )

    def testCheckWordCorrectsRepeatingCharactersAtBeginningWithNaturalRepeats(self):
        self.mySpellChecker.addWord( "jazzer" )

        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jjazzer" ), "checkWord() did not correct repeating first character" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jjjazzer" ), "checkWord() did not correct repeating first character" )

    def testCheckWordCorrectsRepeatingCharactersAtBeginningAndNotLaterWithNaturalRepeats(self):
        self.mySpellChecker.addWord( "sassy" )

        self.assertEqual( "sassy", self.mySpellChecker.checkWord( "ssassy" ), "checkWord() did not correct repeating first character" )
        self.assertEqual( "sassy", self.mySpellChecker.checkWord( "sssassy" ), "checkWord() did not correct repeating first character" )

    def testCheckWordCorrectsRepeatingCharactersInMiddleWithNaturalRepeats(self):
        self.mySpellChecker.addWord( "jazzer" )

        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jaazzer" ), "checkWord() did not correct repeating middle character" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jaaazzer" ), "checkWord() did not correct repeating middle character" )

        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jaazzzer" ), "checkWord() did not correct repeating middle character" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jaaazzzzer" ), "checkWord() did not correct repeating middle character" )

    def testCheckWordCorrectsRepeatingCharactersInMultiplePlacesWithNaturalRepeats(self):
        self.mySpellChecker.addWord( "jazzer" )

        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jjazzer" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jjaazzer" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jjaazzzer" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jjaazzzeer" ), "checkWord() did not correct repeating character all over the place" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "jjaazzzeerr" ), "checkWord() did not correct repeating character all over the place" )

    def testCheckWordCorrectsMixedAndTitleCaseToLowerCaseAndRemovesRepeats(self):
        self.mySpellChecker.addWord( "carrot" )
        self.assertEqual( "carrot", self.mySpellChecker.checkWord( "CaRrrOT" ), "checkWord() did not correct mixed case to lower case" )
        self.assertEqual( "carrot", self.mySpellChecker.checkWord( "CarrotT" ), "checkWord() did not correct title case to lower case" )

    def testCheckWordCorrectsMixedAndTitleCaseToLowerCaseAndRemovesRepeatsWithNaturalRepears(self):
        self.mySpellChecker.addWord( "jazzer" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "JaZZzeR" ), "checkWord() did not correct mixed case to lower case" )
        self.assertEqual( "jazzer", self.mySpellChecker.checkWord( "Jazzeer" ), "checkWord() did not correct title case to lower case" )

    def testCheckWordCorrectsLowerAndMixedCaseToTitleCaseAndRemovesRepeats(self):
        self.mySpellChecker.addWord( "Moscow" )
        self.assertEqual( "Moscow", self.mySpellChecker.checkWord( "MoscCoow" ), "checkWord() did not correct mixed case to title case" )
        self.assertEqual( "Moscow", self.mySpellChecker.checkWord( "mossscoww" ), "checkWord() did not correct lower case to title case" )

    def testCheckWordCorrectsMixedCaseToTitleCaseAndRemovesRepeats(self):
        self.mySpellChecker.addWord( "Moscow" )
        self.assertEqual( "Moscow", self.mySpellChecker.checkWord( "MmosSscoww" ), "checkWord() did not correct lower case to title case" )

    def testCheckWordPrefersExactInput(self):
        self.mySpellChecker.addWord( "poop" )
        self.mySpellChecker.addWord( "pop" )
        self.mySpellChecker.addWord( "Pop" )

        self.assertEqual( "poop", self.mySpellChecker.checkWord( "poop" ), "checkWord() did not prefer the entered word" )
        self.assertEqual( "pop", self.mySpellChecker.checkWord( "pop" ), "checkWord() did not prefer the entered word" )
        self.assertEqual( "Pop", self.mySpellChecker.checkWord( "Pop" ), "checkWord() did not prefer the entered word" )

    def testCheckWordPrefersLongestMatching(self):
        self.mySpellChecker.addWord( "loop" )
        self.mySpellChecker.addWord( "lop" )

        self.assertEqual( "loop", self.mySpellChecker.checkWord( "looooopppp" ), "checkWord() did not prefer the longest match" )

    def testCheckWordCorrectsLowerAndTitleCaseToMixedCaseAndRemovesRepeats(self):
        self.mySpellChecker.addWord( "Jean-Pierre" )
        self.assertEqual( "Jean-Pierre", self.mySpellChecker.checkWord( "Jeaan-pierre" ), "checkWord() did not correct to mixed case" )
        self.assertEqual( "Jean-Pierre", self.mySpellChecker.checkWord( "jean-pieeRrre" ), "checkWord() did not correct to mixed case" )
Example #9
0
 def setUp(self):
     self.mySpellChecker = SpellChecker()
     [self.mySpellChecker.addWord( line.strip() ) for line in open('/usr/share/dict/words')]
Example #10
0
class Test(unittest.TestCase):


    def setUp(self):
        self.mySpellChecker = SpellChecker()
        [self.mySpellChecker.addWord( line.strip() ) for line in open('/usr/share/dict/words')]

    def tearDown(self):
        pass


    def CheckResult(self, anExpected, anInput ):
        theResult = self.mySpellChecker.checkWord( anInput )
        if theResult != anExpected:
            print "Input " + anInput + " failed: expected = " + anExpected + ", actual = " + theResult

    def testCheckResult(self):
        self.CheckResult( "Aaron", "Aaron" )
        self.CheckResult( "aardvark", "aardvark" )
        self.CheckResult( "aardvark", "AARDvark" )
        self.CheckResult( "aardvark", "AaRDvark" )
        self.CheckResult( "aardvark", "AAAAAAAaRDvark" )
        self.CheckResult( "aardvark", "aaaaaarddddvaaaaaark" )
        self.CheckResult( "platypus", "platypus" )
        self.CheckResult( "bookkeeper", "bookkeeper" )
        self.CheckResult( "bookkeeper", "boOkKeEper" )
        self.CheckResult( "bookkeeper", "boookkkeeeper" )
        self.CheckResult( "subbookkeeper", "subbookkeeper" )
        self.CheckResult( "subbookkeeper", "sUbbOOkkEEpEr" )
        self.CheckResult( "mele", "mele" )
        self.CheckResult( "melee", "melee" )
        self.CheckResult( "melee", "mellee" ) #// or "mele" can also be a valid correction
        self.CheckResult( "melee", "mmeelleeee" ) #// or "mele" can also be a valid correction
        self.CheckResult( "melee", "mMEelLeEEe" ) #// or "mele" can also be a valid correction
        self.CheckResult( "carrot", "CaRrOt" )
        self.CheckResult( "phone", "pPhone" )
        self.CheckResult( "Boolian", "Boolian" )
        self.CheckResult( "Boolian", "BOOLian" )
        self.CheckResult( "Boolian", "boolian" )
        self.CheckResult( "Boolian", "boolIaN" )
        self.CheckResult( "Boolian", "bOoLiAn" )
        self.CheckResult( "Xyris", "Xyris" )
        self.CheckResult( "Xyris", "xyris" )
        self.CheckResult( "Xyris", "XYRIS" )
        self.CheckResult( "Xyris", "XyRiS" )
        self.CheckResult( "Xyris", "xYrIs" )
        self.CheckResult( "Xyris", "xYRIs" )

        self.CheckResult( "duck", "duck" )
        self.CheckResult( "duck", "duucck" )

        self.CheckResult( "no correction found", "Shyela" )
        self.CheckResult( "no correction found", "Toyota" )

        self.CheckResult( "carrot", "CarOt" )  #// extra credit

        # --------------------- ADDITIONAL

        self.CheckResult( "poop", "poop" )
        self.CheckResult( "pop", "pop" )
        self.CheckResult( "Pop", "Pop" )
        self.CheckResult( "loop", "looooopppp" )
Example #11
0
 def setUp(self):
     self.mySpellChecker = SpellChecker()
     [
         self.mySpellChecker.addWord(line.strip())
         for line in open('/usr/share/dict/words')
     ]
Example #12
0
class Test(unittest.TestCase):
    def setUp(self):
        self.mySpellChecker = SpellChecker()
        [
            self.mySpellChecker.addWord(line.strip())
            for line in open('/usr/share/dict/words')
        ]

    def tearDown(self):
        pass

    def CheckResult(self, anExpected, anInput):
        theResult = self.mySpellChecker.checkWord(anInput)
        if theResult != anExpected:
            print "Input " + anInput + " failed: expected = " + anExpected + ", actual = " + theResult

    def testCheckResult(self):
        self.CheckResult("Aaron", "Aaron")
        self.CheckResult("aardvark", "aardvark")
        self.CheckResult("aardvark", "AARDvark")
        self.CheckResult("aardvark", "AaRDvark")
        self.CheckResult("aardvark", "AAAAAAAaRDvark")
        self.CheckResult("aardvark", "aaaaaarddddvaaaaaark")
        self.CheckResult("platypus", "platypus")
        self.CheckResult("bookkeeper", "bookkeeper")
        self.CheckResult("bookkeeper", "boOkKeEper")
        self.CheckResult("bookkeeper", "boookkkeeeper")
        self.CheckResult("subbookkeeper", "subbookkeeper")
        self.CheckResult("subbookkeeper", "sUbbOOkkEEpEr")
        self.CheckResult("mele", "mele")
        self.CheckResult("melee", "melee")
        self.CheckResult(
            "melee", "mellee")  #// or "mele" can also be a valid correction
        self.CheckResult(
            "melee",
            "mmeelleeee")  #// or "mele" can also be a valid correction
        self.CheckResult(
            "melee",
            "mMEelLeEEe")  #// or "mele" can also be a valid correction
        self.CheckResult("carrot", "CaRrOt")
        self.CheckResult("phone", "pPhone")
        self.CheckResult("Boolian", "Boolian")
        self.CheckResult("Boolian", "BOOLian")
        self.CheckResult("Boolian", "boolian")
        self.CheckResult("Boolian", "boolIaN")
        self.CheckResult("Boolian", "bOoLiAn")
        self.CheckResult("Xyris", "Xyris")
        self.CheckResult("Xyris", "xyris")
        self.CheckResult("Xyris", "XYRIS")
        self.CheckResult("Xyris", "XyRiS")
        self.CheckResult("Xyris", "xYrIs")
        self.CheckResult("Xyris", "xYRIs")

        self.CheckResult("duck", "duck")
        self.CheckResult("duck", "duucck")

        self.CheckResult("no correction found", "Shyela")
        self.CheckResult("no correction found", "Toyota")

        self.CheckResult("carrot", "CarOt")  #// extra credit

        # --------------------- ADDITIONAL

        self.CheckResult("poop", "poop")
        self.CheckResult("pop", "pop")
        self.CheckResult("Pop", "Pop")
        self.CheckResult("loop", "looooopppp")