For a starter

convert number to English words

/**
  * @author lwpro
  * @since 10/17/2017
  * @version 1
  */
object NumberTranslator extends App {

  def translateSingle(num: Int): String = {
    num match {
      case 0 => "zero"
      case 1 => "one"
      case 2 => "two"
      case 3 => "three"
      case 4 => "four"
      case 5 => "five"
      case 6 => "six"
      case 7 => "seven"
      case 8 => "eight"
      case 9 => "nine"
    }
  }

    def translateDouble(num: Int): String = {

      num match {
        case 10 => "ten"
        case 11 => "elven"
        case 12 => "twelve"
        case 13 => "thirteen"
        case 14 => "fourteen"
        case 15 => "fifteen"
        case 16 => "sixteen"
        case 17 => "seventeen"
        case 18 => "eighteen"
        case 19 => "nineteen"
        case 20 => "twenty"
        case x if 21 until 30 contains x => "twenty " concat (translateSingle(x - 20))
        case 30 => "thirty"
        case x if 31 until 40 contains x => "thirty " concat (translateSingle(x - 30))
        case 40 => "forty"
        case x if 41 until 50 contains x => "forty " concat (translateSingle(x - 40))
        case 50 => "fifty"
        case x if 51 until 60 contains x => "fifty " concat (translateSingle(x - 50))
        case 60 => "sixty"
        case x if 61 until 70 contains x => "sixty " concat (translateSingle(x - 60))
        case 70 => "seventy"
        case x if 71 until 80 contains x => "seventy " concat (translateSingle(x - 70))
        case 80 => "eighty"
        case x if 81 until 90 contains x => "eightty " concat (translateSingle(x - 80))
        case 90 => "ninety"
        case x if 90 until 100 contains x => "ninety " concat (translateSingle(x - 90))
      }
    }


    def translateBlock(num: Int) = {
      num match {
        case x if 0 until 10 contains x => translateSingle(num)
        case x if 10 until 100 contains x => translateDouble(num)
        case x if (100 until 1000 contains x) && (x %100 == 0) => translateSingle(num / 100) concat " hundred"
        case x if x % 100 < 10 => translateSingle(num / 100) concat " hundred and " concat (translateSingle(num % 100) )
        case _ => translateSingle(num / 100) concat " hundred and " concat (translateDouble(num % 100) )
      }
    }

  for (i <- 0 until 1000)
    println( i.toString concat("::") concat translateBlock(i))


  def translateWhole (num: Int) = {
    num toString() length  match {
      case x if 0 until 3 contains x => translateBlock(num)
      case x if 4 until 6 contains x => translateBlock(num / 1000) concat("thousand and ") concat(translateBlock(num %1000))
      case x if 7 until 9 contains x => translateBlock(num / 1000000) concat("million and ") concat translateBlock(num % 1000 /1000) concat("thousand and ") concat(translateBlock(num %1000 /1000 % 1000))
    }
  }

  }


Advertisements

Another angle of view: imperative/procedural vs functional/declarative

quote
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/functional-programming-vs-imperative-programming
 

Transitioning for OOP Developers
In traditional object-oriented programming (OOP), most developers are accustomed to programming in the imperative/procedural style. To switch to developing in a pure functional style, they have to make a transition in their thinking and their approach to development.
To solve problems, OOP developers design class hierarchies, focus on proper encapsulation, and think in terms of class contracts. The behavior and state of object types are paramount, and language features, such as classes, interfaces, inheritance, and polymorphism, are provided to address these concerns.
In contrast, functional programming approaches computational problems as an exercise in the evaluation of pure functional transformations of data collections. Functional programming avoids state and mutable data, and instead emphasizes the application of functions.

AI for system support

Have tried to build an AI bot since almost 3 years back, finally did a prototype, in case anybody would like to do something similar:

Technologies:

Java, Scala, Python, Anaconda, Scikit Learn,  EWS, BootStrap, AngularJS/JQuery

Components:

Data Set

  1. I have built a scala web crawler, to download all historical support issues.
  2. at the same time, have manually cleaned up/read through each of the thousand of support issues, put in corresponding resolutions corresponding to each
AI
  1. have leveraged on anaconda & scikit learn, to NLP, to tokenize each support issue (text), remove stop words, stemmed each, remove punctuations
  2. have leveraged on anaconda & scikit learn, bag each token of the text as feature vs class, to feed into linear regression classifier, tried SLDA, so far working at 72% accuracy
AI Exposer
  1. have exposed AI as a service
Issue Feeder
  1. have leveraged EWS to read in all issues, post to AI service
UI
  1. have built a web user interface, on top of HTML5 + JQuery + Bootstrap, to show the support emails + AI responded resolutions
  2. have a option on UI, to provide user feedback to AI, to keep its intelligence updated
Notifier
  1. leverage on Java Mail API, Chat API, to post alerts for critical issues